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