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