Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][FEATURE] Add new notifications feature #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 74 additions & 11 deletions Classes/Command/ReportCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
* LICENSE.txt file that was distributed with this source code.
*/

use T3Monitor\T3monitoring\Domain\Model\Client;
use T3Monitor\T3monitoring\Domain\Model\Extension;
use T3Monitor\T3monitoring\Domain\Repository\ClientRepository;
use T3Monitor\T3monitoring\Notification\EmailNotification;
use T3Monitor\T3monitoring\Notification\Channel\EmailChannel;
use T3Monitor\T3monitoring\Notification\ReportNotification;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Lang\LanguageService;
use UnexpectedValueException;

Expand All @@ -21,10 +24,6 @@
*/
class ReportCommandController extends CommandController
{

/** @var EmailNotification */
protected $emailNotification;

/** @var LanguageService */
protected $languageService;

Expand All @@ -44,15 +43,17 @@ public function injectClientRepository(ClientRepository $clientRepository)
*/
public function __construct()
{
$this->emailNotification = GeneralUtility::makeInstance(EmailNotification::class);
$this->languageService = $GLOBALS['LANG'];
}

/**
* Generate collective report for all insecure clients (core or extensions)
*
* @param string $email Send email to this email address
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function adminCommand($email = '')
{
Expand All @@ -63,13 +64,26 @@ public function adminCommand($email = '')
return;
}

$arguments = [
'email' => $email,
'clients' => $clients
];
$text = $this->getFluidTemplate($arguments, 'AdminEmail.txt', 'txt');
$notification = GeneralUtility::makeInstance(ReportNotification::class, 'Monitoring Report', $text);
if (!empty($email)) {
if (GeneralUtility::validEmail($email)) {
$this->emailNotification->sendAdminEmail($email, $clients);
} else {
if (!GeneralUtility::validEmail($email)) {
throw new UnexpectedValueException(sprintf('Email address "%s" is invalid!', $email));
}
} else {

$channelOverrideConfiguration = $notification->getOverrideChannelConfig();
$channelOverrideConfiguration[EmailChannel::class] = [
'recipientAddress' => $email
];
$notification->setOverrideChannelConfig($channelOverrideConfiguration);
}
$notification->send();

if (empty($email)) {
$collectedClientData = [];
foreach ($clients as $client) {
$insecureExtensions = [];
Expand Down Expand Up @@ -101,6 +115,10 @@ public function adminCommand($email = '')

/**
* Client command
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws \UnexpectedValueException
*/
public function clientCommand()
{
Expand All @@ -111,7 +129,30 @@ public function clientCommand()
return;
}

$this->emailNotification->sendClientEmail($clients);
/** @var Client $client */
foreach ($clients as $client) {
$email = $client->getEmail();
if (!GeneralUtility::validEmail($email)) {
continue;
}
$arguments = [
'email' => $email,
'client' => $client
];
$text = $this->getFluidTemplate($arguments, 'ClientEmail.txt', 'txt');
$notification = GeneralUtility::makeInstance(ReportNotification::class, 'Monitoring Report', $text);
if (!empty($email)) {
if (!GeneralUtility::validEmail($email)) {
throw new UnexpectedValueException(sprintf('Email address "%s" is invalid!', $email));
}
$channelOverrideConfiguration = $notification->getOverrideChannelConfig();
$channelOverrideConfiguration[EmailChannel::class] = [
'recipientAddress' => $email
];
$notification->setOverrideChannelConfig($channelOverrideConfiguration);
}
$notification->send();
}
}

/**
Expand All @@ -123,4 +164,26 @@ protected function getLabel($key)
return $this->languageService->sL('LLL:EXT:t3monitoring/Resources/Private/Language/locallang.xlf:' . $key);
}

/**
* Creates a fluid instance with given template-file
*
* @param array $arguments
* @param string $file Path below Template-Root-Path
* @param string $format
*
* @return string
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
protected function getFluidTemplate(array $arguments, $file, $format = 'html')
{
/** @var StandaloneView $renderer */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to avoid those lines

$renderer = GeneralUtility::makeInstance(StandaloneView::class);
$renderer->setFormat($format);
$path = GeneralUtility::getFileAbsFileName('EXT:t3monitoring/Resources/Private/Templates/Notification/' . $file);
$renderer->setTemplatePathAndFilename($path);
$renderer->assignMultiple($arguments);

return trim($renderer->render());
}
}
157 changes: 157 additions & 0 deletions Classes/Notification/AbstractNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
namespace T3Monitor\T3monitoring\Notification;

/*
* This file is part of the t3monitoring extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

use T3Monitor\T3monitoring\Notification\Channel\AbstractNotificationChannel;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class AbstractNotification
*/
abstract class AbstractNotification {
/**
* @var AbstractNotificationChannel[]
*/
protected $channels;

/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna move the /** @var array */ syntax? because no description will be used and will save some lines.

* @var array
*/
protected $overrideChannelConfig;

/**
* @var string
*/
protected $subject;

/**
* @var string
*/
protected $message;

/**
* @var array
*/
protected $data;

/**
* AbstractNotification constructor.
*
* @param string $subject
* @param string $message
* @param array $data
*
* @throws \InvalidArgumentException
*/
public function __construct($subject, $message = '', array $data = []) {
$this->subject = $subject;
$this->message = $message;
$this->data = $data;

$channels = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3monitoring'][get_class($this)]['channels'];
if (!empty($channels) && is_array($channels)) {
foreach ($channels as $channelClass => $channelConfig) {
$this->channels[] = GeneralUtility::makeInstance($channelClass, $channelConfig);
}
}
}

/**
* Send notification to all configured channels
*/
public function send() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's follow cgl, new lines

if (!empty($this->channels)) {
foreach ($this->channels as $channel) {
if (!empty($this->overrideChannelConfig[get_class($channel)])) {
$options = $channel->getOptions();
ArrayUtility::mergeRecursiveWithOverrule($options, $this->overrideChannelConfig[get_class($channel)]);
$channel->setOptions($options);
}
$channel->process($this);
}
}
}

/**
* @return string
*/
public function getSubject()
{
return $this->subject;
}

/**
* @param string $subject
*
* @return $this
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}

/**
* @return string
*/
public function getMessage()
{
return $this->message;
}

/**
* @param string $message
*
* @return $this
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}

/**
* @return array
*/
public function getData()
{
return $this->data;
}

/**
* @param array $data
*
* @return $this
*/
public function setData(array $data)
{
$this->data = $data;
return $this;
}

/**
* @return mixed
*/
public function getOverrideChannelConfig()
{
return $this->overrideChannelConfig;
}

/**
* @param mixed $overrideChannelConfig
*
* @return AbstractNotification
*/
public function setOverrideChannelConfig($overrideChannelConfig)
{
$this->overrideChannelConfig = $overrideChannelConfig;
return $this;
}
}
49 changes: 49 additions & 0 deletions Classes/Notification/Channel/AbstractNotificationChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace T3Monitor\T3monitoring\Notification\Channel;

/*
* This file is part of the t3monitoring extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

use T3Monitor\T3monitoring\Notification\AbstractNotification;

/**
* Class AbstractNotificationChannel
*/
abstract class AbstractNotificationChannel implements NotificationChannelInterface {
/**
* @var array
*/
protected $options;

public function __construct(array $options) {
$this->options = $options;
}

/**
* @param AbstractNotification $notification
*/
abstract public function process(AbstractNotification $notification);

/**
* @return array
*/
public function getOptions()
{
return $this->options;
}

/**
* @param array $options
*
* @return AbstractNotificationChannel
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}
}
40 changes: 40 additions & 0 deletions Classes/Notification/Channel/EmailChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace T3Monitor\T3monitoring\Notification\Channel;

/*
* This file is part of the t3monitoring extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

use T3Monitor\T3monitoring\Notification\AbstractNotification;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class EmailChannel
* Options:
* - senderAddress
* - senderName
* - recipientAddress
*/
class EmailChannel extends AbstractNotificationChannel
{
/**
* @param AbstractNotification $notification
*
* @throws \InvalidArgumentException
*/
public function process(AbstractNotification $notification)
{
$mailMessage = GeneralUtility::makeInstance(MailMessage::class);
$mailMessage
->setSubject($notification->getSubject())
->addFrom($this->getOptions()['senderAddress'], $this->getOptions()['senderName'])
->setTo($this->getOptions()['recipientAddress'])
->setBody($notification->getMessage())
->setContentType('text/plain');
$mailMessage->send();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check response?

}
}
Loading