Passed
Push — master ( ce0bdd...5bd05b )
by
unknown
51s queued 12s
created

Message::isJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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