1 | <?php |
||
2 | |||
3 | namespace Spatie\WebhookServer; |
||
4 | |||
5 | use Exception; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Exception\ConnectException; |
||
8 | use GuzzleHttp\Exception\RequestException; |
||
9 | use GuzzleHttp\Psr7\Response; |
||
10 | use Illuminate\Bus\Queueable; |
||
11 | use Illuminate\Contracts\Queue\ShouldQueue; |
||
12 | use Illuminate\Foundation\Bus\Dispatchable; |
||
13 | use Illuminate\Queue\InteractsWithQueue; |
||
14 | use Illuminate\Queue\SerializesModels; |
||
15 | use Illuminate\Support\Str; |
||
16 | use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent; |
||
17 | use Spatie\WebhookServer\Events\WebhookCallFailedEvent; |
||
18 | use Spatie\WebhookServer\Events\WebhookCallSucceededEvent; |
||
19 | |||
20 | class CallWebhookJob implements ShouldQueue |
||
21 | { |
||
22 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
||
23 | |||
24 | public ?string $webhookUrl = null; |
||
25 | |||
26 | public string $httpVerb; |
||
27 | |||
28 | public int $tries; |
||
29 | |||
30 | public int $requestTimeout; |
||
31 | |||
32 | public string $backoffStrategyClass; |
||
33 | |||
34 | public ?string $signerClass = null; |
||
35 | |||
36 | public array $headers = []; |
||
37 | |||
38 | public bool $verifySsl; |
||
39 | |||
40 | /** @var string */ |
||
41 | public $queue; |
||
42 | |||
43 | public array $payload = []; |
||
44 | |||
45 | public array $meta = []; |
||
46 | |||
47 | public array $tags = []; |
||
48 | |||
49 | public string $uuid = ''; |
||
50 | |||
51 | private ?Response $response = null; |
||
52 | |||
53 | private ?string $errorType = null; |
||
54 | |||
55 | private ?string $errorMessage = null; |
||
56 | |||
57 | public function handle() |
||
58 | { |
||
59 | /** @var \GuzzleHttp\Client $client */ |
||
60 | $client = app(Client::class); |
||
61 | |||
62 | $lastAttempt = $this->attempts() >= $this->tries; |
||
63 | |||
64 | try { |
||
65 | $body = strtoupper($this->httpVerb) === 'GET' |
||
66 | ? ['query' => $this->payload] |
||
67 | : ['body' => json_encode($this->payload)]; |
||
68 | |||
69 | $this->response = $client->request($this->httpVerb, $this->webhookUrl, array_merge([ |
||
70 | 'timeout' => $this->requestTimeout, |
||
71 | 'verify' => $this->verifySsl, |
||
72 | 'headers' => $this->headers, |
||
73 | ], $body)); |
||
74 | |||
75 | if (! Str::startsWith($this->response->getStatusCode(), 2)) { |
||
0 ignored issues
–
show
|
|||
76 | throw new Exception('Webhook call failed'); |
||
77 | } |
||
78 | |||
79 | $this->dispatchEvent(WebhookCallSucceededEvent::class); |
||
80 | |||
81 | return; |
||
82 | } catch (Exception $exception) { |
||
83 | if ($exception instanceof RequestException) { |
||
84 | $this->response = $exception->getResponse(); |
||
85 | $this->errorType = get_class($exception); |
||
86 | $this->errorMessage = $exception->getMessage(); |
||
87 | } |
||
88 | |||
89 | if ($exception instanceof ConnectException) { |
||
90 | $this->errorType = get_class($exception); |
||
91 | $this->errorMessage = $exception->getMessage(); |
||
92 | } |
||
93 | |||
94 | if (! $lastAttempt) { |
||
95 | /** @var \Spatie\WebhookServer\BackoffStrategy\BackoffStrategy $backoffStrategy */ |
||
96 | $backoffStrategy = app($this->backoffStrategyClass); |
||
97 | |||
98 | $waitInSeconds = $backoffStrategy->waitInSecondsAfterAttempt($this->attempts()); |
||
99 | |||
100 | $this->release($waitInSeconds); |
||
101 | } |
||
102 | |||
103 | $this->dispatchEvent(WebhookCallFailedEvent::class); |
||
104 | } |
||
105 | |||
106 | if ($lastAttempt) { |
||
107 | $this->dispatchEvent(FinalWebhookCallFailedEvent::class); |
||
108 | |||
109 | $this->delete(); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public function tags() |
||
114 | { |
||
115 | return $this->tags; |
||
116 | } |
||
117 | |||
118 | public function getResponse() |
||
119 | { |
||
120 | return $this->response; |
||
121 | } |
||
122 | |||
123 | private function dispatchEvent(string $eventClass) |
||
124 | { |
||
125 | event(new $eventClass( |
||
126 | $this->httpVerb, |
||
127 | $this->webhookUrl, |
||
128 | $this->payload, |
||
129 | $this->headers, |
||
130 | $this->meta, |
||
131 | $this->tags, |
||
132 | $this->attempts(), |
||
133 | $this->response, |
||
134 | $this->errorType, |
||
135 | $this->errorMessage, |
||
136 | $this->uuid |
||
137 | )); |
||
138 | } |
||
139 | } |
||
140 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.