Completed
Branch master (339a46)
by Damian
04:14
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\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
    use HasDefaultRemoveAndElement;
12
13
    /**
14
     * @var Envelope[]
15
     */
16
    private $items = [];
17
18 2
    public function add(Envelope $envelope): bool
19
    {
20 2
        return $this->offer($envelope);
21
    }
22
23 2
    public function offer(Envelope $envelope): bool
24
    {
25 2
        $this->items[] = $envelope;
26
27 2
        return true;
28
    }
29
30 2
    public function poll(): ?Envelope
31
    {
32 2
        $item = array_shift($this->items);
33
34 2
        return $item ?: null;
35
    }
36
37 2
    public function peek(): ?Envelope
38
    {
39 2
        $item = $this->items[0] ?? null;
40
41 2
        return $item;
42
    }
43
}
44