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 Spatie\WebhookServer\Signer\Signer; |
||
6 | use Spatie\WebhookServer\Exceptions\InvalidSigner; |
||
7 | use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook; |
||
8 | use Spatie\WebhookServer\BackoffStrategy\BackoffStrategy; |
||
9 | use Spatie\WebhookServer\Exceptions\InvalidBackoffStrategy; |
||
10 | |||
11 | class WebhookCall |
||
12 | { |
||
13 | protected CallWebhookJob $callWebhookJob; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
14 | |||
15 | protected string $secret; |
||
16 | |||
17 | protected Signer $signer; |
||
18 | |||
19 | protected array $headers = []; |
||
20 | |||
21 | private array $payload = []; |
||
22 | |||
23 | public static function create(): self |
||
24 | { |
||
25 | $config = config('webhook-server'); |
||
26 | |||
27 | return (new static()) |
||
28 | ->onQueue($config['queue']) |
||
29 | ->useHttpVerb($config['http_verb']) |
||
30 | ->maximumTries($config['tries']) |
||
31 | ->useBackoffStrategy($config['backoff_strategy']) |
||
32 | ->timeoutInSeconds($config['timeout_in_seconds']) |
||
33 | ->signUsing($config['signer']) |
||
34 | ->withHeaders($config['headers']) |
||
35 | ->withTags($config['tags']) |
||
36 | ->verifySsl($config['verify_ssl']); |
||
37 | } |
||
38 | |||
39 | public function __construct() |
||
40 | { |
||
41 | $this->callWebhookJob = app(CallWebhookJob::class); |
||
42 | } |
||
43 | |||
44 | public function url(string $url) |
||
45 | { |
||
46 | $this->callWebhookJob->webhookUrl = $url; |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | public function payload(array $payload) |
||
52 | { |
||
53 | $this->payload = $payload; |
||
54 | |||
55 | $this->callWebhookJob->payload = $payload; |
||
56 | |||
57 | return $this; |
||
58 | } |
||
59 | |||
60 | public function onQueue(string $queue) |
||
61 | { |
||
62 | $this->callWebhookJob->queue = $queue; |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | public function useSecret(string $secret) |
||
68 | { |
||
69 | $this->secret = $secret; |
||
70 | |||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | public function useHttpVerb(string $verb) |
||
75 | { |
||
76 | $this->callWebhookJob->httpVerb = $verb; |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | public function maximumTries(int $tries) |
||
82 | { |
||
83 | $this->callWebhookJob->tries = $tries; |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | public function useBackoffStrategy(string $backoffStrategyClass) |
||
89 | { |
||
90 | if (! is_subclass_of($backoffStrategyClass, BackoffStrategy::class)) { |
||
91 | throw InvalidBackoffStrategy::doesNotExtendBackoffStrategy($backoffStrategyClass); |
||
92 | } |
||
93 | |||
94 | $this->callWebhookJob->backoffStrategyClass = $backoffStrategyClass; |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | public function timeoutInSeconds(int $timeoutInSeconds) |
||
100 | { |
||
101 | $this->callWebhookJob->requestTimeout = $timeoutInSeconds; |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | public function signUsing(string $signerClass) |
||
107 | { |
||
108 | if (! is_subclass_of($signerClass, Signer::class)) { |
||
109 | throw InvalidSigner::doesImplementSigner($signerClass); |
||
110 | } |
||
111 | |||
112 | $this->signer = app($signerClass); |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | public function withHeaders(array $headers) |
||
118 | { |
||
119 | $this->headers = $headers; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | public function verifySsl(bool $verifySsl = true) |
||
125 | { |
||
126 | $this->callWebhookJob->verifySsl = $verifySsl; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | public function doNotVerifySsl() |
||
132 | { |
||
133 | $this->verifySsl(false); |
||
134 | |||
135 | return $this; |
||
136 | } |
||
137 | |||
138 | public function meta(array $meta) |
||
139 | { |
||
140 | $this->callWebhookJob->meta = $meta; |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | public function withTags(array $tags) |
||
146 | { |
||
147 | $this->callWebhookJob->tags = $tags; |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | public function dispatch(): void |
||
153 | { |
||
154 | if (! $this->callWebhookJob->webhookUrl) { |
||
155 | throw CouldNotCallWebhook::urlNotSet(); |
||
156 | } |
||
157 | |||
158 | if (empty($this->secret)) { |
||
159 | throw CouldNotCallWebhook::secretNotSet(); |
||
160 | } |
||
161 | |||
162 | $this->callWebhookJob->headers = $this->getAllHeaders(); |
||
163 | |||
164 | dispatch($this->callWebhookJob); |
||
165 | } |
||
166 | |||
167 | protected function getAllHeaders(): array |
||
168 | { |
||
169 | $headers = $this->headers; |
||
170 | |||
171 | $signature = $this->signer->calculateSignature($this->payload, $this->secret); |
||
172 | |||
173 | $headers[$this->signer->signatureHeaderName()] = $signature; |
||
174 | |||
175 | return $headers; |
||
176 | } |
||
177 | } |
||
178 |