|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Mailer\Bridge\Mailjet\RemoteEvent; |
| 13 | + |
| 14 | +use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent; |
| 15 | +use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent; |
| 16 | +use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent; |
| 17 | +use Symfony\Component\RemoteEvent\Exception\ParseException; |
| 18 | +use Symfony\Component\RemoteEvent\PayloadConverterInterface; |
| 19 | + |
| 20 | +final class MailjetPayloadConverter implements PayloadConverterInterface |
| 21 | +{ |
| 22 | + public function convert(array $payload): AbstractMailerEvent |
| 23 | + { |
| 24 | + if (\in_array($payload['event'], ['bounce', 'sent', 'blocked'], true)) { |
| 25 | + $name = match ($payload['event']) { |
| 26 | + 'bounce' => MailerDeliveryEvent::BOUNCE, |
| 27 | + 'sent' => MailerDeliveryEvent::DELIVERED, |
| 28 | + 'blocked' => MailerDeliveryEvent::DROPPED, |
| 29 | + }; |
| 30 | + |
| 31 | + $event = new MailerDeliveryEvent($name, $payload['MessageID'], $payload); |
| 32 | + $event->setReason($this->getReason($payload)); |
| 33 | + } else { |
| 34 | + $name = match ($payload['event']) { |
| 35 | + 'click' => MailerEngagementEvent::CLICK, |
| 36 | + 'open' => MailerEngagementEvent::OPEN, |
| 37 | + 'spam' => MailerEngagementEvent::SPAM, |
| 38 | + 'unsub' => MailerEngagementEvent::UNSUBSCRIBE, |
| 39 | + default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['event'])), |
| 40 | + }; |
| 41 | + $event = new MailerEngagementEvent($name, $payload['MessageID'], $payload); |
| 42 | + } |
| 43 | + |
| 44 | + if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['time'])) { |
| 45 | + throw new ParseException(sprintf('Invalid date "%s".', $payload['time'])); |
| 46 | + } |
| 47 | + |
| 48 | + $event->setDate($date); |
| 49 | + $event->setRecipientEmail($payload['email']); |
| 50 | + |
| 51 | + if (isset($payload['CustomID'])) { |
| 52 | + $event->setTags([$payload['CustomID']]); |
| 53 | + } |
| 54 | + |
| 55 | + if (isset($payload['Payload'])) { |
| 56 | + $event->setMetadata(['Payload' => $payload['Payload']]); |
| 57 | + } |
| 58 | + |
| 59 | + return $event; |
| 60 | + } |
| 61 | + |
| 62 | + private function getReason(array $payload): string |
| 63 | + { |
| 64 | + return $payload['smtp_reply'] ?? $payload['error_related_to'] ?? ''; |
| 65 | + } |
| 66 | +} |
0 commit comments