From e865f82f8add8efd244a3052efd209942ad7892b Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Fri, 13 Nov 2020 01:07:08 +0200 Subject: [PATCH] Always use SwiftMailerHandler --- .../EmailChannel/EmailChannel.php | 42 ++++--------- .../EmailChannel/EmailChannelTest.php | 63 +++---------------- 2 files changed, 20 insertions(+), 85 deletions(-) diff --git a/src/Loggable/Notifications/EmailChannel/EmailChannel.php b/src/Loggable/Notifications/EmailChannel/EmailChannel.php index 9b3487c..3d2c7be 100644 --- a/src/Loggable/Notifications/EmailChannel/EmailChannel.php +++ b/src/Loggable/Notifications/EmailChannel/EmailChannel.php @@ -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; @@ -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()) { diff --git a/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php b/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php index 3575211..bd4e20a 100644 --- a/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php +++ b/tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php @@ -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; @@ -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() { @@ -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); @@ -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' => 'john.doe@example.com', 'name' => 'John Doe'], - ['address' => 'jane.smith@example.com', 'name' => 'Jane Smith'], - ]), - "[TESTING] %level_name% in `{$name}` command", - to_rfc2822_email([ - 'address' => 'no-reply@example.com', - 'name' => 'ICLogger Notification', - ]), - Logger::NOTICE - ); - $handler->setContentType('text/html'); - $handler->setFormatter(new MonologHtmlFormatter); - - return $handler; - } - /** * Compose "deduplication" handler. * @@ -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' => 'no-reply@example.com', 'name' => 'ICLogger Notification',