Passed
Push — master ( 4c5495...4336f6 )
by Alexander
13:11 queued 10:30
created

EventConfigurator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 70.27%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 38
c 4
b 0
f 0
dl 0
loc 81
ccs 26
cts 37
cp 0.7027
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isCallable() 0 18 6
B registerListeners() 0 41 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Web\Config;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Yiisoft\EventDispatcher\Provider\AbstractProviderConfigurator;
10
use Yiisoft\EventDispatcher\Provider\Provider;
11
use Yiisoft\Injector\Injector;
12
13
final class EventConfigurator extends AbstractProviderConfigurator
14
{
15
    private Provider $listenerProvider;
16
17
    private ContainerInterface $container;
18
19 2
    public function __construct(Provider $listenerProvider, ContainerInterface $container)
20
    {
21 2
        $this->listenerProvider = $listenerProvider;
22 2
        $this->container = $container;
23 2
    }
24
25
    /**
26
     * @suppress PhanAccessMethodProtected
27
     *
28
     * @param array $eventListeners Event listener list in format ['eventName1' => [$listener1, $listener2, ...]]
29
     */
30 2
    public function registerListeners(array $eventListeners): void
31
    {
32 2
        foreach ($eventListeners as $eventName => $listeners) {
33 2
            if (!is_string($eventName)) {
34
                throw new InvalidEventConfigurationFormatException(
35
                    'Incorrect event listener format. Format with event name must be used.'
36
                );
37
            }
38
39 2
            if (!is_array($listeners)) {
40
                $type = $this->isCallable($listeners) ? 'callable' : gettype($listeners);
41
42
                throw new InvalidEventConfigurationFormatException(
43
                    "Event listeners for $eventName must be an array, $type given."
44
                );
45
            }
46
47 2
            foreach ($listeners as $callable) {
48
                try {
49 2
                    if (!$this->isCallable($callable)) {
50
                        $type = gettype($listeners);
51
52
                        throw new InvalidListenerConfigurationException(
53 2
                            "Listener must be a callable. $type given."
54
                        );
55
                    }
56
                } catch (ContainerExceptionInterface $exception) {
57
                    $message = "Could not instantiate event listener or listener class has invalid configuration.";
58
59
                    throw new InvalidListenerConfigurationException($message, 0, $exception);
60
                }
61
62
63 2
                if (is_array($callable) && !is_object($callable[0])) {
64 1
                    $callable = [$this->container->get($callable[0]), $callable[1]];
65
                }
66
67 2
                $this->listenerProvider
68 2
                    ->attach(
69 2
                        fn ($event) => (new Injector($this->container))->invoke($callable, [$event]),
70
                        $eventName
71
                    );
72
            }
73
        }
74 2
    }
75
76 2
    private function isCallable($definition): bool
77
    {
78 2
        if (is_callable($definition)) {
79 2
            return true;
80
        }
81
82
        if (
83 1
            is_array($definition)
84 1
            && array_keys($definition) === [0, 1]
85 1
            && is_string($definition[0])
86 1
            && $this->container->has($definition[0])
87
        ) {
88 1
            $object = $this->container->get($definition[0]);
89
90 1
            return method_exists($object, $definition[1]);
91
        }
92
93
        return false;
94
    }
95
}
96