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