ListenerProviderWithContainer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 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\Container\ContainerInterface;
13
use Psr\EventDispatcher\ListenerProviderInterface;
14
15
/**
16
 * A Listener Provider that uses a PSR container to retrieve Listeners.
17
 */
18
final class ListenerProviderWithContainer implements ListenerProviderInterface
19
{
20
    /**
21
     * @var array<class-string, string[]>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, string[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, string[]>.
Loading history...
22
     */
23
    private $listeners;
24
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * @param array<class-string, string[]> $listeners
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, string[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, string[]>.
Loading history...
32
     */
33
    public function __construct(array $listeners, ContainerInterface $container)
34
    {
35
        $this->listeners = $listeners;
36
        $this->container = $container;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     *
42
     * @return iterable<callable(object):void>
43
     */
44
    public function getListenersForEvent(object $event): iterable
45
    {
46
        foreach ($this->listeners as $class => $listeners) {
47
            if ($event instanceof $class) {
48
                foreach ($listeners as $listener) {
49
                    yield $this->container->get($listener);
50
                }
51
            }
52
        }
53
    }
54
}
55