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
Push — master ( c8b447...30f819 )
by Freek
05:34
created

CallWebhookJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 123
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\WebhookServer;
4
5
use GuzzleHttp\Psr7\Response;
6
use Exception;
7
use GuzzleHttp\Client;
8
use Illuminate\Support\Str;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Queue\SerializesModels;
11
use Illuminate\Queue\InteractsWithQueue;
12
use GuzzleHttp\Exception\RequestException;
13
use Illuminate\Contracts\Queue\ShouldQueue;
14
use Illuminate\Foundation\Bus\Dispatchable;
15
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
16
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent;
17
use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent;
18
19
class CallWebhookJob implements ShouldQueue
20
{
21
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
22
23
    public ?string $webhookUrl = null;
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 '?', expecting T_FUNCTION or T_CONST
Loading history...
24
25
    public string $httpVerb;
26
27
    public int $tries;
28
29
    public int $requestTimeout;
30
31
    public string $backoffStrategyClass;
32
33
    public ?string $signerClass = null;
34
35
    public array $headers = [];
36
37
    public bool $verifySsl;
38
39
    /** @var string */
40
    public  $queue;
41
42
    public array $payload = [];
43
44
    public array $meta = [];
45
46
    public array $tags = [];
47
48
    private ?Response $response = null;
49
50
    private ?string $errorType = null;
51
52
    private ?string $errorMessage = null;
53
54
    public function handle()
55
    {
56
        /** @var \GuzzleHttp\Client $client */
57
        $client = app(Client::class);
58
59
        $lastAttempt = $this->attempts() >= $this->tries;
60
61
        try {
62
            $this->response = $client->request($this->httpVerb, $this->webhookUrl, [
63
                'timeout' => $this->requestTimeout,
64
                'body' => json_encode($this->payload),
65
                'verify' => $this->verifySsl,
66
                'headers' => $this->headers,
67
            ]);
68
69
            if (! Str::startsWith($this->response->getStatusCode(), 2)) {
70
                throw new Exception('Webhook call failed');
71
            }
72
73
            $this->dispatchEvent(WebhookCallSucceededEvent::class);
74
75
            return;
76
        } catch (Exception $exception) {
77
            if ($exception instanceof RequestException) {
78
                $this->response = $exception->getResponse();
79
                $this->errorType = get_class($exception);
80
                $this->errorMessage = $exception->getMessage();
81
            }
82
83
            if (! $lastAttempt) {
84
                /** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */
85
                $backoffStrategy = app($this->backoffStrategyClass);
86
87
                $waitInSeconds = $backoffStrategy->waitInSecondsAfterAttempt($this->attempts());
88
89
                $this->release($waitInSeconds);
90
            }
91
92
            $this->dispatchEvent(WebhookCallFailedEvent::class);
93
        }
94
95
        if ($lastAttempt) {
96
            $this->dispatchEvent(FinalWebhookCallFailedEvent::class);
97
98
            $this->delete();
99
        }
100
    }
101
102
    public function tags()
103
    {
104
        return $this->tags;
105
    }
106
107
    public function getResponse()
108
    {
109
        return $this->response;
110
    }
111
112
    private function dispatchEvent(string $eventClass)
113
    {
114
        event(new $eventClass(
115
            $this->httpVerb,
116
            $this->webhookUrl,
117
            $this->payload,
118
            $this->headers,
119
            $this->meta,
120
            $this->tags,
121
            $this->attempts(),
122
            $this->response,
123
            $this->errorType,
124
            $this->errorMessage
125
        ));
126
    }
127
}
128