Skip to content

Commit 9cfbc39

Browse files
committed
Always use SwiftMailerHandler
1 parent a717509 commit 9cfbc39

File tree

2 files changed

+20
-85
lines changed

2 files changed

+20
-85
lines changed

Diff for: src/Loggable/Notifications/EmailChannel/EmailChannel.php

+12-30
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Illuminated\Console\Loggable\Notifications\EmailChannel;
44

55
use Monolog\Handler\DeduplicationHandler;
6-
use Monolog\Handler\NativeMailerHandler;
76
use Monolog\Handler\SwiftMailerHandler;
87
use Monolog\Logger;
98
use Swift_Mailer;
@@ -37,35 +36,18 @@ protected function getEmailChannelHandler()
3736
$from = $this->getEmailNotificationsFrom();
3837
$level = $this->getEmailNotificationsLevel();
3938

40-
$driver = config('mail.driver');
41-
switch ($driver) {
42-
case 'null':
43-
return false;
44-
45-
case 'mail':
46-
case 'smtp':
47-
case 'sendmail':
48-
/** @var Swift_Mailer $mailer */
49-
$mailer = app('mailer')->getSwiftMailer();
50-
51-
/** @var Swift_Message $message */
52-
$message = $mailer->createMessage();
53-
$message->setSubject($subject);
54-
$message->setFrom(to_swiftmailer_emails($from));
55-
$message->setTo(to_swiftmailer_emails($recipients));
56-
$message->setContentType('text/html');
57-
$message->setCharset('utf-8');
58-
59-
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
60-
break;
61-
62-
default:
63-
$to = to_rfc2822_email($recipients);
64-
$from = to_rfc2822_email($from);
65-
$mailerHandler = new NativeMailerHandler($to, $subject, $from, $level);
66-
$mailerHandler->setContentType('text/html');
67-
break;
68-
}
39+
/** @var Swift_Mailer $mailer */
40+
$mailer = app('mailer')->getSwiftMailer();
41+
42+
/** @var Swift_Message $message */
43+
$message = $mailer->createMessage();
44+
$message->setSubject($subject);
45+
$message->setFrom(to_swiftmailer_emails($from));
46+
$message->setTo(to_swiftmailer_emails($recipients));
47+
$message->setContentType('text/html');
48+
$message->setCharset('utf-8');
49+
50+
$mailerHandler = new SwiftMailerHandler($mailer, $message, $level);
6951
$mailerHandler->setFormatter(new MonologHtmlFormatter);
7052

7153
if ($this->useEmailNotificationsDeduplication()) {

Diff for: tests/Loggable/Notifications/EmailChannel/EmailChannelTest.php

+8-55
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Illuminated\Console\Tests\App\Console\Commands\EmailNotificationsInvalidRecipientsCommand;
99
use Illuminated\Console\Tests\TestCase;
1010
use Monolog\Handler\DeduplicationHandler;
11-
use Monolog\Handler\NativeMailerHandler;
1211
use Monolog\Handler\SwiftMailerHandler;
1312
use Monolog\Logger;
1413
use Swift_Message;
@@ -23,17 +22,6 @@ public function it_validates_and_filters_notification_recipients()
2322
$this->assertNotInstanceOf(SwiftMailerHandler::class, $command->emailChannelHandler());
2423
}
2524

26-
/** @test */
27-
public function it_is_disabled_on_null_driver()
28-
{
29-
config(['mail.driver' => 'null']);
30-
31-
/** @var EmailNotificationsCommand $command */
32-
$command = $this->runArtisan(new EmailNotificationsCommand);
33-
34-
$this->assertFalse($command->createEmailChannelHandler());
35-
}
36-
3725
/** @test */
3826
public function it_uses_configured_monolog_swift_mailer_handler_on_mail_driver()
3927
{
@@ -71,21 +59,10 @@ public function it_uses_configured_monolog_swift_mailer_handler_on_sendmail_driv
7159
$this->assertMailerHandlersEqual($this->composeSwiftMailerHandler(), $command->emailChannelHandler());
7260
}
7361

74-
/** @test */
75-
public function it_uses_configured_monolog_native_mailer_handler_on_other_drivers()
76-
{
77-
config(['mail.driver' => 'any-other']);
78-
79-
/** @var EmailNotificationsCommand $command */
80-
$command = $this->runArtisan(new EmailNotificationsCommand);
81-
82-
$this->assertMailerHandlersEqual($this->composeNativeMailerHandler(), $command->emailChannelHandler());
83-
}
84-
8562
/** @test */
8663
public function it_uses_configured_monolog_deduplication_handler_if_deduplication_enabled()
8764
{
88-
config(['mail.driver' => 'any-other']);
65+
config(['mail.driver' => 'sendmail']);
8966

9067
/** @var EmailNotificationsDeduplicationCommand $command */
9168
$command = $this->runArtisan(new EmailNotificationsDeduplicationCommand);
@@ -98,43 +75,18 @@ public function it_uses_configured_monolog_deduplication_handler_if_deduplicatio
9875
/**
9976
* Compose "swift mailer" handler.
10077
*
78+
* @param string $name
10179
* @return \Monolog\Handler\SwiftMailerHandler
10280
*/
103-
private function composeSwiftMailerHandler()
81+
private function composeSwiftMailerHandler($name = 'email-notifications-command')
10482
{
105-
$handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage(), Logger::NOTICE);
83+
$handler = new SwiftMailerHandler(app('mailer')->getSwiftMailer(), $this->composeMailerHandlerMessage($name), Logger::NOTICE);
10684

10785
$handler->setFormatter(new MonologHtmlFormatter);
10886

10987
return $handler;
11088
}
11189

112-
/**
113-
* Compose "native mailer" handler.
114-
*
115-
* @param string $name
116-
* @return \Monolog\Handler\NativeMailerHandler
117-
*/
118-
private function composeNativeMailerHandler(string $name = 'email-notifications-command')
119-
{
120-
$handler = new NativeMailerHandler(
121-
to_rfc2822_email([
122-
['address' => '[email protected]', 'name' => 'John Doe'],
123-
['address' => '[email protected]', 'name' => 'Jane Smith'],
124-
]),
125-
"[TESTING] %level_name% in `{$name}` command",
126-
to_rfc2822_email([
127-
'address' => '[email protected]',
128-
'name' => 'ICLogger Notification',
129-
]),
130-
Logger::NOTICE
131-
);
132-
$handler->setContentType('text/html');
133-
$handler->setFormatter(new MonologHtmlFormatter);
134-
135-
return $handler;
136-
}
137-
13890
/**
13991
* Compose "deduplication" handler.
14092
*
@@ -143,20 +95,21 @@ private function composeNativeMailerHandler(string $name = 'email-notifications-
14395
private function composeDeduplicationHandler()
14496
{
14597
return new DeduplicationHandler(
146-
$this->composeNativeMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
98+
$this->composeSwiftMailerHandler('email-notifications-deduplication-command'), null, Logger::NOTICE, 60
14799
);
148100
}
149101

150102
/**
151103
* Compose mailer handler message.
152104
*
105+
* @param string $name
153106
* @return \Swift_Message
154107
*/
155-
private function composeMailerHandlerMessage()
108+
private function composeMailerHandlerMessage($name = 'email-notifications-command')
156109
{
157110
/** @var Swift_Message $message */
158111
$message = app('mailer')->getSwiftMailer()->createMessage();
159-
$message->setSubject('[TESTING] %level_name% in `email-notifications-command` command');
112+
$message->setSubject("[TESTING] %level_name% in `{$name}` command");
160113
$message->setFrom(to_swiftmailer_emails([
161114
'address' => '[email protected]',
162115
'name' => 'ICLogger Notification',

0 commit comments

Comments
 (0)