Passed
Pull Request — master (#17)
by Alex
17:16
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\Factory\Exception\FactoryException;
11
use Arp\Factory\FactoryInterface;
12
use Psr\EventDispatcher\EventDispatcherInterface;
13
use Psr\EventDispatcher\ListenerProviderInterface;
14
15
/**
16
 * @author  Alex Patterson <[email protected]>
17
 * @package Arp\EventDispatcher\Factory
18
 */
19
class EventDispatcherFactory implements FactoryInterface
20
{
21
    use ListenerRegistrationTrait;
22
23
    /**
24
     * @var FactoryInterface|null
25
     */
26
    private $listenerProviderFactory;
27
28
    /**
29
     * @param FactoryInterface|null $listenerProviderFactory
30
     */
31
    public function __construct(?FactoryInterface $listenerProviderFactory = null)
32
    {
33
        $this->listenerProviderFactory = $listenerProviderFactory;
34
    }
35
36
    /**
37
     * Create a new event dispatcher.
38
     *
39
     * @param array $config
40
     *
41
     * @return EventDispatcherInterface
42
     *
43
     * @throws FactoryException
44
     */
45
    public function create(array $config = []): EventDispatcherInterface
46
    {
47
        $eventDispatcher = new EventDispatcher(
48
            $this->createListenerProvider($config['listener_provider'] ?? [])
49
        );
50
51
        $listeners = $config['listeners'] ?? [];
52
        if (!empty($listeners) && $eventDispatcher instanceof AddableListenerProviderInterface) {
0 ignored issues
show
introduced by
$eventDispatcher is never a sub-type of Arp\EventDispatcher\List...stenerProviderInterface.
Loading history...
53
            $this->registerEventListeners($eventDispatcher, $listeners);
54
        }
55
56
        return $eventDispatcher;
57
    }
58
59
    /**
60
     * Create a new listener provider with the provided configuration options.
61
     *
62
     * @param ListenerProviderInterface|array $config
63
     *
64
     * @return AddableListenerProviderInterface
65
     *
66
     * @throws FactoryException
67
     */
68
    private function createListenerProvider($config): AddableListenerProviderInterface
69
    {
70
        if ($config instanceof AddableListenerProviderInterface) {
71
            return $config;
72
        }
73
74
        if (!is_array($config)) {
75
            throw new FactoryException(
76
                sprintf(
77
                    'The listener provider configuration must be of type \'array\'; \'%s\' provided in \'%s\'',
78
                    gettype($config),
79
                    __METHOD__
80
                )
81
            );
82
        }
83
84
        if (null === $this->listenerProviderFactory) {
85
            $this->listenerProviderFactory = new ListenerProviderFactory();
86
        }
87
88
        /** @var AddableListenerProviderInterface $listenerProvider */
89
        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...
90
    }
91
}
92