EventMultiHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 73
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A invokeEvent() 0 10 2
A addHandler() 0 5 1
A removeHandler() 0 7 2
A getHandlerKey() 0 4 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\RequestExecutor;
11
12
use AsyncSockets\Event\Event;
13
use AsyncSockets\Socket\SocketInterface;
14
15
/**
16
 * Class EventMultiHandler
17
 */
18
class EventMultiHandler implements EventHandlerInterface
19
{
20
    /**
21
     * List of handlers
22
     *
23
     * @var EventHandlerInterface[]
24
     */
25
    private $handlers = [];
26
27
    /**
28
     * EventMultiHandler constructor.
29
     *
30
     * @param EventHandlerInterface[] $handlers List of handlers
31
     */
32 4
    public function __construct(array $handlers = [])
33
    {
34 4
        foreach ($handlers as $handler) {
35 1
            $this->addHandler($handler);
36 4
        }
37 4
    }
38
39
    /** {@inheritdoc} */
40 4
    public function invokeEvent(
41
        Event $event,
42
        RequestExecutorInterface $executor,
43
        SocketInterface $socket,
44
        ExecutionContext $context
45
    ) {
46 4
        foreach ($this->handlers as $handler) {
47 3
            $handler->invokeEvent($event, $executor, $socket, $context);
48 4
        }
49 4
    }
50
51
    /**
52
     * Add handler to list
53
     *
54
     * @param EventHandlerInterface $handler Handler to add
55
     *
56
     * @return void
57
     */
58 4
    public function addHandler(EventHandlerInterface $handler)
59
    {
60 4
        $key = $this->getHandlerKey($handler);
61 4
        $this->handlers[$key] = $handler;
62 4
    }
63
64
    /**
65
     * Remove handler from list
66
     *
67
     * @param EventHandlerInterface $handler Handler to remove
68
     *
69
     * @return void
70
     */
71 1
    public function removeHandler(EventHandlerInterface $handler)
72
    {
73 1
        $key = $this->getHandlerKey($handler);
74 1
        if (isset($this->handlers[$key])) {
75 1
            unset($this->handlers[$key]);
76 1
        }
77 1
    }
78
79
    /**
80
     * Return key for given handler
81
     *
82
     * @param EventHandlerInterface $handler Handler to get key for
83
     *
84
     * @return string
85
     */
86 4
    private function getHandlerKey(EventHandlerInterface $handler)
87
    {
88 4
        return spl_object_hash($handler);
89
    }
90
}
91