ListenerCollection::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Event;
6
7
use IteratorAggregate;
8
9
use function array_key_exists;
10
11
/**
12
 * @implements \IteratorAggregate<mixed>
13
 */
14
class ListenerCollection implements IteratorAggregate, ListenerCollectorInterface, ListenerLocatorInterface
15
{
16
    /** @var array<string, array<int, callable>> */
17
    private array $listeners;
18
19 5
    public function __construct()
20
    {
21 5
        $this->listeners = [];
22 5
    }
23
24 3
    public function addListener(string $eventClass, callable $listener): void
25
    {
26 3
        if ($this->has($eventClass)) {
27 3
            $this->listeners[$eventClass][] = $listener;
28 3
            return;
29
        }
30
31 3
        $this->listeners[$eventClass] = [$listener];
32 3
    }
33
34
    /**
35
     * @return iterable<callable>
36
     */
37 3
    public function get(string $eventClass): iterable
38
    {
39 3
        if ($this->has($eventClass)) {
40 3
            yield from $this->listeners[$eventClass];
41
        }
42 3
    }
43
44 3
    public function has(string $eventClass): bool
45
    {
46 3
        return array_key_exists($eventClass, $this->listeners);
47
    }
48
49
    /**
50
     * @return \Generator<mixed>|\Traversable<mixed>
51
     */
52 1
    public function getIterator()
53
    {
54 1
        yield from $this->listeners;
55 1
    }
56
}
57