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.
Completed
Pull Request — master (#30)
by
unknown
13:33
created

WebhookCall::onConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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