1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swm\Bundle\MailHookBundle\ApiService; |
4
|
|
|
|
5
|
|
|
use Swm\Bundle\MailHookBundle\Hook\DefaultHook; |
6
|
|
|
use Swm\Bundle\MailHookBundle\SwmMailHookEvent; |
7
|
|
|
|
8
|
|
|
class SesApiService extends BaseApiService |
9
|
|
|
{ |
10
|
|
|
private $eventAssoc = [ |
11
|
|
|
'permanent_bounce' => SwmMailHookEvent::MAILHOOK_HARDBOUNCE, |
12
|
|
|
'transient_bounce' => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE, |
13
|
|
|
'undetermined_bounce' => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE, |
14
|
|
|
'spam_complaint' => SwmMailHookEvent::MAILHOOK_SPAM, |
15
|
|
|
'delivery' => SwmMailHookEvent::MAILHOOK_SEND, |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
const SNS_BOUNCE = 'Bounce'; |
19
|
|
|
const SNS_COMPLAINT = 'Complaint'; |
20
|
|
|
const SNS_DELIVERY = 'Delivery'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @throws \Exception |
24
|
|
|
* |
25
|
|
|
* @return array<HookInterface> |
26
|
|
|
*/ |
27
|
|
|
public function bind() |
28
|
|
|
{ |
29
|
|
|
$metaData = json_decode($this->request->getContent(), true); |
30
|
|
|
|
31
|
|
|
if (isset($metaData['Type']) && $metaData['Type'] == 'SubscriptionConfirmation') { |
32
|
|
|
throw new \Exception('AWS SNS Require a subscription confirmation: '.$metaData['SubscribeURL']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!isset($metaData['Message'])) { |
36
|
|
|
throw new \Exception('Could not find data'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$sesMessage = json_decode($metaData['Message'], true); |
40
|
|
|
|
41
|
|
|
$hooks = []; |
42
|
|
|
|
43
|
|
|
switch ($sesMessage['notificationType']) { |
44
|
|
|
case self::SNS_BOUNCE: |
45
|
|
|
$event = strtolower($sesMessage['bounce']['bounceType']).'_bounce'; |
46
|
|
View Code Duplication |
foreach ($sesMessage['bounce']['bouncedRecipients'] as $recipient) { |
|
|
|
|
47
|
|
|
$hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
break; |
51
|
|
|
case self::SNS_COMPLAINT: |
52
|
|
|
$event = 'spam_complaint'; |
53
|
|
View Code Duplication |
foreach ($sesMessage['complaint']['complainedRecipients'] as $recipient) { |
|
|
|
|
54
|
|
|
$hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
break; |
58
|
|
|
case self::SNS_DELIVERY: |
59
|
|
|
$event = 'delivery'; |
60
|
|
|
foreach ($sesMessage['delivery']['recipients'] as $recipient) { |
61
|
|
|
$hooks[] = new DefaultHook($event, $recipient, 'ses', $sesMessage, $this->eventAssoc[$event]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
break; |
65
|
|
|
default: |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $hooks; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.