ListenerCollection::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Listener;
6
7
class ListenerCollection implements ListenerCollectionInterface
8
{
9
    private PriorityQueue $listeners;
10
11
    private int $queueOrder = PHP_INT_MAX;
12
13
    public function __construct(iterable $listeners = [])
14
    {
15
        $this->listeners = new PriorityQueue();
16
17
        if (!empty($listeners)) {
18
            $this->addListeners($listeners);
19
        }
20
    }
21
22
    public function addListeners(iterable $listeners, int $priority = 1): void
23
    {
24
        foreach ($listeners as $listener) {
25
            $this->addListener($listener, $priority);
26
        }
27
    }
28
29
    public function addListener(callable $listener, int $priority = 1): void
30 10
    {
31
        $this->listeners->insert($listener, [$priority, $this->queueOrder--]);
32 10
    }
33
34 10
    public function getIterator(): \Traversable
35 1
    {
36
        $iterator = clone $this->listeners;
37
        $iterator->rewind();
38
39
        return $iterator;
40
    }
41
42
    public function count(): int
43
    {
44
        return $this->listeners->count();
45 7
    }
46
}
47