Skip to content

Commit

Permalink
Add SesApiService
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemaire committed Oct 12, 2017
1 parent e8a8549 commit a647517
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
70 changes: 70 additions & 0 deletions ApiService/SesApiService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Swm\Bundle\MailHookBundle\ApiService;

use Swm\Bundle\MailHookBundle\Hook\DefaultHook;
use Swm\Bundle\MailHookBundle\SwmMailHookEvent;

class SesApiService extends BaseApiService
{
private $eventAssoc = [
'permanent_bounce' => SwmMailHookEvent::MAILHOOK_HARDBOUNCE,
'transient_bounce' => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE,
'undetermined_bounce' => SwmMailHookEvent::MAILHOOK_SOFTBOUNCE,
'spam_complaint' => SwmMailHookEvent::MAILHOOK_SPAM,
'delivery' => SwmMailHookEvent::MAILHOOK_SEND,
];

const SNS_BOUNCE = 'Bounce';
const SNS_COMPLAINT = 'Complaint';
const SNS_DELIVERY = 'Delivery';

/**
* @throws \Exception
*
* @return array<HookInterface>
*/
public function bind()
{
$metaData = json_decode($this->request->getContent(), true);

if (isset($params['Type']) && $metaData['Type'] == 'SubscriptionConfirmation') {
throw new \Exception('AWS SNS Require a subscription confirmation: '.$metaData['SubscribeURL']);
}

if (!isset($metaData['Message'])) {
throw new \Exception('Could not find data');
}

$sesMessage = json_decode($metaData['Message'], true);

$hooks = [];

switch ($sesMessage['notificationType']) {
case self::SNS_BOUNCE:
$event = strtolower($sesMessage['bounce']['bounceType']).'_bounce';
foreach ($sesMessage['bounce']['bouncedRecipients'] as $recipient) {
$hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]);
}

break;
case self::SNS_COMPLAINT:
$event = 'spam_complaint';
foreach ($sesMessage['complaint']['complainedRecipients'] as $recipient) {
$hooks[] = new DefaultHook($event, $recipient['emailAddress'], 'ses', $sesMessage, $this->eventAssoc[$event]);
}

break;
case self::SNS_DELIVERY:
$event = 'delivery';
foreach ($sesMessage['delivery']['recipients'] as $recipient) {
$hooks[] = new DefaultHook($event, $recipient, 'ses', $sesMessage, $this->eventAssoc[$event]);
}

break;
default:
}

return $hooks;
}
}
5 changes: 5 additions & 0 deletions Resources/config/apiService.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ services:
class: "%swm.mail_hook.api_service.sparkpost.class%"
tags:
- { name: swm.mailhook, alias: sparkpost }

swm.mail_hook.api_service.ses:
class: "%swm.mail_hook.api_service.ses.class%"
tags:
- { name: swm.mailhook, alias: ses }

0 comments on commit a647517

Please sign in to comment.