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

WebhookProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 62
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 12 2
A ensureValidSignature() 0 10 2
A storeWebhook() 0 7 1
A processWebhook() 0 14 2
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