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
|
18 |
|
*/ |
32
|
|
|
public function __construct(array $messages = []) |
33
|
18 |
|
{ |
34
|
18 |
|
$this->enqueue($messages); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $messages |
39
|
|
|
*/ |
40
|
|
|
public function acknowledge(array $messages) |
41
|
4 |
|
{ |
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
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Attempt to reject all the following messages (make the message immediately visible to other consumers) |
49
|
|
|
* |
50
|
|
|
* @param MessageInterface[] $messages |
51
|
|
|
* |
52
|
16 |
|
* @return void |
53
|
|
|
* |
54
|
|
|
* @throws FailedAcknowledgementException |
55
|
|
|
*/ |
56
|
|
|
public function reject(array $messages) |
57
|
|
|
{ |
58
|
|
|
// do nothing, timeouts not implemented, so messages are immediately available |
59
|
|
|
} |
60
|
16 |
|
|
61
|
|
|
/** |
62
|
16 |
|
* @param MessageFactoryInterface $factory |
63
|
|
|
* @param int $limit |
64
|
|
|
* |
65
|
|
|
* @return LimitIterator |
66
|
|
|
*/ |
67
|
|
|
public function dequeue(MessageFactoryInterface $factory, $limit) |
68
|
18 |
|
{ |
69
|
|
|
/* |
70
|
18 |
|
* If {@see $limit} is null then {@see LimitIterator} should be passed -1 as the count |
71
|
18 |
|
* to avoid throwing OutOfBoundsException. |
72
|
18 |
|
* |
73
|
18 |
|
* @link https://github.com/php/php-src/blob/php-5.6.12/ext/spl/internal/limititerator.inc#L60-L62 |
74
|
|
|
*/ |
75
|
|
|
$count = (null === $limit) ? -1 : $limit; |
76
|
|
|
|
77
|
|
|
return new LimitIterator(new ArrayIterator($this->queue), 0, $count); |
78
|
4 |
|
} |
79
|
|
|
|
80
|
4 |
|
/** |
81
|
4 |
|
* @param array $messages |
82
|
|
|
*/ |
83
|
|
|
public function enqueue(array $messages) |
84
|
|
|
{ |
85
|
|
|
foreach ($messages as $message) { |
86
|
2 |
|
$this->addMessage($message); |
87
|
|
|
} |
88
|
2 |
|
} |
89
|
2 |
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function purge() |
94
|
18 |
|
{ |
95
|
|
|
$this->queue = []; |
96
|
18 |
|
} |
97
|
18 |
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function delete() |
102
|
|
|
{ |
103
|
|
|
$this->purge(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param MessageInterface $message |
108
|
|
|
*/ |
109
|
|
|
protected function addMessage(MessageInterface $message) |
110
|
|
|
{ |
111
|
|
|
$this->queue[] = $message; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..