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

InMemoryQueue   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 9 2
A element() 0 9 2
A poll() 0 5 2
A offer() 0 5 1
A add() 0 3 1
A peek() 0 5 1
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