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 | /** |
||
86 | * Webhook constructor. |
||
87 | * |
||
88 | * @param string $url |
||
89 | * @param $body |
||
90 | */ |
||
91 | 10 | public function __construct(string $url, $body) |
|
100 | |||
101 | public function retry() |
||
113 | |||
114 | 10 | private function calculateNextRetry() |
|
122 | |||
123 | /** |
||
124 | * @param \DateTime $nextAttempt |
||
125 | */ |
||
126 | 10 | private function setNextAttempt(\DateTime $nextAttempt) |
|
130 | |||
131 | private function processed() |
||
135 | |||
136 | /** |
||
137 | * If data is raw we will send it as html form |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | 11 | public function isRaw(): bool |
|
145 | |||
146 | /** |
||
147 | * @param bool $raw |
||
148 | */ |
||
149 | 1 | public function setRaw(bool $raw) |
|
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getId(): string |
||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | 11 | public function getUrl(): string |
|
169 | |||
170 | /** |
||
171 | * @return mixed |
||
172 | */ |
||
173 | 11 | public function getBody() |
|
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | */ |
||
181 | 9 | public function getExpectedCode(): int |
|
185 | |||
186 | /** |
||
187 | * @param int $expectedCode |
||
188 | */ |
||
189 | 9 | public function setExpectedCode(int $expectedCode) |
|
193 | |||
194 | /** |
||
195 | * @return mixed |
||
196 | */ |
||
197 | 7 | public function getExpectedContent() |
|
201 | |||
202 | /** |
||
203 | * @param mixed $expectedContent |
||
204 | */ |
||
205 | 9 | public function setExpectedContent($expectedContent) |
|
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | */ |
||
213 | 11 | public function getUserAgent() |
|
217 | |||
218 | /** |
||
219 | * @return \DateTime |
||
220 | */ |
||
221 | public function getProcessed(): \DateTime |
||
225 | |||
226 | /** |
||
227 | * @return int |
||
228 | */ |
||
229 | public function getMaxAttempts(): int |
||
233 | |||
234 | public function done() |
||
239 | |||
240 | /** |
||
241 | * @return array |
||
242 | */ |
||
243 | public function jsonSerialize(): array |
||
256 | |||
257 | /** |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getStatus(): string |
||
264 | |||
265 | /** |
||
266 | * @param string $status |
||
267 | */ |
||
268 | public function setStatus(string $status) |
||
272 | |||
273 | /** |
||
274 | * @return \DateTime |
||
275 | */ |
||
276 | public function getCreated(): \DateTime |
||
280 | |||
281 | /** |
||
282 | * @return int |
||
283 | */ |
||
284 | 11 | public function getAttempt(): int |
|
288 | |||
289 | /** |
||
290 | * @return \DateTime |
||
291 | */ |
||
292 | 11 | public function getNextAttempt(): \DateTime |
|
296 | |||
297 | /** |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getStatusDetails(): string |
||
304 | |||
305 | /** |
||
306 | * @param string $statusDetails |
||
307 | */ |
||
308 | public function setStatusDetails(string $statusDetails) |
||
312 | |||
313 | /** |
||
314 | * @return AbstractStrategy|StrategyInterface |
||
315 | */ |
||
316 | public function getStrategy() |
||
320 | |||
321 | /** |
||
322 | * @param StrategyInterface $strategy |
||
323 | */ |
||
324 | 10 | public function setStrategy(StrategyInterface $strategy) |
|
329 | |||
330 | /** |
||
331 | * @return array |
||
332 | */ |
||
333 | public function getMetadata(): array |
||
337 | |||
338 | /** |
||
339 | * @param array $metadata |
||
340 | */ |
||
341 | public function setMetadata(array $metadata) |
||
345 | |||
346 | /** |
||
347 | * @return null|string |
||
348 | */ |
||
349 | public function getCallbackUrl() |
||
353 | |||
354 | /** |
||
355 | * @param null|string $callbackUrl |
||
356 | */ |
||
357 | public function setCallbackUrl($callbackUrl) |
||
361 | } |