Created
July 31, 2017 12:05
-
-
Save alister/c63afdaa3f2de7ffd50be34a315c9579 to your computer and use it in GitHub Desktop.
Send message to a Slack channel (via another internal class) on MailgunEvent events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Event; | |
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent; | |
use Azine\MailgunWebhooksBundle\Entity\MailgunWebhookEvent; | |
use Ca\ProfileBundle\Services\SlackMessageable; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\EventDispatcher\Event; | |
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
class MailgunWebhookSubscriber implements EventSubscriberInterface | |
{ | |
const DATE_FMT = "Y-m-d\TH:i:s"; | |
/** @var SlackMessageable `$msg->send('text..')` a message to our slack channel */ | |
private $msg; | |
/** @var LoggerInterface Application log */ | |
private $log; | |
/** | |
* @param SlackMessageable $msg | |
* @param LoggerInterface $log ordinary logger, to file | |
*/ | |
public function __construct(SlackMessageable $msg, LoggerInterface $log) | |
{ | |
$this->msg = $msg; | |
$this->log = $log; | |
} | |
/** | |
* https://github.com/azine/AzineMailgunWebhooksBundle currently only has CREATE_EVENT | |
* | |
* The potential https://documentation.mailgun.com/api-events.html#event-types are | |
* not currently broken down into further Symfony events. | |
* | |
* @return array MailgunEvent's to handle (currently just ::CREATE_EVENT) | |
*/ | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
MailgunEvent::CREATE_EVENT => array('genericMailgunEvent'), | |
); | |
} | |
public function sendSlackMsg($text) | |
{ | |
if ($text) { | |
return $this->msg->send($text); | |
} | |
@trigger_error("MailgunWebhookSubscriber::sendSlackMsg with empty message", E_USER_NOTICE); | |
return ''; | |
} | |
/** | |
* Space to check on system messages,and not log them to Slack. | |
* | |
* @param MailgunEvent $event [description] | |
* | |
* @return boolean Should this message be ignored, and not sent to Slack? | |
*/ | |
public function isIgnoreable(MailgunEvent $event) | |
{ | |
return false; | |
} | |
public function genericMailgunEvent(MailgunWebhookEvent $mailGunEvent) | |
{ | |
$event = $mailGunEvent->getMailgunEvent(); | |
$eventName = $event->getEvent(); | |
$email = $event->getRecipient(); | |
$occurredAt = $event->getDateTime()->format(self::DATE_FMT); | |
$subject = $event->getEventTitle(); | |
if ($this->isIgnoreable($event)) { | |
$this->log->info("Ignore Mailgun event '{$email}' event:'$eventName' at {$occurredAt}, subject:'{$subject}'"); | |
return; | |
} | |
// todo - get msg from a function | |
$msg = "Mailgun:{$email} *$eventName* at {$occurredAt}, subject:'$subject'"; | |
switch ($eventName) { | |
case 'accepted': | |
case 'clicked': | |
case 'delivered': | |
case 'opened': | |
// swallow, no message displayed to slack. Might log to the file though. | |
$this->log->info($msg); | |
return; | |
break; | |
case 'rejected': | |
$msg = "{$msg}. Mailgun rejected the request to send/forward the email."; | |
break; | |
case 'failed': | |
case 'dropped': | |
$msg = "{$msg}. Mailgun could not deliver the email to the recipient email server."; | |
break; | |
case 'unsubscribed': | |
$msg = "{$msg}. The email recipient clicked on the unsubscribe link."; | |
break; | |
case 'complained': | |
$msg = "{$msg}. The email recipient clicked on the spam complaint button within their email client."; | |
break; | |
case 'stored': | |
$msg = "{$msg}. Mailgun has stored an incoming message"; | |
break; | |
default: | |
$this->log->warning( | |
"Mailgun: Unknown event:*{$eventName}* at {$occurredAt}. subject:'{$subject}' to '{$email}'" | |
); | |
break; | |
} | |
return $this->sendSlackMsg($msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment