Skip to content

Commit

Permalink
use event dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Nov 12, 2024
1 parent fe2a1c7 commit af6fac3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/SparkPostApiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +43,11 @@ class SparkPostApiTransport extends AbstractApiTransport
*/
private $apiResult;

/**
* @var EventDispatcherInterface
*/
private $dispatcher = null;

/**
* @param SparkPostApiClient $apiClient
* @param HttpClientInterface|null $client
Expand All @@ -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);
}

Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/SparkPostHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions tests/SparkPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]");

// 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", "[email protected]");

$email->send();
$result = $transport->getApiResult();

$this->assertEquals("[email protected]", $result["email"]);
}

public function testSending(): void
{
$test_to = Environment::getEnv('SPARKPOST_TEST_TO');
Expand Down

0 comments on commit af6fac3

Please sign in to comment.