1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Loevgaard\Linkmobility\Response\BatchStatusResponse; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
|
6
|
|
|
class Details |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* The batch is ready for processing. The batch will be processed shortly after sendtime has passed. |
10
|
|
|
*/ |
11
|
|
|
const STATE_QUEUED = 'QUEUED'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The batch is currently processing. Messages are being sent, and the "buffered", |
15
|
|
|
* "received" and "rejected" numbers will be updated as it is processed. |
16
|
|
|
*/ |
17
|
|
|
const STATE_RUNNING = 'RUNNING'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The batch has finished processing. Up to 48 hours after this the messages will still be sent out for delivery. |
21
|
|
|
*/ |
22
|
|
|
const STATE_DONE = 'DONE'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \DateTimeImmutable |
26
|
|
|
*/ |
27
|
|
|
protected $sendTime; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $batchId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $state; |
38
|
|
|
|
39
|
1 |
|
public function __construct(array $data) |
40
|
|
|
{ |
41
|
1 |
|
Assert::that($data) |
42
|
1 |
|
->keyExists('sendtime') |
43
|
1 |
|
->keyExists('batchid') |
44
|
1 |
|
->keyExists('state') |
45
|
|
|
; |
46
|
|
|
|
47
|
1 |
|
Assert::that($data['state'])->choice(self::getStates()); |
48
|
|
|
|
49
|
1 |
|
$this->sendTime = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', $data['sendtime']); |
50
|
1 |
|
Assert::that($this->sendTime)->isInstanceOf(\DateTimeImmutable::class, '`sendtime` does not have the correct format. Value given: '.$data['sendtime']); |
51
|
|
|
|
52
|
1 |
|
$this->batchId = (int)$data['batchid']; |
53
|
1 |
|
$this->state = $data['state']; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
View Code Duplication |
public static function getStates() : array |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
1 |
|
self::STATE_DONE => self::STATE_DONE, |
60
|
1 |
|
self::STATE_QUEUED => self::STATE_QUEUED, |
61
|
1 |
|
self::STATE_RUNNING => self::STATE_RUNNING, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function isState(string $state) : bool |
66
|
|
|
{ |
67
|
1 |
|
return $this->state === $state; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
1 |
|
public function isDone() : bool |
74
|
|
|
{ |
75
|
1 |
|
return $this->isState(self::STATE_DONE); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
1 |
|
public function isQueued() : bool |
82
|
|
|
{ |
83
|
1 |
|
return $this->isState(self::STATE_QUEUED); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
1 |
|
public function isRunning() : bool |
90
|
|
|
{ |
91
|
1 |
|
return $this->isState(self::STATE_RUNNING); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \DateTimeImmutable |
96
|
|
|
*/ |
97
|
1 |
|
public function getSendTime(): \DateTimeImmutable |
98
|
|
|
{ |
99
|
1 |
|
return $this->sendTime; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
1 |
|
public function getBatchId(): int |
106
|
|
|
{ |
107
|
1 |
|
return $this->batchId; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getState(): string |
114
|
|
|
{ |
115
|
1 |
|
return $this->state; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.