From 8eba77a92b5bf37701143092dd744256789bec8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Gusm=C3=A3o?= Date: Tue, 28 Nov 2023 10:04:50 +0000 Subject: [PATCH] standardizing the beforeSendPerformed method and making the handle method public to use manually --- src/Listeners/SwiftEmbedImages.php | 16 +++++++++++++--- src/Listeners/SymfonyEmbedImages.php | 15 ++++++++++++--- src/ServiceProvider.php | 2 +- tests/Traits/InteractsWithMessage.php | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php index ee56869..ec3490b 100755 --- a/src/Listeners/SwiftEmbedImages.php +++ b/src/Listeners/SwiftEmbedImages.php @@ -14,6 +14,8 @@ use Swift_Events_SendEvent; use Swift_Events_SendListener; use Swift_Message; +use Swift_Mime_SimpleMessage; +use Symfony\Component\Mime\Email; class SwiftEmbedImages implements Swift_Events_SendListener { @@ -23,7 +25,7 @@ class SwiftEmbedImages implements Swift_Events_SendListener private $config; /** - * @var Swift_Message + * @var Email|Swift_Mime_SimpleMessage */ private $message; @@ -40,8 +42,16 @@ public function __construct($config) */ public function beforeSendPerformed(Swift_Events_SendEvent $evt) { - $this->message = $evt->getMessage(); + $this->handle($evt->getMessage()); + } + /** + * @param Swift_Mime_SimpleMessage $message + * @return void + */ + public function handle(Swift_Mime_SimpleMessage $message) + { + $this->message = $message; $this->attachImages(); } @@ -215,4 +225,4 @@ private function embed(Embedder $embedder, $src) return $src; } -} +} \ No newline at end of file diff --git a/src/Listeners/SymfonyEmbedImages.php b/src/Listeners/SymfonyEmbedImages.php index 38ba3a8..2fc4a7d 100644 --- a/src/Listeners/SymfonyEmbedImages.php +++ b/src/Listeners/SymfonyEmbedImages.php @@ -23,7 +23,7 @@ class SymfonyEmbedImages private $config; /** - * @var Email|Swift_Message + * @var Email */ private $message; @@ -38,10 +38,18 @@ public function __construct($config) /** * @param MessageSending $event */ - public function handle(MessageSending $event) + public function beforeSendPerformed(MessageSending $event) { - $this->message = $event->message; + $this->handle($event->message); + } + /** + * @param Email $message + * @return void + */ + public function handle(Email $message) + { + $this->message = $message; $this->attachImages(); } @@ -195,3 +203,4 @@ private function embed(Embedder $embedder, $src) return $src; } } + diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 5d74b97..e74170f 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -24,7 +24,7 @@ public function boot() if (version_compare(app()->version(), '9.0.0', '>=')) { Event::listen(function (MessageSending $event) { (new SymfonyEmbedImages($this->app['config']->get('mail-auto-embed'))) - ->handle($event); + ->beforeSendPerformed($event); }); } else { foreach (Arr::get($this->app['config'], 'mail.mailers', []) as $driver => $mailer) { diff --git a/tests/Traits/InteractsWithMessage.php b/tests/Traits/InteractsWithMessage.php index 30c7775..2c7271a 100755 --- a/tests/Traits/InteractsWithMessage.php +++ b/tests/Traits/InteractsWithMessage.php @@ -63,7 +63,7 @@ protected function handleBeforeSendPerformedEvent($libraryFile, $options) if ($this->isLaravel9()) { $event = new MessageSending($message); (new SymfonyEmbedImages($options)) - ->handle($event); + ->beforeSendPerformed($event); $event->message->getBody(); return $event->message; }