Completed
Push — master ( b123ad...2604a0 )
by Joachim
02:07
created

WebhookQueueItem::getTries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use GuzzleHttp\Psr7;
7
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
/**
14
 * The webhook queue is where events are inserted/produced and later consumed.
15
 *
16
 * @ORM\Table(name="dandomain_altapay_webhook_queue")
17
 * @ORM\Entity()
18
 * @UniqueEntity("url")
19
 */
20
class WebhookQueueItem
21
{
22
    use ORMBehaviors\Timestampable\Timestampable;
23
24
    const STATUS_PENDING = 'pending';
25
    const STATUS_ERROR = 'error';
26
    const STATUS_SUCCESS = 'success';
27
28
    /**
29
     * @var int
30
     *
31
     * @ORM\Id
32
     * @ORM\Column(name="id", type="integer")
33
     * @ORM\GeneratedValue(strategy="AUTO")
34
     */
35
    private $id;
36
37
    /**
38
     * This is the content that will be posted to the $url
39
     * This will be JSON.
40
     *
41
     * @var string
42
     *
43
     * @Assert\NotBlank()
44
     *
45
     * @ORM\Column(type="text")
46
     */
47
    private $content;
48
49
    /**
50
     * This will hold a string representation of the request sent to the $url.
51
     *
52
     * @var string|null
53
     *
54
     * @ORM\Column(type="text", nullable=true)
55
     */
56
    private $request;
57
58
    /**
59
     * This will hold a string representation of the response received from the $url.
60
     *
61
     * @var string|null
62
     *
63
     * @ORM\Column(type="text", nullable=true)
64
     */
65
    private $response;
66
67
    /**
68
     * @var int
69
     *
70
     * @Assert\GreaterThanOrEqual(0)
71
     *
72
     * @ORM\Column(type="integer")
73
     */
74
    private $tries;
75
76
    /**
77
     * This is the time when we try to consume this item.
78
     *
79
     * @var \DateTimeImmutable
80
     *
81
     * @Assert\NotBlank()
82
     *
83
     * @ORM\Column(type="datetime_immutable")
84
     */
85
    private $nextTry;
86
87
    /**
88
     * @var string
89
     *
90
     * @Assert\Choice(callback="getStatuses")
91
     *
92
     * @ORM\Column(type="string")
93
     */
94
    private $status;
95
96
    /**
97
     * @var string
98
     *
99
     * @ORM\Column(type="text", nullable=true)
100
     */
101
    private $error;
102
103
    /**
104
     * @var WebhookExchange
105
     *
106
     * @ORM\ManyToOne(targetEntity="WebhookExchange", inversedBy="webhookQueueItems")
107
     * @ORM\JoinColumn(onDelete="CASCADE")
108
     */
109
    private $webhookExchange;
110
111
    /**
112
     * @param string          $content
113
     * @param WebhookExchange $webhookExchange
114
     */
115
    public function __construct(string $content, WebhookExchange $webhookExchange)
116
    {
117
        $this->setContent($content);
118
        $this->setTries(0);
119
        $this->setNextTry(new \DateTimeImmutable());
120
        $this->setStatus(self::STATUS_PENDING);
121
        $this->setWebhookExchange($webhookExchange);
122
    }
123
124
    public static function getStatuses(): array
125
    {
126
        return [
127
            self::STATUS_PENDING => self::STATUS_PENDING,
128
            self::STATUS_ERROR => self::STATUS_ERROR,
129
            self::STATUS_SUCCESS => self::STATUS_SUCCESS,
130
        ];
131
    }
132
133
    public function markAsSuccess()
134
    {
135
        $this->incrementTries();
136
        $this->setStatus(self::STATUS_SUCCESS);
137
        $this->setError(null);
138
    }
139
140
    public function markAsError(string $error)
141
    {
142
        $this->incrementTries();
143
        $this->setStatus(self::STATUS_ERROR);
144
        $this->setError($error);
145
        $this->updateNextTry();
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getId(): int
152
    {
153
        return (int) $this->id;
154
    }
155
156
    /**
157
     * @param int $id
158
     *
159
     * @return WebhookQueueItem
160
     */
161
    public function setId(int $id): self
162
    {
163
        $this->id = $id;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getContent(): string
172
    {
173
        return $this->content;
174
    }
175
176
    /**
177
     * @param string $content
178
     *
179
     * @return WebhookQueueItem
180
     */
181
    public function setContent(string $content): self
182
    {
183
        $this->content = $content;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return null|string
190
     */
191
    public function getRequest(): ?string
192
    {
193
        return $this->request;
194
    }
195
196
    /**
197
     * @param null|string $request
198
     *
199
     * @return WebhookQueueItem
200
     */
201
    public function setRequest($request): self
202
    {
203
        if ($request instanceof RequestInterface) {
204
            $request = Psr7\str($request);
205
        }
206
207
        $this->request = $request;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return null|string
214
     */
215
    public function getResponse(): ?string
216
    {
217
        return $this->response;
218
    }
219
220
    /**
221
     * @param ResponseInterface|string $response
222
     *
223
     * @return WebhookQueueItem
224
     */
225
    public function setResponse($response): self
226
    {
227
        if ($response instanceof ResponseInterface) {
228
            $response = Psr7\str($response);
229
        }
230
231
        $this->response = $response;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @return int
238
     */
239
    public function getTries(): int
240
    {
241
        return $this->tries;
242
    }
243
244
    /**
245
     * @param int $tries
246
     *
247
     * @return WebhookQueueItem
248
     */
249
    public function setTries(int $tries): self
250
    {
251
        $this->tries = $tries;
252
253
        return $this;
254
    }
255
256
    /**
257
     * @return \DateTimeImmutable
258
     */
259
    public function getNextTry(): \DateTimeImmutable
260
    {
261
        return $this->nextTry;
262
    }
263
264
    /**
265
     * @param \DateTimeImmutable $nextTry
266
     *
267
     * @return WebhookQueueItem
268
     */
269
    public function setNextTry(\DateTimeImmutable $nextTry): self
270
    {
271
        $this->nextTry = $nextTry;
272
273
        return $this;
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public function getStatus(): string
280
    {
281
        return $this->status;
282
    }
283
284
    /**
285
     * @param string $status
286
     *
287
     * @return WebhookQueueItem
288
     */
289
    public function setStatus(string $status): self
290
    {
291
        $this->status = $status;
292
293
        return $this;
294
    }
295
296
    /**
297
     * @return string
298
     */
299
    public function getError(): ?string
300
    {
301
        return $this->error;
302
    }
303
304
    /**
305
     * @param string|null $error
306
     *
307
     * @return WebhookQueueItem
308
     */
309
    public function setError(?string $error): self
310
    {
311
        $this->error = $error;
312
313
        return $this;
314
    }
315
316
    /**
317
     * @return WebhookExchange
318
     */
319
    public function getWebhookExchange(): WebhookExchange
320
    {
321
        return $this->webhookExchange;
322
    }
323
324
    /**
325
     * @param WebhookExchange $webhookExchange
326
     *
327
     * @return WebhookQueueItem
328
     */
329
    public function setWebhookExchange(WebhookExchange $webhookExchange): self
330
    {
331
        $this->webhookExchange = $webhookExchange;
332
333
        return $this;
334
    }
335
336
    private function incrementTries(): self
337
    {
338
        ++$this->tries;
339
340
        return $this;
341
    }
342
343
    /**
344
     * This method will compute the next try and set the nextTry property.
345
     *
346
     * @return WebhookQueueItem
347
     */
348
    private function updateNextTry(): self
349
    {
350
        $minutesToWait = 2 ** $this->tries;
351
352
        if ($minutesToWait > 120) {
353
            $minutesToWait = 120;
354
        }
355
356
        $nextTry = \DateTimeImmutable::createFromMutable($this->getUpdatedAt() ?? new \DateTime());
357
        $nextTry = $nextTry->add(new \DateInterval('PT'.$minutesToWait.'M'));
358
359
        $this->setNextTry($nextTry);
360
361
        return $this;
362
    }
363
}
364