Completed
Push — master ( f9be8c...a601ba )
by Damian
04:25
created

InMemoryQueue::element()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Initx\Driver;
4
5
use Initx\Envelope;
6
use Initx\Exception\NoSuchElementException;
7
use Initx\Queue;
8
9
final class InMemoryQueue implements Queue
10
{
11
    private $items = [];
12
13 2
    public function add(Envelope $envelope): void
14
    {
15 2
        $this->offer($envelope);
16 2
    }
17
18 2
    public function offer(Envelope $envelope): bool
19
    {
20 2
        $this->items[] = $envelope;
21
22 2
        return true;
23
    }
24
25 2
    public function remove(): Envelope
26
    {
27 2
        $element = $this->poll();
28
29 2
        if (!$element) {
30 1
            throw new NoSuchElementException();
31
        }
32
33 1
        return $element;
34
    }
35
36 2
    public function poll(): ?Envelope
37
    {
38 2
        $item = array_shift($this->items);
39
40 2
        return $item ?: null;
41
    }
42
43 2
    public function element(): Envelope
44
    {
45 2
        $envelope = $this->peek();
46
47 2
        if (!$envelope) {
48 1
            throw new NoSuchElementException();
49
        }
50
51 1
        return $envelope;
52
    }
53
54 2
    public function peek(): ?Envelope
55
    {
56 2
        $item = $this->items[0] ?? null;
57
58 2
        return $item;
59
    }
60
}
61