PipeLine::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow;
5
6
class PipeLine implements PipeLineInterface, \Countable
7
{
8
    private $queue = [];
9
    private $pointer = 0;
10
    private $queueOrder = PHP_INT_MAX;
11
12 6
    public function current(): PipeInterface
13
    {
14 6
        return $this->queue[$this->pointer]['item'];
15
    }
16
17 8
    public function insert(PipeInterface $handler, int $priority = 0): void
18
    {
19 8
        $this->queue[] = [
20 8
            'item' => $handler,
21 8
            'p' => $priority,
22 8
            '_p' => $this->queueOrder--,
23
        ];
24
        usort($this->queue, function ($itemA, $itemB) {
25 8
            if ($itemA['p'] == $itemB['p']) {
26 5
                return ($itemA['_p'] > $itemB['_p']) ? -1 : 1;
27
            }
28
29 3
            return ($itemA['p'] > $itemB['p']) ? -1 : 1;
30 8
        });
31 8
    }
32
33 5
    public function next(): void
34
    {
35 5
        $this->pointer++;
36 5
    }
37
38 4
    public function valid(): bool
39
    {
40 4
        return isset($this->queue[$this->pointer]);
41
    }
42
43 1
    public function count(): int
44
    {
45 1
        return count($this->queue);
46
    }
47
48 5
    public function rewind(): void
49
    {
50 5
        $this->pointer = 0;
51 5
    }
52
}
53