Completed
Push — master ( 129581...a310fa )
by Damian
03:27
created

InMemoryQueue   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 53
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A offer() 0 5 1
A remove() 0 9 2
A peek() 0 5 1
A poll() 0 5 2
A add() 0 3 1
A element() 0 9 2
1
<?php declare(strict_types=1);
2
3
namespace Initx\Querabilis\Driver;
4
5
use Initx\Querabilis\Envelope;
6
use Initx\Querabilis\Exception\NoSuchElementException;
7
use Initx\Querabilis\Queue;
8
9
final class InMemoryQueue implements Queue
10
{
11
    /**
12
     * @var Envelope[]
13
     */
14
    private $items = [];
15
16 2
    public function add(Envelope $envelope): bool
17
    {
18 2
        return $this->offer($envelope);
19
    }
20
21 2
    public function offer(Envelope $envelope): bool
22
    {
23 2
        $this->items[] = $envelope;
24
25 2
        return true;
26
    }
27
28 2
    public function remove(): Envelope
29
    {
30 2
        $element = $this->poll();
31
32 2
        if (!$element) {
33 1
            throw new NoSuchElementException();
34
        }
35
36 1
        return $element;
37
    }
38
39 2
    public function poll(): ?Envelope
40
    {
41 2
        $item = array_shift($this->items);
42
43 2
        return $item ?: null;
44
    }
45
46 2
    public function element(): Envelope
47
    {
48 2
        $envelope = $this->peek();
49
50 2
        if (!$envelope) {
51 1
            throw new NoSuchElementException();
52
        }
53
54 1
        return $envelope;
55
    }
56
57 2
    public function peek(): ?Envelope
58
    {
59 2
        $item = $this->items[0] ?? null;
60
61 2
        return $item;
62
    }
63
}
64