1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Webhook\Bundle\Service; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Webhook\Bundle\Event\WebhookEvent; |
9
|
|
|
use Webhook\Bundle\WebhookEvents; |
10
|
|
|
use Webhook\Domain\Infrastructure\HandlerInterface; |
11
|
|
|
use Webhook\Domain\Infrastructure\RequestResult; |
12
|
|
|
use Webhook\Domain\Model\Webhook; |
13
|
|
|
use Webhook\Domain\Repository\WebhookRepositoryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class WebhookConsumer |
17
|
|
|
* |
18
|
|
|
* @package Webhook\Domain\Infrastructure |
19
|
|
|
*/ |
20
|
|
|
final class WebhookConsumer |
21
|
|
|
{ |
22
|
|
|
/** @var HandlerInterface */ |
23
|
|
|
private $handler; |
24
|
|
|
|
25
|
|
|
/** @var WebhookRepositoryInterface */ |
26
|
|
|
private $repository; |
27
|
|
|
/** |
28
|
|
|
* @var EventDispatcherInterface |
29
|
|
|
*/ |
30
|
|
|
private $dispatcher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param HandlerInterface $handler |
34
|
|
|
* @param WebhookRepositoryInterface $repository |
35
|
|
|
* @param EventDispatcherInterface $dispatcher |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
HandlerInterface $handler, |
39
|
|
|
WebhookRepositoryInterface $repository, |
40
|
|
|
EventDispatcherInterface $dispatcher |
41
|
|
|
) { |
42
|
|
|
$this->handler = $handler; |
43
|
|
|
$this->repository = $repository; |
44
|
|
|
$this->dispatcher = $dispatcher; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $id |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function consume($id) |
53
|
|
|
{ |
54
|
|
|
$webhook = $this->repository->get($id); |
55
|
|
|
|
56
|
|
|
if (null === $webhook) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
try { |
60
|
|
|
if ($webhook->getStatus() === Webhook::STATUS_FAILED) { |
61
|
|
|
$this->notifyFail($webhook); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
/** @var RequestResult $r */ |
65
|
|
|
$r = $this->handler->handle($webhook); |
66
|
|
|
$webhook->setStatusDetails($r->getDetails()); |
67
|
|
|
|
68
|
|
|
if (!$r->isSuccess()) { |
69
|
|
|
$webhook->retry(); |
70
|
|
|
$this->notifyRetry($webhook); |
71
|
|
|
} else { |
72
|
|
|
$webhook->done(); |
73
|
|
|
$this->notifyDone($webhook); |
74
|
|
|
} |
75
|
|
|
} catch (\Throwable $exception) { |
76
|
|
|
$webhook->setStatus(Webhook::STATUS_FAILED); |
77
|
|
|
$webhook->setStatusDetails($exception->getMessage()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->repository->update($webhook); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Webhook $webhook |
85
|
|
|
*/ |
86
|
|
|
private function notifyFail(Webhook $webhook) |
87
|
|
|
{ |
88
|
|
|
$this->notify(WebhookEvents::WEBHOOK_FAIL, $webhook); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $event |
93
|
|
|
* @param Webhook $webhook |
94
|
|
|
*/ |
95
|
|
|
private function notify(string $event, Webhook $webhook) |
96
|
|
|
{ |
97
|
|
|
$this->dispatcher->dispatch($event, new WebhookEvent($webhook)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Webhook $webhook |
102
|
|
|
*/ |
103
|
|
|
private function notifyRetry(Webhook $webhook) |
104
|
|
|
{ |
105
|
|
|
$this->notify(WebhookEvents::WEBHOOK_RETRY, $webhook); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Webhook $webhook |
110
|
|
|
*/ |
111
|
|
|
private function notifyDone(Webhook $webhook) |
112
|
|
|
{ |
113
|
|
|
$this->notify(WebhookEvents::WEBHOOK_DONE, $webhook); |
114
|
|
|
} |
115
|
|
|
} |