Test Failed
Pull Request — master (#18)
by Alex
16:59 queued 14:19
created

EventDispatcherFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createListenerProvider() 0 22 4
A create() 0 12 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Factory;
6
7
use Arp\EventDispatcher\EventDispatcher;
8
use Arp\EventDispatcher\Factory\Listener\ListenerProviderFactory;
9
use Arp\EventDispatcher\Listener\AddableListenerProviderInterface;
10
use Arp\EventDispatcher\Listener\AddListenerAwareInterface;
11
use Arp\Factory\Exception\FactoryException;
12
use Arp\Factory\FactoryInterface;
13
use Psr\EventDispatcher\EventDispatcherInterface;
14
use Psr\EventDispatcher\ListenerProviderInterface;
15
16
/**
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\EventDispatcher\Factory
19
 */
20
class EventDispatcherFactory implements FactoryInterface
21
{
22
    use ListenerRegistrationTrait;
23
24
    /**
25
     * @var FactoryInterface|null
26
     */
27
    private $listenerProviderFactory;
28
29
    /**
30
     * @param FactoryInterface|null $listenerProviderFactory
31
     */
32
    public function __construct(?FactoryInterface $listenerProviderFactory = null)
33
    {
34
        $this->listenerProviderFactory = $listenerProviderFactory;
35
    }
36
37
    /**
38
     * Create a new event dispatcher.
39
     *
40
     * @param array $config
41
     *
42
     * @return EventDispatcherInterface
43
     *
44
     * @throws FactoryException
45
     */
46
    public function create(array $config = []): EventDispatcherInterface
47
    {
48
        $eventDispatcher = new EventDispatcher(
49
            $this->createListenerProvider($config['listener_provider'] ?? [])
50
        );
51
52
        $listeners = $config['listeners'] ?? [];
53
        if (!empty($listeners) && $eventDispatcher instanceof AddListenerAwareInterface) {
54
            $this->registerEventListeners($eventDispatcher, $listeners);
55
        }
56
57
        return $eventDispatcher;
58
    }
59
60
    /**
61
     * Create a new listener provider with the provided configuration options.
62
     *
63
     * @param ListenerProviderInterface|array $config
64
     *
65
     * @return AddableListenerProviderInterface
66
     *
67
     * @throws FactoryException
68
     */
69
    private function createListenerProvider($config): AddableListenerProviderInterface
70
    {
71
        if ($config instanceof AddableListenerProviderInterface) {
72
            return $config;
73
        }
74
75
        if (!is_array($config)) {
76
            throw new FactoryException(
77
                sprintf(
78
                    'The listener provider configuration must be of type \'array\'; \'%s\' provided in \'%s\'',
79
                    gettype($config),
80
                    __METHOD__
81
                )
82
            );
83
        }
84
85
        if (null === $this->listenerProviderFactory) {
86
            $this->listenerProviderFactory = new ListenerProviderFactory();
87
        }
88
89
        /** @var AddableListenerProviderInterface $listenerProvider */
90
        return $this->listenerProviderFactory->create($config);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->listenerPr...actory->create($config) could return the type Psr\EventDispatcher\ListenerProviderInterface which includes types incompatible with the type-hinted return Arp\EventDispatcher\List...stenerProviderInterface. Consider adding an additional type-check to rule them out.
Loading history...
91
    }
92
}
93