1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Simple\Queue; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use DateTimeImmutable; |
9
|
|
|
use Laminas\Hydrator\ReflectionHydrator; |
10
|
|
|
use Laminas\Hydrator\Strategy\HydratorStrategy; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MessageHydrator |
14
|
|
|
* |
15
|
|
|
* Hydrator which changes system data |
16
|
|
|
* |
17
|
|
|
* @package Simple\Queue |
18
|
|
|
*/ |
19
|
|
|
class MessageHydrator |
20
|
|
|
{ |
21
|
|
|
/** @var Message */ |
22
|
|
|
private Message $message; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MessageHydrator constructor. |
26
|
|
|
* @param Message $message |
27
|
|
|
*/ |
28
|
12 |
|
public function __construct(Message $message) |
29
|
|
|
{ |
30
|
12 |
|
$this->message = clone $message; |
31
|
12 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $status |
35
|
|
|
* @return MessageHydrator |
36
|
|
|
*/ |
37
|
4 |
|
public function changeStatus(string $status): self |
38
|
|
|
{ |
39
|
4 |
|
$this->message = $this->hydrate(['status' => new Status($status)]); |
40
|
|
|
|
41
|
4 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
2 |
|
public function jobable(): self |
48
|
|
|
{ |
49
|
2 |
|
$this->message = $this->hydrate(['isJob' => true]); |
50
|
|
|
|
51
|
2 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
1 |
|
public function unJobable(): self |
58
|
|
|
{ |
59
|
1 |
|
$this->message = $this->hydrate(['isJob' => false]); |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|null $error |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
2 |
|
public function setError(?string $error): self |
69
|
|
|
{ |
70
|
2 |
|
$this->message = $this->hydrate(['error' => $error]); |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
1 |
|
public function increaseAttempt(): self |
79
|
|
|
{ |
80
|
1 |
|
$this->hydrate(['attempts' => $this->message->getAttempts() + 1]); |
81
|
|
|
|
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Message |
87
|
|
|
*/ |
88
|
12 |
|
public function getMessage(): Message |
89
|
|
|
{ |
90
|
12 |
|
return $this->message; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $data |
95
|
|
|
* @return Message |
96
|
|
|
*/ |
97
|
10 |
|
private function hydrate(array $data): Message |
98
|
|
|
{ |
99
|
|
|
/** @var Message $redeliveredMessage */ |
100
|
10 |
|
$redeliveredMessage = (new ReflectionHydrator())->hydrate($data, $this->message); |
101
|
|
|
|
102
|
10 |
|
return $redeliveredMessage; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create entity Message from array data |
107
|
|
|
* |
108
|
|
|
* @param array $data |
109
|
|
|
* @return Message |
110
|
|
|
* @throws Exception |
111
|
|
|
*/ |
112
|
1 |
|
public static function createMessage(array $data): Message |
113
|
|
|
{ |
114
|
1 |
|
$strategy = new HydratorStrategy(new ReflectionHydrator(), Message::class); |
115
|
|
|
|
116
|
|
|
/** @var Message $message */ |
117
|
1 |
|
$message = $strategy->hydrate(array_merge($data, [ |
118
|
1 |
|
'queue' => $data['queue'] ?? 'default', |
119
|
1 |
|
'event' => $data['event'] ?? null, |
120
|
1 |
|
'isJob' => $data['is_job'] ?? false, |
121
|
1 |
|
'body' => $data['body'] ?? '', |
122
|
1 |
|
'error' => $data['error'] ?? null, |
123
|
1 |
|
'attempts' => $data['attempts'] ?? 0, |
124
|
1 |
|
'status' => new Status($data['status'] ?? Status::NEW), |
125
|
1 |
|
'priority' => new Priority((int)($data['priority'] ?? Priority::DEFAULT)), |
126
|
1 |
|
'exactTime' => $data['exact_time'] ?? time(), |
127
|
1 |
|
'createdAt' => new DateTimeImmutable($data['created_at'] ?? 'now'), |
128
|
1 |
|
'redeliveredAt' => isset($data['redelivered_at']) ? new DateTimeImmutable($data['redelivered_at']) : null, |
129
|
|
|
])); |
130
|
|
|
|
131
|
1 |
|
return $message; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|