1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of graze/queue. |
||
5 | * |
||
6 | * Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | * |
||
11 | * @license https://github.com/graze/queue/blob/master/LICENSE MIT |
||
12 | * |
||
13 | * @link https://github.com/graze/queue |
||
14 | */ |
||
15 | |||
16 | namespace Graze\Queue\Adapter; |
||
17 | |||
18 | use ArrayIterator; |
||
19 | use Graze\Queue\Adapter\Exception\FailedAcknowledgementException; |
||
20 | use Graze\Queue\Message\MessageFactoryInterface; |
||
21 | use Graze\Queue\Message\MessageInterface; |
||
22 | use LimitIterator; |
||
23 | |||
24 | final class ArrayAdapter implements AdapterInterface |
||
25 | { |
||
26 | /** @var MessageInterface[] */ |
||
27 | protected $queue = []; |
||
28 | |||
29 | /** |
||
30 | * @param MessageInterface[] $messages |
||
31 | */ |
||
32 | 20 | public function __construct(array $messages = []) |
|
33 | { |
||
34 | 20 | $this->enqueue($messages); |
|
35 | 20 | } |
|
36 | |||
37 | /** |
||
38 | * @param array $messages |
||
39 | */ |
||
40 | public function acknowledge(array $messages) |
||
41 | { |
||
42 | 4 | $this->queue = array_values(array_filter($this->queue, function ($message) use ($messages) { |
|
43 | 4 | return false === array_search($message, $messages, true); |
|
44 | 4 | })); |
|
45 | 4 | } |
|
46 | |||
47 | /** |
||
48 | * @param MessageInterface[] $messages |
||
49 | * @param int $duration Number of seconds to ensure that this message stays being processed and not |
||
50 | * put back on the queue |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function extend(array $messages, $duration) |
||
55 | { |
||
56 | // do nothing, timeouts not implemented, so messages are immediately available |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Attempt to reject all the following messages (make the message immediately visible to other consumers) |
||
61 | * |
||
62 | * @param MessageInterface[] $messages |
||
63 | * |
||
64 | * @return void |
||
65 | * |
||
66 | * @throws FailedAcknowledgementException |
||
67 | */ |
||
68 | 2 | public function reject(array $messages) |
|
69 | { |
||
70 | // do nothing, timeouts not implemented, so messages are immediately available |
||
71 | 2 | } |
|
72 | |||
73 | /** |
||
74 | * @param MessageFactoryInterface $factory |
||
75 | * @param int $limit |
||
76 | * |
||
77 | * @return LimitIterator |
||
78 | */ |
||
79 | 18 | public function dequeue(MessageFactoryInterface $factory, $limit) |
|
80 | { |
||
81 | /* |
||
82 | * If {@see $limit} is null then {@see LimitIterator} should be passed -1 as the count |
||
83 | * to avoid throwing OutOfBoundsException. |
||
84 | * |
||
85 | * @link https://github.com/php/php-src/blob/php-5.6.12/ext/spl/internal/limititerator.inc#L60-L62 |
||
86 | */ |
||
87 | 18 | $count = (null === $limit) ? -1 : $limit; |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
88 | |||
89 | 18 | return new LimitIterator(new ArrayIterator($this->queue), 0, $count); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param array $messages |
||
94 | */ |
||
95 | 20 | public function enqueue(array $messages) |
|
96 | { |
||
97 | 20 | foreach ($messages as $message) { |
|
98 | 20 | $this->addMessage($message); |
|
99 | 20 | } |
|
100 | 20 | } |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 4 | public function purge() |
|
106 | { |
||
107 | 4 | $this->queue = []; |
|
108 | 4 | } |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 2 | public function delete() |
|
114 | { |
||
115 | 2 | $this->purge(); |
|
116 | 2 | } |
|
117 | |||
118 | /** |
||
119 | * @param MessageInterface $message |
||
120 | */ |
||
121 | 20 | protected function addMessage(MessageInterface $message) |
|
122 | { |
||
123 | 20 | $this->queue[] = $message; |
|
124 | 20 | } |
|
125 | } |
||
126 |