Passed
Pull Request — master (#20)
by Anatolyi
13:23
created

Message::changeRedeliveredAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Simple\Queue;
6
7
use DateTimeImmutable;
8
9
/**
10
 * Class Message
11
 * @package Simple\Queue
12
 */
13
class Message
14
{
15
    /**
16
     * Message ID
17
     * Not set if the message is not send to the database
18
     *
19
     * @var string|null
20
     */
21
    private ?string $id = null;
22
23
    /**
24
     * Message status in the queue
25
     * (The message will be deleted from db if successfully processed)
26
     *
27
     * @var Status
28
     */
29
    private Status $status;
30
31
    /**
32
     * Queue name
33
     *
34
     * @var string
35
     */
36
    private string $queue;
37
38
    /**
39
     * Event name
40
     * May be empty. Necessary for a more detailed separation of consumer processing
41
     *
42
     * @var string|null
43
     */
44
    private ?string $event;
45
46
    /** @var bool */
47
    private bool $isJob;
48
49
    /**
50
     * Message data
51
     * For example: JSON, Serialized string etc.
52
     *
53
     * @var string
54
     */
55
    private string $body;
56
57
    /**
58
     * Processing priority
59
     * May affect the sequence of message processing
60
     *
61
     * @var Priority
62
     */
63
    private Priority $priority;
64
65
    /**
66
     * Number of attempts before considering processing a failure
67
     *
68
     * @var int
69
     */
70
    private int $attempts;
71
72
    /**
73
     * Error information during processing
74
     *
75
     * @var string|null
76
     */
77
    private ?string $error;
78
79
    /**
80
     * @var int
81
     */
82
    private int $exactTime;
83
84
    /**
85
     * @var DateTimeImmutable
86
     */
87
    private DateTimeImmutable $createdAt;
88
89
    /**
90
     * @var DateTimeImmutable|null
91
     */
92
    private ?DateTimeImmutable $redeliveredAt;
93
94
    /**
95
     * Message constructor.
96
     * @param string $queue
97
     * @param string $body
98
     */
99 32
    public function __construct(string $queue, string $body)
100
    {
101 32
        $this->status = new Status(Status::NEW);
102 32
        $this->queue = $queue;
103 32
        $this->body = $body;
104 32
        $this->priority = new Priority(Priority::DEFAULT);
105 32
        $this->attempts = 0;
106 32
        $this->error = null;
107 32
        $this->event = null;
108 32
        $this->isJob = false;
109 32
        $this->exactTime = time();
110 32
        $this->createdAt = new DateTimeImmutable('now');
111 32
        $this->redeliveredAt = null;
112 32
    }
113
114
    /**
115
     * @return string
116
     * @throws QueueException
117
     */
118 8
    public function getId(): string
119
    {
120 8
        if ($this->id === null) {
121 3
            throw new QueueException('The message has no id. It looks like it was not sent to the queue.');
122
        }
123
124 5
        return $this->id;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 12
    public function getStatus(): string
131
    {
132 12
        return (string)$this->status;
133
    }
134
135
    /**
136
     * @return string|null
137
     */
138 13
    public function getError(): ?string
139
    {
140 13
        return $this->error;
141
    }
142
143
    /**
144
     * @return int
145
     */
146 5
    public function getExactTime(): int
147
    {
148 5
        return $this->exactTime;
149
    }
150
151
    /**
152
     * @return DateTimeImmutable
153
     */
154 6
    public function getCreatedAt(): DateTimeImmutable
155
    {
156 6
        return $this->createdAt;
157
    }
158
159
    /**
160
     * @return int
161
     */
162 13
    public function getAttempts(): int
163
    {
164 13
        return $this->attempts;
165
    }
166
167
    /**
168
     * @return string
169
     */
170 13
    public function getQueue(): string
171
    {
172 13
        return $this->queue;
173
    }
174
175
    /**
176
     * @return string|null
177
     */
178 13
    public function getEvent(): ?string
179
    {
180 13
        return $this->event;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 12
    public function getBody(): string
187
    {
188 12
        return $this->body;
189
    }
190
191
    /**
192
     * @return int
193
     */
194 13
    public function getPriority(): int
195
    {
196 13
        return (int)((string)$this->priority);
197
    }
198
199
    /**
200
     * @return DateTimeImmutable|null
201
     */
202 12
    public function getRedeliveredAt(): ?DateTimeImmutable
203
    {
204 12
        return $this->redeliveredAt;
205
    }
206
207
    /**
208
     * @return bool
209
     */
210 2
    public function isRedelivered(): bool
211
    {
212 2
        if ((string)$this->status === Status::REDELIVERED) {
213 1
            return true;
214
        }
215
216 1
        return $this->redeliveredAt ? true : false;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222 13
    public function isJob(): bool
223
    {
224 13
        return $this->isJob;
225
    }
226
227
    /**
228
     * @param DateTimeImmutable|null $redeliveredAt
229
     * @return $this
230
     */
231 8
    public function changeRedeliveredAt(?DateTimeImmutable $redeliveredAt): self
232
    {
233 8
        $this->redeliveredAt = $redeliveredAt;
234
235 8
        return $this;
236
    }
237
238
    /**
239
     * @param string $queue
240
     * @return $this
241
     */
242 1
    public function changeQueue(string $queue): self
243
    {
244 1
        $this->queue = $queue;
245
246 1
        return $this;
247
    }
248
249
    /**
250
     * @param int $priority
251
     * @return $this
252
     */
253 8
    public function changePriority(int $priority): self
254
    {
255 8
        $this->priority = new Priority($priority);
256
257 8
        return $this;
258
    }
259
260
    /**
261
     * @param string|null $event
262
     * @return $this
263
     */
264 9
    public function withEvent(?string $event): self
265
    {
266 9
        $this->event = $event;
267
268 9
        return $this;
269
    }
270
}
271