ListenerProviderWithMap   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 7
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getListenersForEvent() 0 6 4
1
<?php
2
3
/*
4
 * (c) Olivier Laviale <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace olvlvl\EventDispatcher;
11
12
use Psr\EventDispatcher\ListenerProviderInterface;
13
14
/**
15
 * A simple listener provider, that's using a mapping of event to callables.
16
 */
17
final class ListenerProviderWithMap implements ListenerProviderInterface
18
{
19
    /**
20
     * @var array<class-string, callable[]>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable[]>.
Loading history...
21
     */
22
    private $listeners;
23
24
    /**
25
     * @param array<class-string, callable[]> $listeners
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable[]>.
Loading history...
26
     */
27
    public function __construct(array $listeners)
28
    {
29
        $this->listeners = $listeners;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     *
35
     * @return iterable<callable(object):void>
36
     */
37
    public function getListenersForEvent(object $event): iterable
38
    {
39
        foreach ($this->listeners as $class => $listeners) {
40
            if ($event instanceof $class) {
41
                foreach ($listeners as $listener) {
42
                    yield $listener;
43
                }
44
            }
45
        }
46
    }
47
}
48