Completed
Push — master ( f34bd5...f2e2e5 )
by Freek
01:17
created

WebhookProcessor::ensureValidSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\WebhookClient;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
use Spatie\WebhookClient\Models\WebhookCall;
8
use Spatie\WebhookClient\Exceptions\WebhookFailed;
9
use Spatie\WebhookClient\Events\InvalidSignatureEvent;
10
11
class WebhookProcessor
12
{
13
    /** @var \Illuminate\Http\Request */
14
    protected $request;
15
16
    /** @var \Spatie\WebhookClient\WebhookConfig */
17
    protected $config;
18
19
    public function __construct(Request $request, WebhookConfig $config)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
20
    {
21
        $this->request = $request;
22
23
        $this->config = $config;
24
    }
25
26
    public function process()
27
    {
28
        $this->ensureValidSignature();
29
30
        if (! $this->config->webhookProfile->shouldProcess($this->request)) {
31
            return;
32
        }
33
34
        $webhookCall = $this->storeWebhook();
35
36
        $this->processWebhook($webhookCall);
37
    }
38
39
    protected function ensureValidSignature()
40
    {
41
        if (! $this->config->signatureValidator->isValid($this->request, $this->config)) {
42
            event(new InvalidSignatureEvent($this->request));
43
44
            throw WebhookFailed::invalidSignature();
45
        }
46
47
        return $this;
48
    }
49
50
    protected function storeWebhook(): WebhookCall
51
    {
52
        return $this->config->webhookModel::create([
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->config->webhookModel (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
53
            'name' => $this->config->name,
54
            'payload' => $this->request->input(),
55
        ]);
56
    }
57
58
    protected function processWebhook(WebhookCall $webhookCall): void
59
    {
60
        try {
61
            $job = new $this->config->processWebhookJob($webhookCall);
62
63
            $webhookCall->clearException();
64
65
            dispatch($job);
66
        } catch (Exception $exception) {
67
            $webhookCall->saveException($exception);
68
69
            throw $exception;
70
        }
71
    }
72
}
73