Skip to content

Commit

Permalink
standardizing the beforeSendPerformed method and making the handle me…
Browse files Browse the repository at this point in the history
…thod public to use manually
  • Loading branch information
eduardokum committed Nov 28, 2023
1 parent 86bac31 commit 8eba77a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/Listeners/SwiftEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -23,7 +25,7 @@ class SwiftEmbedImages implements Swift_Events_SendListener
private $config;

/**
* @var Swift_Message
* @var Email|Swift_Mime_SimpleMessage
*/
private $message;

Expand All @@ -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();
}

Expand Down Expand Up @@ -215,4 +225,4 @@ private function embed(Embedder $embedder, $src)

return $src;
}
}
}
15 changes: 12 additions & 3 deletions src/Listeners/SymfonyEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SymfonyEmbedImages
private $config;

/**
* @var Email|Swift_Message
* @var Email
*/
private $message;

Expand All @@ -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();
}

Expand Down Expand Up @@ -195,3 +203,4 @@ private function embed(Embedder $embedder, $src)
return $src;
}
}

2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Traits/InteractsWithMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8eba77a

Please sign in to comment.