1 | <?php |
||
17 | class Webhook implements \JsonSerializable |
||
18 | { |
||
19 | const STATUS_QUEUED = 'queued'; |
||
20 | const STATUS_DONE = 'done'; |
||
21 | const STATUS_FAILED = 'failed'; |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $id; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $url; |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $status; |
||
34 | |||
35 | /** @var array */ |
||
36 | private $body; |
||
37 | /** |
||
38 | * We can send body as raw json or array form data |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $raw = true; |
||
43 | /** |
||
44 | * @var \DateTime |
||
45 | */ |
||
46 | private $created; |
||
47 | /** |
||
48 | * Time at what webhook was processed to final state OK|FAIL |
||
49 | * |
||
50 | * @var \DateTime|null |
||
51 | */ |
||
52 | private $processed; |
||
53 | /** |
||
54 | * @var \DateTime |
||
55 | */ |
||
56 | private $nextAttempt; |
||
57 | |||
58 | /** @var int */ |
||
59 | private $attempt = 1; |
||
60 | |||
61 | /** @var int */ |
||
62 | private $maxAttempts = 10; |
||
63 | |||
64 | /** @var int */ |
||
65 | private $expectedCode = 200; |
||
66 | |||
67 | /** @var string|null */ |
||
68 | private $expectedContent; |
||
69 | |||
70 | /** @var string|null */ |
||
71 | private $userAgent; |
||
72 | |||
73 | /** @var StrategyInterface|AbstractStrategy */ |
||
74 | private $strategy; |
||
75 | |||
76 | /** @var string|null */ |
||
77 | private $statusDetails; |
||
78 | |||
79 | /** @var array */ |
||
80 | private $metadata = []; |
||
81 | |||
82 | /** @var null|string */ |
||
83 | private $callbackUrl; |
||
84 | |||
85 | /** @var null|string */ |
||
86 | private $reference; |
||
87 | |||
88 | /** |
||
89 | * Webhook constructor. |
||
90 | * |
||
91 | * @param string $url |
||
92 | * @param $body |
||
93 | */ |
||
94 | 10 | public function __construct(string $url, $body) |
|
103 | |||
104 | public function retry() |
||
116 | |||
117 | 10 | private function calculateNextRetry() |
|
125 | |||
126 | /** |
||
127 | * @param \DateTime $nextAttempt |
||
128 | */ |
||
129 | 10 | private function setNextAttempt(\DateTime $nextAttempt) |
|
133 | |||
134 | private function processed() |
||
138 | |||
139 | /** |
||
140 | * If data is raw we will send it as html form |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 11 | public function isRaw(): bool |
|
148 | |||
149 | /** |
||
150 | * @param bool $raw |
||
151 | */ |
||
152 | 1 | public function setRaw(bool $raw) |
|
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getId(): string |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | 11 | public function getUrl(): string |
|
172 | |||
173 | /** |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 11 | public function getBody() |
|
180 | |||
181 | /** |
||
182 | * @return int |
||
183 | */ |
||
184 | 9 | public function getExpectedCode(): int |
|
188 | |||
189 | /** |
||
190 | * @param int $expectedCode |
||
191 | */ |
||
192 | 9 | public function setExpectedCode(int $expectedCode) |
|
196 | |||
197 | /** |
||
198 | * @return mixed |
||
199 | */ |
||
200 | 7 | public function getExpectedContent() |
|
204 | |||
205 | /** |
||
206 | * @param mixed $expectedContent |
||
207 | */ |
||
208 | 9 | public function setExpectedContent($expectedContent) |
|
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | 11 | public function getUserAgent() |
|
220 | |||
221 | /** |
||
222 | * @return \DateTime |
||
223 | */ |
||
224 | public function getProcessed(): \DateTime |
||
228 | |||
229 | /** |
||
230 | * @return int |
||
231 | */ |
||
232 | public function getMaxAttempts(): int |
||
236 | |||
237 | public function done() |
||
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | public function jsonSerialize(): array |
||
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getStatus(): string |
||
267 | |||
268 | /** |
||
269 | * @param string $status |
||
270 | */ |
||
271 | public function setStatus(string $status) |
||
275 | |||
276 | /** |
||
277 | * @return \DateTime |
||
278 | */ |
||
279 | public function getCreated(): \DateTime |
||
283 | |||
284 | /** |
||
285 | * @return int |
||
286 | */ |
||
287 | 11 | public function getAttempt(): int |
|
291 | |||
292 | /** |
||
293 | * @return \DateTime |
||
294 | */ |
||
295 | 11 | public function getNextAttempt(): \DateTime |
|
299 | |||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getStatusDetails(): string |
||
307 | |||
308 | /** |
||
309 | * @param string $statusDetails |
||
310 | */ |
||
311 | public function setStatusDetails(string $statusDetails) |
||
315 | |||
316 | /** |
||
317 | * @return AbstractStrategy|StrategyInterface |
||
318 | */ |
||
319 | public function getStrategy() |
||
323 | |||
324 | /** |
||
325 | * @param StrategyInterface $strategy |
||
326 | */ |
||
327 | 10 | public function setStrategy(StrategyInterface $strategy) |
|
332 | |||
333 | /** |
||
334 | * @return array |
||
335 | */ |
||
336 | public function getMetadata(): array |
||
340 | |||
341 | /** |
||
342 | * @param array $metadata |
||
343 | */ |
||
344 | public function setMetadata(array $metadata) |
||
348 | |||
349 | /** |
||
350 | * @return null|string |
||
351 | */ |
||
352 | public function getCallbackUrl() |
||
356 | |||
357 | /** |
||
358 | * @param null|string $callbackUrl |
||
359 | */ |
||
360 | public function setCallbackUrl($callbackUrl) |
||
364 | |||
365 | /** |
||
366 | * @param int $maxAttempts |
||
367 | */ |
||
368 | public function setMaxAttempts(int $maxAttempts): void |
||
372 | |||
373 | /** |
||
374 | * @return null|string |
||
375 | */ |
||
376 | public function getReference(): ?string |
||
380 | |||
381 | /** |
||
382 | * @param null|string $reference |
||
383 | */ |
||
384 | public function setReference(?string $reference): void |
||
388 | } |