EventDispatcherFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 29 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasEvent\Factory;
6
7
use Arp\EventDispatcher\EventDispatcher;
8
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface;
9
use Arp\LaminasEvent\Factory\Listener\ListenerRegistrationTrait;
10
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
11
use Laminas\ServiceManager\ServiceLocatorInterface;
12
use Psr\Container\ContainerExceptionInterface;
13
use Psr\Container\ContainerInterface;
14
use Psr\Container\NotFoundExceptionInterface;
15
use Psr\EventDispatcher\EventDispatcherInterface;
16
17
final class EventDispatcherFactory extends AbstractEventDispatcherFactory
18
{
19
    use ListenerRegistrationTrait;
20
21
    /**
22
     * @param ContainerInterface&ServiceLocatorInterface $container
23
     *
24
     * @throws ServiceNotCreatedException
25
     * @throws ContainerExceptionInterface
26
     * @throws NotFoundExceptionInterface
27
     */
28
    public function __invoke(
29
        ContainerInterface $container,
30
        string $requestedName,
31
        array $options = null
32
    ): EventDispatcherInterface {
33
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'event_dispatchers');
34
35
        $listenerProvider = $this->getListenerProvider(
36
            $container,
37
            $options['listener_provider'] ?? AddableListenerProviderInterface::class,
38
            $requestedName
39
        );
40
41
        if (!$listenerProvider instanceof AddableListenerProviderInterface) {
42
            throw new ServiceNotCreatedException(
43
                sprintf(
44
                    'The listener provider must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
45
                    AddableListenerProviderInterface::class,
46
                    get_class($listenerProvider),
47
                    $requestedName
48
                )
49
            );
50
        }
51
52
        if (!empty($options['listeners']) && is_array($options['listeners'])) {
53
            $this->registerEventListeners($container, $listenerProvider, $options['listeners'], $requestedName);
54
        }
55
56
        return new EventDispatcher($listenerProvider);
57
    }
58
}
59