EventSubscriberByMap::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Event\EventSubscriber;
7
8
9
use Gica\Cqrs\Event\EventSubscriber;
10
use Gica\Dependency\AbstractFactory;
11
12
abstract class EventSubscriberByMap implements EventSubscriber
13
{
14
    abstract protected function getMap(): array;
15
16
    /** @var AbstractFactory */
17
    private $abstractFactory;
18
19 1
    public function __construct(
20
        AbstractFactory $abstractFactory
21
    )
22
    {
23 1
        $this->abstractFactory = $abstractFactory;
24 1
    }
25
26
    /**
27
     * @param array $listenerDescriptor
28
     * @return callable
29
     */
30 1
    private function createListenerByMethod(array $listenerDescriptor)
31
    {
32 1
        return [$this->abstractFactory->createObject($listenerDescriptor[0]), $listenerDescriptor[1]];
33
    }
34
35
    /**
36
     * @param array $listenersDescriptor
37
     * @return callable[]
38
     */
39 1
    private function createListenersByMethod(array $listenersDescriptor)
40
    {
41 1
        $result = [];
42
43 1
        foreach ($listenersDescriptor as $listenerDescriptor) {
44 1
            $result[] = $this->createListenerByMethod($listenerDescriptor);
45
        }
46
47 1
        return $result;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function getListenersForEvent($event)
54
    {
55 1
        $eventClass = get_class($event);
56
57 1
        if (isset($this->getMap()[$eventClass])) {
58 1
            return $this->createListenersByMethod($this->getMap()[$eventClass]);
59
        }
60
61 1
        return [];
62
    }
63
}