forked from remp2020/remp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailgunEventHandler.php
53 lines (43 loc) · 1.51 KB
/
MailgunEventHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Remp\MailerModule\Hermes;
use Nette\Utils\DateTime;
use Remp\MailerModule\Repository\LogsRepository;
use Tomaj\Hermes\Handler\HandlerInterface;
use Tomaj\Hermes\MessageInterface;
class MailgunEventHandler implements HandlerInterface
{
private $logsRepository;
public function __construct(
LogsRepository $logsRepository
) {
$this->logsRepository = $logsRepository;
}
public function handle(MessageInterface $message): bool
{
$payload = $message->getPayload();
if (!isset($payload['mail_sender_id'])) {
throw new HermesException('unable to handle event: mail_sender_id is missing');
}
if (!isset($payload['timestamp'])) {
throw new HermesException('unable to handle event: timestamp is missing');
}
if (!isset($payload['event'])) {
throw new HermesException('unable to handle event: event is missing');
}
$log = $this->logsRepository->findBySenderId($payload['mail_sender_id']);
if (!$log) {
return false;
}
$eventTimestamp = explode('.', $payload['timestamp'])[0];
$date = DateTime::from($eventTimestamp);
$mailgunEvent = $this->logsRepository->mapEvent($payload['event'], $payload['reason']);
if (!$mailgunEvent) {
return false;
}
$this->logsRepository->update($log, [
$mailgunEvent => $date,
'updated_at' => new DateTime(),
]);
return true;
}
}