1 | <?php |
||
11 | class WebhookProcessor |
||
12 | { |
||
13 | protected Request $request; |
||
|
|||
14 | |||
15 | protected WebhookConfig $config; |
||
16 | |||
17 | public function __construct(Request $request, WebhookConfig $config) |
||
18 | { |
||
19 | $this->request = $request; |
||
20 | |||
21 | $this->config = $config; |
||
22 | } |
||
23 | |||
24 | public function process() |
||
25 | { |
||
26 | $this->ensureValidSignature(); |
||
27 | |||
28 | if (! $this->config->webhookProfile->shouldProcess($this->request)) { |
||
29 | return; |
||
30 | } |
||
31 | |||
32 | $webhookCall = $this->storeWebhook(); |
||
33 | |||
34 | $this->processWebhook($webhookCall); |
||
35 | } |
||
36 | |||
37 | protected function ensureValidSignature() |
||
38 | { |
||
39 | if (! $this->config->signatureValidator->isValid($this->request, $this->config)) { |
||
40 | event(new InvalidSignatureEvent($this->request)); |
||
41 | |||
42 | throw WebhookFailed::invalidSignature(); |
||
43 | } |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | protected function storeWebhook(): WebhookCall |
||
49 | { |
||
50 | return $this->config->webhookModel::storeWebhook($this->config, $this->request); |
||
51 | } |
||
52 | |||
53 | protected function processWebhook(WebhookCall $webhookCall): void |
||
54 | { |
||
55 | try { |
||
56 | $job = new $this->config->processWebhookJob($webhookCall); |
||
57 | |||
58 | $webhookCall->clearException(); |
||
59 | |||
60 | dispatch($job); |
||
61 | } catch (Exception $exception) { |
||
62 | $webhookCall->saveException($exception); |
||
63 | |||
64 | throw $exception; |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 |