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