GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WebhookCall::prepareForDispatch()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Spatie\WebhookServer;
4
5
use Illuminate\Support\Str;
6
use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy;
7
use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
8
use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy;
9
use Spatie\WebhookServer\Exceptions\InvalidSigner;
10
use Spatie\WebhookServer\Signer\Signer;
11
12
class WebhookCall
13
{
14
    protected CallWebhookJob $callWebhookJob;
15
16
    protected string $uuid = '';
17
18
    protected string $secret;
19
20
    protected Signer $signer;
21
22
    protected array $headers = [];
23
24
    private array $payload = [];
25
26
    private $signWebhook = true;
27
28
    public static function create(): self
29
    {
30
        $config = config('webhook-server');
31
32
        return (new static())
33
            ->uuid(Str::uuid())
34
            ->onQueue($config['queue'])
35
            ->useHttpVerb($config['http_verb'])
36
            ->maximumTries($config['tries'])
37
            ->useBackoffStrategy($config['backoff_strategy'])
38
            ->timeoutInSeconds($config['timeout_in_seconds'])
39
            ->signUsing($config['signer'])
40
            ->withHeaders($config['headers'])
41
            ->withTags($config['tags'])
42
            ->verifySsl($config['verify_ssl']);
43
    }
44
45
    public function __construct()
46
    {
47
        $this->callWebhookJob = app(CallWebhookJob::class);
48
    }
49
50
    public function url(string $url): self
51
    {
52
        $this->callWebhookJob->webhookUrl = $url;
53
54
        return $this;
55
    }
56
57
    public function payload(array $payload): self
58
    {
59
        $this->payload = $payload;
60
61
        $this->callWebhookJob->payload = $payload;
62
63
        return $this;
64
    }
65
66
    public function uuid(string $uuid): self
67
    {
68
        $this->uuid = $uuid;
69
70
        $this->callWebhookJob->uuid = $uuid;
71
72
        return $this;
73
    }
74
75
    public function getUuid(): string
76
    {
77
        return $this->uuid;
78
    }
79
80
    public function onQueue(string $queue): self
81
    {
82
        $this->callWebhookJob->queue = $queue;
83
84
        return $this;
85
    }
86
87
    public function useSecret(string $secret): self
88
    {
89
        $this->secret = $secret;
90
91
        return $this;
92
    }
93
94
    public function useHttpVerb(string $verb): self
95
    {
96
        $this->callWebhookJob->httpVerb = $verb;
97
98
        return $this;
99
    }
100
101
    public function maximumTries(int $tries): self
102
    {
103
        $this->callWebhookJob->tries = $tries;
104
105
        return $this;
106
    }
107
108
    public function useBackoffStrategy(string $backoffStrategyClass): self
109
    {
110
        if (! is_subclass_of($backoffStrategyClass, BackoffStrategy::class)) {
111
            throw InvalidBackoffStrategy::doesNotExtendBackoffStrategy($backoffStrategyClass);
112
        }
113
114
        $this->callWebhookJob->backoffStrategyClass = $backoffStrategyClass;
115
116
        return $this;
117
    }
118
119
    public function timeoutInSeconds(int $timeoutInSeconds): self
120
    {
121
        $this->callWebhookJob->requestTimeout = $timeoutInSeconds;
122
123
        return $this;
124
    }
125
126
    public function signUsing(string $signerClass): self
127
    {
128
        if (! is_subclass_of($signerClass, Signer::class)) {
129
            throw InvalidSigner::doesImplementSigner($signerClass);
130
        }
131
132
        $this->signer = app($signerClass);
133
134
        return $this;
135
    }
136
137
    public function doNotSign(): self
138
    {
139
        $this->signWebhook = false;
140
141
        return $this;
142
    }
143
144
    public function withHeaders(array $headers): self
145
    {
146
        $this->headers = $headers;
147
148
        return $this;
149
    }
150
151
    public function verifySsl(bool $verifySsl = true): self
152
    {
153
        $this->callWebhookJob->verifySsl = $verifySsl;
154
155
        return $this;
156
    }
157
158
    public function doNotVerifySsl(): self
159
    {
160
        $this->verifySsl(false);
161
162
        return $this;
163
    }
164
165
    public function meta(array $meta): self
166
    {
167
        $this->callWebhookJob->meta = $meta;
168
169
        return $this;
170
    }
171
172
    public function withTags(array $tags): self
173
    {
174
        $this->callWebhookJob->tags = $tags;
175
176
        return $this;
177
    }
178
179
    public function dispatch(): void
180
    {
181
        $this->prepareForDispatch();
182
183
        dispatch($this->callWebhookJob);
184
    }
185
186
    public function dispatchNow(): void
187
    {
188
        $this->prepareForDispatch();
189
190
        dispatch_now($this->callWebhookJob);
191
    }
192
193
    protected function prepareForDispatch(): void
194
    {
195
        if (! $this->callWebhookJob->webhookUrl) {
196
            throw CouldNotCallWebhook::urlNotSet();
197
        }
198
199
        if ($this->signWebhook && empty($this->secret)) {
200
            throw CouldNotCallWebhook::secretNotSet();
201
        }
202
203
        $this->callWebhookJob->headers = $this->getAllHeaders();
204
    }
205
206
    protected function getAllHeaders(): array
207
    {
208
        $headers = $this->headers;
209
210
        if (! $this->signWebhook) {
211
            return $headers;
212
        }
213
214
        $signature = $this->signer->calculateSignature($this->payload, $this->secret);
215
216
        $headers[$this->signer->signatureHeaderName()] = $signature;
217
218
        return $headers;
219
    }
220
}
221