|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Queue; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\AbstractProtocolException; |
|
7
|
|
|
use Genkgo\Mail\Exception\EmptyQueueException; |
|
8
|
|
|
use Genkgo\Mail\MessageInterface; |
|
9
|
|
|
use Genkgo\Mail\Transport\QueueIfFailedTransport; |
|
10
|
|
|
use Genkgo\Mail\TransportInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class QueueProcessor |
|
14
|
|
|
* @package Genkgo\Mail\Queue |
|
15
|
|
|
*/ |
|
16
|
|
|
final class QueueProcessor |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var TransportInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $transport; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array|QueueInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $queue; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param TransportInterface $transport |
|
30
|
|
|
* @param QueueInterface[] $queue |
|
31
|
|
|
*/ |
|
32
|
7 |
|
public function __construct(TransportInterface $transport, iterable $queue) |
|
33
|
|
|
{ |
|
34
|
7 |
|
$this->transport = $transport; |
|
35
|
7 |
|
$this->queue = $queue; |
|
36
|
7 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int|null $messageLimit |
|
40
|
|
|
* @param int|null $timeLimit |
|
41
|
|
|
* @return int |
|
42
|
|
|
* @throws \Genkgo\Mail\Exception\QueueStoreException |
|
43
|
|
|
*/ |
|
44
|
7 |
|
public function process(?int $messageLimit = null, ?int $timeLimit = null): int |
|
45
|
|
|
{ |
|
46
|
7 |
|
$count = 0; |
|
47
|
7 |
|
foreach ($this->queue as $queue) { |
|
48
|
|
|
try { |
|
49
|
7 |
|
while ($message = $queue->fetch()) { |
|
50
|
7 |
|
if ($messageLimit && $count >= $messageLimit) { |
|
|
|
|
|
|
51
|
1 |
|
$queue->store($message); |
|
52
|
|
|
|
|
53
|
1 |
|
return $count; |
|
54
|
|
|
} |
|
55
|
|
|
try { |
|
56
|
7 |
|
if (!$this->hasMessageExpired($message, (int) $timeLimit)) { |
|
57
|
7 |
|
$this->transport->send($message); |
|
58
|
|
|
} |
|
59
|
2 |
|
} catch (AbstractProtocolException $e) { |
|
60
|
2 |
|
$queue->store($message); |
|
61
|
|
|
|
|
62
|
|
|
// do not continue transporting messages |
|
63
|
|
|
// apparently our transport is not ready to receive messages yet |
|
64
|
2 |
|
return $count; |
|
65
|
|
|
} |
|
66
|
5 |
|
++$count; |
|
67
|
|
|
} |
|
68
|
4 |
|
} catch (EmptyQueueException $e) { |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
return $count; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param MessageInterface $message |
|
77
|
|
|
* @param int $timeLimit |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
7 |
|
private function hasMessageExpired(MessageInterface $message, int $timeLimit): bool |
|
81
|
|
|
{ |
|
82
|
7 |
|
if ($timeLimit === 0) { |
|
83
|
6 |
|
return false; |
|
84
|
|
|
} |
|
85
|
1 |
|
if ($this->getSubmissionTimestamp($message) + $timeLimit > \time()) { |
|
86
|
1 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param MessageInterface $message |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
1 |
|
private function getSubmissionTimestamp(MessageInterface $message): int |
|
97
|
|
|
{ |
|
98
|
1 |
|
$queuedAt = $message->getHeader(QueueIfFailedTransport::QUEUED_HEADER); |
|
99
|
|
|
|
|
100
|
1 |
|
return (int) \strtotime(reset($queuedAt)->getValue()->getRaw()); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: