Completed
Pull Request — master (#43)
by Harry
07:41 queued 05:29
created

ArrayAdapter::reject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
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) {
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values(array_filte..., $messages, true); })) of type array<integer,?> is incompatible with the declared type array<integer,object<Gra...sage\MessageInterface>> of property $queue.

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..

Loading history...
43 4
            return false === array_search($message, $messages, true);
44 4
        }));
45 4
    }
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
     * @return void
53
     *
54
     * @throws FailedAcknowledgementException
55
     */
56 2
    public function reject(array $messages)
57
    {
58
        // do nothing, timeouts not implemented, so messages are immediately available
59 2
    }
60
61
    /**
62
     * @param MessageFactoryInterface $factory
63
     * @param int                     $limit
64
     *
65
     * @return LimitIterator
66
     */
67 18
    public function dequeue(MessageFactoryInterface $factory, $limit)
68
    {
69
        /*
70
         * If {@see $limit} is null then {@see LimitIterator} should be passed -1 as the count
71
         * to avoid throwing OutOfBoundsException.
72
         *
73
         * @link https://github.com/php/php-src/blob/php-5.6.12/ext/spl/internal/limititerator.inc#L60-L62
74
         */
75 18
        $count = (null === $limit) ? -1 : $limit;
76
77 18
        return new LimitIterator(new ArrayIterator($this->queue), 0, $count);
78
    }
79
80
    /**
81
     * @param array $messages
82
     */
83 20
    public function enqueue(array $messages)
84
    {
85 20
        foreach ($messages as $message) {
86 20
            $this->addMessage($message);
87 20
        }
88 20
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 4
    public function purge()
94
    {
95 4
        $this->queue = [];
96 4
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function delete()
102
    {
103 2
        $this->purge();
104 2
    }
105
106
    /**
107
     * @param MessageInterface $message
108
     */
109 20
    protected function addMessage(MessageInterface $message)
110
    {
111 20
        $this->queue[] = $message;
112 20
    }
113
}
114