Completed
Push — master ( 548654...ca6abf )
by Dmytro
24:35
created

Webhook::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Model;
6
7
use Ramsey\Uuid\Uuid;
8
use Webhook\Domain\Infrastructure\Strategy\AbstractStrategy;
9
use Webhook\Domain\Infrastructure\Strategy\LinearStrategy;
10
use Webhook\Domain\Infrastructure\Strategy\StrategyInterface;
11
12
/**
13
 * Class Webhook
14
 *
15
 * @package Webhook\Domain\Model
16
 */
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)
95
    {
96 10
        $this->id = Uuid::getFactory()->uuid4()->toString();
97 10
        $this->url = $url;
98 10
        $this->body = $body;
99 10
        $this->created = new \DateTime();
100 10
        $this->status = self::STATUS_QUEUED;
101 10
        $this->setStrategy(new LinearStrategy(60));
102 10
    }
103
104
    public function retry()
105
    {
106
        $this->attempt++;
107
        $this->calculateNextRetry();
108
109
        if ($this->attempt === $this->maxAttempts) {
110
            $this->status = self::STATUS_FAILED;
111
            $this->processed();
112
        } else {
113
            $this->status = self::STATUS_QUEUED;
114
        }
115
    }
116
117 10
    private function calculateNextRetry()
118
    {
119 10
        $interval = $this->strategy->process($this->attempt);
120 10
        $int = new \DateInterval('PT' . $interval . 'S');
121 10
        $nextAttempt = (new \DateTime())->add($int);
122
123 10
        $this->setNextAttempt($nextAttempt);
124 10
    }
125
126
    /**
127
     * @param \DateTime $nextAttempt
128
     */
129 10
    private function setNextAttempt(\DateTime $nextAttempt)
130
    {
131 10
        $this->nextAttempt = $nextAttempt;
132 10
    }
133
134
    private function processed()
135
    {
136
        $this->processed = new \DateTime();
137
    }
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
145
    {
146 11
        return $this->raw;
147
    }
148
149
    /**
150
     * @param bool $raw
151
     */
152 1
    public function setRaw(bool $raw)
153
    {
154 1
        $this->raw = $raw;
155 1
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getId(): string
161
    {
162
        return $this->id;
163
    }
164
165
    /**
166
     * @return string
167
     */
168 11
    public function getUrl(): string
169
    {
170 11
        return $this->url;
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176 11
    public function getBody()
177
    {
178 11
        return $this->body;
179
    }
180
181
    /**
182
     * @return int
183
     */
184 9
    public function getExpectedCode(): int
185
    {
186 9
        return $this->expectedCode;
187
    }
188
189
    /**
190
     * @param int $expectedCode
191
     */
192 9
    public function setExpectedCode(int $expectedCode)
193
    {
194 9
        $this->expectedCode = $expectedCode;
195 9
    }
196
197
    /**
198
     * @return mixed
199
     */
200 7
    public function getExpectedContent()
201
    {
202 7
        return $this->expectedContent;
203
    }
204
205
    /**
206
     * @param mixed $expectedContent
207
     */
208 9
    public function setExpectedContent($expectedContent)
209
    {
210 9
        $this->expectedContent = $expectedContent;
211 9
    }
212
213
    /**
214
     * @return string
215
     */
216 11
    public function getUserAgent()
217
    {
218 11
        return $this->userAgent;
219
    }
220
221
    /**
222
     * @return \DateTime
223
     */
224
    public function getProcessed(): \DateTime
225
    {
226
        return $this->processed;
227
    }
228
229
    /**
230
     * @return int
231
     */
232
    public function getMaxAttempts(): int
233
    {
234
        return $this->maxAttempts;
235
    }
236
237
    public function done()
238
    {
239
        $this->status = self::STATUS_DONE;
240
        $this->processed();
241
    }
242
243
    /**
244
     * @return array
245
     */
246
    public function jsonSerialize(): array
247
    {
248
        $result = [];
249
        foreach (get_object_vars($this) as $k => $v) {
250
            if ($v instanceof \DateTime) {
251
                $v = (int)$v->format('U');
252
            }
253
254
            $result[$k] = $v;
255
        }
256
257
        return $result;
258
    }
259
260
    /**
261
     * @return string
262
     */
263
    public function getStatus(): string
264
    {
265
        return $this->status;
266
    }
267
268
    /**
269
     * @param string $status
270
     */
271
    public function setStatus(string $status)
272
    {
273
        $this->status = $status;
274
    }
275
276
    /**
277
     * @return \DateTime
278
     */
279
    public function getCreated(): \DateTime
280
    {
281
        return $this->created;
282
    }
283
284
    /**
285
     * @return int
286
     */
287 11
    public function getAttempt(): int
288
    {
289 11
        return $this->attempt;
290
    }
291
292
    /**
293
     * @return \DateTime
294
     */
295 11
    public function getNextAttempt(): \DateTime
296
    {
297 11
        return $this->nextAttempt;
298
    }
299
300
    /**
301
     * @return string
302
     */
303
    public function getStatusDetails(): string
304
    {
305
        return $this->statusDetails;
306
    }
307
308
    /**
309
     * @param string $statusDetails
310
     */
311
    public function setStatusDetails(string $statusDetails)
312
    {
313
        $this->statusDetails = $statusDetails;
314
    }
315
316
    /**
317
     * @return AbstractStrategy|StrategyInterface
318
     */
319
    public function getStrategy()
320
    {
321
        return $this->strategy;
322
    }
323
324
    /**
325
     * @param StrategyInterface $strategy
326
     */
327 10
    public function setStrategy(StrategyInterface $strategy)
328
    {
329 10
        $this->strategy = $strategy;
330 10
        $this->calculateNextRetry();
331 10
    }
332
333
    /**
334
     * @return array
335
     */
336
    public function getMetadata(): array
337
    {
338
        return $this->metadata;
339
    }
340
341
    /**
342
     * @param array $metadata
343
     */
344
    public function setMetadata(array $metadata)
345
    {
346
        $this->metadata = $metadata;
347
    }
348
349
    /**
350
     * @return null|string
351
     */
352
    public function getCallbackUrl()
353
    {
354
        return $this->callbackUrl;
355
    }
356
357
    /**
358
     * @param null|string $callbackUrl
359
     */
360
    public function setCallbackUrl($callbackUrl)
361
    {
362
        $this->callbackUrl = $callbackUrl;
363
    }
364
365
    /**
366
     * @param int $maxAttempts
367
     */
368
    public function setMaxAttempts(int $maxAttempts): void
369
    {
370
        $this->maxAttempts = $maxAttempts;
371
    }
372
373
    /**
374
     * @return null|string
375
     */
376
    public function getReference(): ?string
377
    {
378
        return $this->reference;
379
    }
380
381
    /**
382
     * @param null|string $reference
383
     */
384
    public function setReference(?string $reference): void
385
    {
386
        $this->reference = $reference;
387
    }
388
}