Skip to content

Commit

Permalink
Merge branch 'master' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Nov 12, 2020
2 parents ca8df03 + e865f82 commit e392eea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 85 deletions.
42 changes: 12 additions & 30 deletions src/Loggable/Notifications/EmailChannel/EmailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Str;
use Monolog\Handler\DeduplicationHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\SwiftMailerHandler;
use Monolog\Logger;
use Swift_Mailer;
Expand Down Expand Up @@ -38,35 +37,18 @@ protected function getEmailChannelHandler()
$from = $this->getEmailNotificationsFrom();
$level = $this->getEmailNotificationsLevel();

$driver = config('mail.driver');
switch ($driver) {
case 'null':
return false;

case 'mail':
case 'smtp':
case 'sendmail':
/** @var Swift_Mailer $mailer */
$mailer = app('mailer')->getSwiftMailer();

/** @var Swift_Message $message */
$message = $mailer->createMessage();
$message->setSubject($subject);
$message->setFrom(to_swiftmailer_emails($from));
$message->setTo(to_swiftmailer_emails($recipients));
$message->setContentType('text/html');
$message->setCharset('utf-8');

$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
break;

default:
$to = to_rfc2822_email($recipients);
$from = to_rfc2822_email($from);
$mailerHandler = new NativeMailerHandler($to, $subject, $from, $level);
$mailerHandler->setContentType('text/html');
break;
}
/** @var Swift_Mailer $mailer */
$mailer = app('mailer')->getSwiftMailer();

/** @var Swift_Message $message */
$message = $mailer->createMessage();
$message->setSubject($subject);
$message->setFrom(to_swiftmailer_emails($from));
$message->setTo(to_swiftmailer_emails($recipients));
$message->setContentType('text/html');
$message->setCharset('utf-8');

$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
$mailerHandler->setFormatter(new MonologHtmlFormatter);

if ($this->useEmailNotificationsDeduplication()) {
Expand Down
63 changes: 8 additions & 55 deletions tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminated\Console\Tests\App\Console\Commands\EmailNotificationsInvalidRecipientsCommand;
use Illuminated\Console\Tests\TestCase;
use Monolog\Handler\DeduplicationHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\SwiftMailerHandler;
use Monolog\Logger;
use Swift_Message;
Expand All @@ -23,17 +22,6 @@ public function it_validates_and_filters_notification_recipients()
$this->assertNotInstanceOf(SwiftMailerHandler::class, $command->emailChannelHandler());
}

/** @test */
public function it_is_disabled_on_null_driver()
{
config(['mail.driver' => 'null']);

/** @var EmailNotificationsCommand $command */
$command = $this->runArtisan(new EmailNotificationsCommand);

$this->assertFalse($command->createEmailChannelHandler());
}

/** @test */
public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver()
{
Expand Down Expand Up @@ -71,21 +59,10 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driv
$this->assertMailerHandlersEqual($this->composeSwiftMailerHandler(), $command->emailChannelHandler());
}

/** @test */
public function it_uses_configured_monolog_native_mailer_handler_on_other_drivers()
{
config(['mail.driver' => 'any-other']);

/** @var EmailNotificationsCommand $command */
$command = $this->runArtisan(new EmailNotificationsCommand);

$this->assertMailerHandlersEqual($this->composeNativeMailerHandler(), $command->emailChannelHandler());
}

/** @test */
public function it_uses_configured_monolog_deduplication_handler_if_deduplication_enabled()
{
config(['mail.driver' => 'any-other']);
config(['mail.driver' => 'sendmail']);

/** @var EmailNotificationsDeduplicationCommand $command */
$command = $this->runArtisan(new EmailNotificationsDeduplicationCommand);
Expand All @@ -98,43 +75,18 @@ public function it_uses_configured_monolog_deduplication_handler_if_deduplicatio
/**
* Compose "swift mailer" handler.
*
* @param string $command
* @return \Monolog\Handler\SwiftMailerHandler
*/
private function composeSwiftMailerHandler()
private function composeSwiftMailerHandler($command = 'email-notifications-command')
{
$handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage(), Logger::NOTICE);
$handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage($command), Logger::NOTICE);

$handler->setFormatter(new MonologHtmlFormatter);

return $handler;
}

/**
* Compose "native mailer" handler.
*
* @param string $name
* @return \Monolog\Handler\NativeMailerHandler
*/
private function composeNativeMailerHandler(string $name = 'email-notifications-command')
{
$handler = new NativeMailerHandler(
to_rfc2822_email([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => '[email protected]', 'name' => 'Jane Smith'],
]),
"[TESTING] %level_name% in `{$name}` command",
to_rfc2822_email([
'address' => '[email protected]',
'name' => 'ICLogger Notification',
]),
Logger::NOTICE
);
$handler->setContentType('text/html');
$handler->setFormatter(new MonologHtmlFormatter);

return $handler;
}

/**
* Compose "deduplication" handler.
*
Expand All @@ -143,20 +95,21 @@ private function composeNativeMailerHandler(string $name = 'email-notifications-
private function composeDeduplicationHandler()
{
return new DeduplicationHandler(
$this->composeNativeMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
$this->composeSwiftMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
);
}

/**
* Compose mailer handler message.
*
* @param string $command
* @return \Swift_Message
*/
private function composeMailerHandlerMessage()
private function composeMailerHandlerMessage($command)
{
/** @var Swift_Message $message */
$message = app('mailer')->getSwiftMailer()->createMessage();
$message->setSubject('[TESTING] %level_name% in `email-notifications-command` command');
$message->setSubject("[TESTING] %level_name% in `{$command}` command");
$message->setFrom(to_swiftmailer_emails([
'address' => '[email protected]',
'name' => 'ICLogger Notification',
Expand Down

0 comments on commit e392eea

Please sign in to comment.