Completed
Push — master ( ce492c...ed54c9 )
by Freek
03:04 queued 11s
created

WebhookProcessor::processWebhook()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 4
nop 1
1
<?php
2
3
namespace Spatie\WebhookClient;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
8
use Spatie\WebhookClient\Exceptions\WebhookFailed;
9
use Spatie\WebhookClient\Models\WebhookCall;
10
11
class WebhookProcessor
12
{
13
    protected Request $request;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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