Passed
Push — master ( 903907...3dcd6e )
by Alexander
01:28
created

EventDispatcherProvider::isCallable()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Event;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Psr\EventDispatcher\ListenerProviderInterface;
10
use Yiisoft\Di\Container;
11
use Yiisoft\Di\Support\ServiceProvider;
12
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
13
use Yiisoft\EventDispatcher\Provider\ListenerCollection;
14
use Yiisoft\EventDispatcher\Provider\Provider;
15
use Yiisoft\Injector\Injector;
16
17
final class EventDispatcherProvider extends ServiceProvider
18
{
19
    private array $eventListeners;
20
21
    /**
22
     * @param array $eventListeners Event listener list in format ['eventName1' => [$listener1, $listener2, ...]]
23
     */
24 4
    public function __construct(array $eventListeners)
25
    {
26 4
        $this->eventListeners = $eventListeners;
27 4
    }
28
29
    /**
30
     * @suppress PhanAccessMethodProtected
31
     */
32 4
    public function register(Container $container): void
33
    {
34 4
        $listenerCollection = new ListenerCollection();
35
36 4
        $injector = new Injector($container);
37
38 4
        foreach ($this->eventListeners as $eventName => $listeners) {
39 4
            if (!is_string($eventName)) {
40 1
                throw new InvalidEventConfigurationFormatException(
41 1
                    'Incorrect event listener format. Format with event name must be used.'
42
                );
43
            }
44
45 3
            if (!is_array($listeners)) {
46 1
                $type = $this->isCallable($listeners, $container) ? 'callable' : gettype($listeners);
47
48 1
                throw new InvalidEventConfigurationFormatException(
49 1
                    "Event listeners for $eventName must be an array, $type given."
50
                );
51
            }
52
53 2
            foreach ($listeners as $callable) {
54
                try {
55 2
                    if (!$this->isCallable($callable, $container)) {
56
                        $type = gettype($listeners);
57
58
                        throw new InvalidListenerConfigurationException(
59 2
                            "Listener must be a callable. $type given."
60
                        );
61
                    }
62
                } catch (ContainerExceptionInterface $exception) {
63
                    throw new InvalidListenerConfigurationException(
64
                        "Could not instantiate event listener or listener class has invalid configuration.",
65
                        0,
66
                        $exception
67
                    );
68
                }
69
70 2
                $listener = static function (object $event) use ($injector, $callable, $container) {
71 1
                    if (is_array($callable) && !is_object($callable[0])) {
72
                        $callable = [$container->get($callable[0]), $callable[1]];
73
                    }
74
75 1
                    return $injector->invoke($callable, [$event]);
76 2
                };
77 2
                $listenerCollection = $listenerCollection->add($listener, $eventName);
78
            }
79
        }
80
81 2
        $provider = new Provider($listenerCollection);
82
83 2
        $container->set(ListenerProviderInterface::class, $provider);
84 2
        $container->set(EventDispatcherInterface::class, Dispatcher::class);
85 2
    }
86
87 3
    private function isCallable($definition, Container $container): bool
88
    {
89 3
        if (is_callable($definition)) {
90 2
            return true;
91
        }
92
93
        if (
94 2
            is_array($definition)
95 2
            && array_keys($definition) === [0, 1]
96 2
            && is_string($definition[0])
97 2
            && $container->has($definition[0])
98
        ) {
99 1
            $object = $container->get($definition[0]);
100
101 1
            return method_exists($object, $definition[1]);
102
        }
103
104 1
        return false;
105
    }
106
}
107