MessageHandlerRegistry   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 91
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerEventHandler() 0 4 1
A registerCommandHandler() 0 4 1
A registerMessageHandler() 0 12 3
A getHandlerIdsFor() 0 15 2
A getEventFqcns() 0 4 1
A getCommandFqcns() 0 4 1
A getMessageFqcns() 0 4 1
1
<?php
2
namespace PSB\Core;
3
4
5
class MessageHandlerRegistry
6
{
7
    const EVENT_HANDLER = 0;
8
    const COMMAND_HANDLER = 1;
9
10
    /**
11
     * @var string[][][]
12
     */
13
    private $handlers = [self::EVENT_HANDLER => [], self::COMMAND_HANDLER => []];
14
15
    /**
16
     * @param string $eventFqcn
17
     * @param string $handlerContainerId
18
     */
19 4
    public function registerEventHandler($eventFqcn, $handlerContainerId)
20
    {
21 4
        $this->registerMessageHandler($eventFqcn, $handlerContainerId, self::EVENT_HANDLER);
22 4
    }
23
24
    /**
25
     * @param string $eventFqcn
26
     * @param string $handlerContainerId
27
     */
28 4
    public function registerCommandHandler($eventFqcn, $handlerContainerId)
29
    {
30 4
        $this->registerMessageHandler($eventFqcn, $handlerContainerId, self::COMMAND_HANDLER);
31 4
    }
32
33
    /**
34
     * @param string $messageFqcn
35
     * @param string $handlerContainerId
36
     * @param int    $messageType
37
     */
38 5
    private function registerMessageHandler($messageFqcn, $handlerContainerId, $messageType)
39
    {
40 5
        if (!isset($this->handlers[$messageType][$messageFqcn])) {
41 5
            $this->handlers[$messageType][$messageFqcn] = [];
42
        }
43
44 5
        if (in_array($handlerContainerId, $this->handlers[$messageType][$messageFqcn])) {
45 1
            return;
46
        }
47
48 5
        $this->handlers[$messageType][$messageFqcn][] = $handlerContainerId;
49 5
    }
50
51
    /**
52
     * @param array $messageFqcns
53
     *
54
     * @return string[]
55
     */
56 1
    public function getHandlerIdsFor(array $messageFqcns)
57
    {
58 1
        $messageFqcns = array_flip($messageFqcns);
59 1
        $handlerIds = [];
60 1
        foreach ($this->handlers as $messageToHandlerIds) {
61 1
            $filteredMessageToHandlerIds = array_intersect_key($messageToHandlerIds, $messageFqcns);
62 1
            $handlerIds = array_merge(
63 1
                $handlerIds,
64 1
                iterator_to_array(
65 1
                    new \RecursiveIteratorIterator(new \RecursiveArrayIterator($filteredMessageToHandlerIds))
66
                )
67
            );
68
        }
69 1
        return array_unique($handlerIds);
70
    }
71
72
    /**
73
     * @return array
74
     */
75 3
    public function getEventFqcns()
76
    {
77 3
        return array_keys($this->handlers[self::EVENT_HANDLER]);
78
    }
79
80
    /**
81
     * @return array
82
     */
83 3
    public function getCommandFqcns()
84
    {
85 3
        return array_keys($this->handlers[self::COMMAND_HANDLER]);
86
    }
87
88
    /**
89
     * @return array
90
     */
91 2
    public function getMessageFqcns()
92
    {
93 2
        return array_merge($this->getEventFqcns(), $this->getCommandFqcns());
94
    }
95
}
96