From af6fac35265cafce7f3bc3c340ae20421ee01cce Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 12 Nov 2024 15:01:12 +0100 Subject: [PATCH] use event dispatcher --- src/SparkPostApiTransport.php | 22 ++++++++++++++++++++++ src/SparkPostHelper.php | 10 +++++++++- tests/SparkPostTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/SparkPostApiTransport.php b/src/SparkPostApiTransport.php index 723e705..5188af7 100644 --- a/src/SparkPostApiTransport.php +++ b/src/SparkPostApiTransport.php @@ -18,6 +18,7 @@ use Symfony\Component\HttpClient\Response\MockResponse; use Symfony\Component\Mailer\Transport\AbstractApiTransport; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Mailer\Event\MessageEvent; /** * We create our own class @@ -42,6 +43,11 @@ class SparkPostApiTransport extends AbstractApiTransport */ private $apiResult; + /** + * @var EventDispatcherInterface + */ + private $dispatcher = null; + /** * @param SparkPostApiClient $apiClient * @param HttpClientInterface|null $client @@ -58,6 +64,8 @@ public function __construct(SparkPostApiClient $apiClient, HttpClientInterface $ $this->setHost(self::HOST); } + // We need our own reference + $this->dispatcher = $dispatcher; parent::__construct($client, $dispatcher, $logger); } @@ -66,8 +74,22 @@ public function __toString(): string return sprintf('sparkpost+api://%s', $this->getEndpoint()); } + private function dispatchEvent(Email $email, Envelope $envelope = null): void + { + if (!$this->dispatcher) { + return; + } + $sender = $email->getSender()[0] ?? $email->getFrom()[0] ?? null; + $recipients = $email->getTo(); + $envelope ??= new Envelope($sender, $recipients); + $event = new MessageEvent($email, $envelope, $this); + $this->dispatcher->dispatch($event); + } + protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface { + $this->dispatchEvent($email, $envelope); + $disableSending = $email->getHeaders()->has('X-SendingDisabled') || !SparkPostHelper::getSendingEnabled(); // We don't really care about the actual response diff --git a/src/SparkPostHelper.php b/src/SparkPostHelper.php index 2727868..8748f8a 100644 --- a/src/SparkPostHelper.php +++ b/src/SparkPostHelper.php @@ -15,6 +15,7 @@ use LeKoala\SparkPost\Api\SparkPostApiClient; use Symfony\Component\Mailer\MailerInterface; use SilverStripe\Core\Injector\InjectorNotFoundException; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * This configurable class helps decoupling the api client from SilverStripe @@ -259,7 +260,14 @@ public static function getWebhookPassword() public static function registerTransport() { $client = self::getClient(); - $transport = new SparkPostApiTransport($client); + // Make sure MailerSubscriber is registered + try { + $dispatcher = Injector::inst()->get(EventDispatcherInterface::class . '.mailer'); + } catch (Exception $e) { + // It may not be set + $dispatcher = null; + } + $transport = new SparkPostApiTransport($client, null, $dispatcher); $mailer = new Mailer($transport); Injector::inst()->registerService($mailer, MailerInterface::class); return $mailer; diff --git a/tests/SparkPostTest.php b/tests/SparkPostTest.php index b5afe55..94d763d 100644 --- a/tests/SparkPostTest.php +++ b/tests/SparkPostTest.php @@ -86,6 +86,35 @@ public function testTLSVersion(): void } } + public function testSendAllTo(): void + { + $sendAllTo = Environment::getEnv('SS_SEND_ALL_EMAILS_TO'); + + $mailer = SparkPostHelper::registerTransport(); + + $email = new Email(); + $email->setSubject('Test email'); + $email->setBody("Body of my email"); + $email->getHeaders()->addTextHeader('X-SendingDisabled', "true"); + $email->setTo("sendfrom@test.local"); + + // This is async, therefore it does not return anything anymore + $email->send(); + + /** @var \LeKoala\SparkPost\SparkPostApiTransport $transport */ + $transport = SparkPostHelper::getTransportFromMailer($mailer); + $result = $transport->getApiResult(); + + $this->assertEquals($sendAllTo, $result["email"]); + + Environment::setEnv("SS_SEND_ALL_EMAILS_TO", "sendall@test.local"); + + $email->send(); + $result = $transport->getApiResult(); + + $this->assertEquals("sendall@test.local", $result["email"]); + } + public function testSending(): void { $test_to = Environment::getEnv('SPARKPOST_TEST_TO');