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
|
|
|
/** @var string */ |
62
|
|
|
private $errorType; |
63
|
|
|
|
64
|
|
|
/** @var string */ |
65
|
|
|
private $errorMessage; |
66
|
|
|
|
67
|
|
|
public function handle() |
68
|
|
|
{ |
69
|
|
|
/** @var \GuzzleHttp\Client $client */ |
70
|
|
|
$client = app(Client::class); |
71
|
|
|
|
72
|
|
|
$lastAttempt = $this->attempts() >= $this->tries; |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$this->response = $client->request($this->httpVerb, $this->webhookUrl, [ |
|
|
|
|
76
|
|
|
'timeout' => $this->requestTimeout, |
77
|
|
|
'body' => json_encode($this->payload), |
78
|
|
|
'verify' => $this->verifySsl, |
79
|
|
|
'headers' => $this->headers, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
if (! Str::startsWith($this->response->getStatusCode(), 2)) { |
83
|
|
|
throw new Exception('Webhook call failed'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->dispatchEvent(WebhookCallSucceededEvent::class); |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} catch (Exception $exception) { |
90
|
|
|
if ($exception instanceof RequestException) { |
91
|
|
|
$this->response = $exception->getResponse(); |
|
|
|
|
92
|
|
|
$this->errorType = get_class($exception); |
93
|
|
|
$this->errorMessage = $exception->getMessage(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (! $lastAttempt) { |
97
|
|
|
/** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */ |
98
|
|
|
$backoffStrategy = app($this->backoffStrategyClass); |
99
|
|
|
|
100
|
|
|
$waitInSeconds = $backoffStrategy->waitInSecondsAfterAttempt($this->attempts()); |
101
|
|
|
|
102
|
|
|
$this->release($waitInSeconds); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->dispatchEvent(WebhookCallFailedEvent::class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($lastAttempt) { |
109
|
|
|
$this->dispatchEvent(FinalWebhookCallFailedEvent::class); |
110
|
|
|
|
111
|
|
|
$this->delete(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function tags() |
116
|
|
|
{ |
117
|
|
|
return $this->tags; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getResponse() |
121
|
|
|
{ |
122
|
|
|
return $this->response; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function dispatchEvent(string $eventClass) |
126
|
|
|
{ |
127
|
|
|
event(new $eventClass( |
128
|
|
|
$this->httpVerb, |
129
|
|
|
$this->webhookUrl, |
130
|
|
|
$this->payload, |
131
|
|
|
$this->headers, |
132
|
|
|
$this->meta, |
133
|
|
|
$this->tags, |
134
|
|
|
$this->attempts(), |
135
|
|
|
$this->response, |
136
|
|
|
$this->errorType, |
137
|
|
|
$this->errorMessage |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..