Skip to content

Commit 9e91494

Browse files
committed
Fix event response
1 parent 7c17232 commit 9e91494

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

EventSubscriber/CallbackSubscriber.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44

55
namespace MauticPlugin\SparkpostBundle\EventSubscriber;
66

7+
use Mautic\CoreBundle\Helper\CoreParametersHelper;
78
use Mautic\EmailBundle\EmailEvents;
89
use Mautic\EmailBundle\Event\TransportWebhookEvent;
910
use Mautic\EmailBundle\Model\TransportCallback;
1011
use Mautic\LeadBundle\Entity\DoNotContact;
12+
use MauticPlugin\SparkpostBundle\Mailer\Transport\SparkpostTransport;
1113
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Mailer\Transport\Dsn;
1216

1317
class CallbackSubscriber implements EventSubscriberInterface
1418
{
15-
public function __construct(private TransportCallback $transportCallback)
16-
{
19+
public function __construct(
20+
private TransportCallback $transportCallback,
21+
private CoreParametersHelper $coreParametersHelper
22+
) {
1723
}
1824

1925
/**
@@ -28,41 +34,49 @@ public static function getSubscribedEvents(): array
2834

2935
public function processCallbackRequest(TransportWebhookEvent $event): void
3036
{
37+
$dsn = Dsn::fromString($this->coreParametersHelper->get('mailer_dsn'));
38+
39+
if (SparkpostTransport::MAUTIC_SPARKPOST_API_SCHEME !== $dsn->getScheme()) {
40+
return;
41+
}
42+
3143
$payload = $event->getRequest()->request->all();
3244

3345
foreach ($payload as $msys) {
34-
$msys = $msys['msys'] ?? null;
35-
$event = $msys['message_event'] ?? $msys['unsubscribe_event'] ?? null;
46+
$msys = $msys['msys'] ?? null;
47+
$messageEvent = $msys['message_event'] ?? $msys['unsubscribe_event'] ?? null;
3648

37-
if (!$event) {
49+
if (!$messageEvent) {
3850
continue;
3951
}
4052

41-
if (isset($event['rcpt_type']) && 'to' !== $event['rcpt_type']) {
53+
if (isset($messageEvent['rcpt_type']) && 'to' !== $messageEvent['rcpt_type']) {
4254
// Ignore cc/bcc
4355
continue;
4456
}
4557

46-
$type = $event['type'] ?? null;
47-
$bounceClass = $event['bounce_class'] ?? null;
58+
$type = $messageEvent['type'] ?? null;
59+
$bounceClass = $messageEvent['bounce_class'] ?? null;
4860

4961
if ('bounce' === $type && !in_array((int) $bounceClass, [10, 30, 50, 51, 52, 53, 54, 90])) {
5062
// Only parse hard bounces
5163
// https://support.sparkpost.com/customer/portal/articles/1929896-bounce-classification-codes
5264
continue;
5365
}
5466

55-
$hashId = $event['rcpt_meta']['hashId'] ?? null;
67+
$hashId = $messageEvent['rcpt_meta']['hashId'] ?? null;
5668

5769
if ($hashId) {
58-
$this->processCallbackByHashId($hashId, $event);
70+
$this->processCallbackByHashId($hashId, $messageEvent);
5971

6072
continue;
6173
}
6274

63-
$rcptTo = $event['rcpt_to'] ?? '';
64-
$this->processCallbackByEmailAddress($rcptTo, $event);
75+
$rcptTo = $messageEvent['rcpt_to'] ?? '';
76+
$this->processCallbackByEmailAddress($rcptTo, $messageEvent);
6577
}
78+
79+
$event->setResponse(new Response('Callback processed'));
6680
}
6781

6882
/**

0 commit comments

Comments
 (0)