CallbackEventHandler::removeForEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
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 CallbackEventHandler
17
 */
18
class CallbackEventHandler implements \Countable, EventHandlerInterface
19
{
20
    /**
21
     * List of callables in this bag indexed by event name
22
     *
23
     * @var array
24
     */
25
    private $handlers = [];
26
27
    /**
28
     * CallbackEventHandler constructor.
29
     *
30
     * @param array $events Events to handle: [ "eventName" => callable|callable[], ... ]
31
     */
32 133
    public function __construct(array $events = [])
33
    {
34 133
        $this->addHandler($events);
35 133
    }
36
37
38
    /**
39
     * Add handler into this bag
40
     *
41
     * @param array $events Events to handle: [ "eventName" => callable|callable[], ... ]
42
     *
43
     * @return void
44
     */
45 133
    public function addHandler(array $events)
46
    {
47 133
        foreach ($events as $eventName => $subscriber) {
48 130
            $this->handlers[$eventName] = array_merge(
49 130
                isset($this->handlers[$eventName]) ? $this->handlers[$eventName] : [],
50 130
                is_callable($subscriber) ? [$subscriber] : $subscriber
51 130
            );
52 133
        }
53 133
    }
54
55
    /**
56
     * Remove specified handlers from this bag
57
     *
58
     * @param array $events Events to remove: [ "eventName" => callable|callable[], ... ]
59
     *
60
     * @return void
61
     */
62 4
    public function removeHandler(array $events)
63
    {
64 4
        foreach ($events as $eventName => $subscribers) {
65 4
            if (!isset($this->handlers[$eventName])) {
66 2
                continue;
67
            }
68
69 2
            $subscribers = is_callable($subscribers) ? [$subscribers] : $subscribers;
70 2
            foreach ($subscribers as $subscriber) {
71 2
                $key = array_search($subscriber, $this->handlers[$eventName], true);
72 2
                if ($key !== false) {
73 2
                    unset($this->handlers[$eventName][$key]);
74 2
                }
75 2
            }
76 4
        }
77 4
    }
78
79
    /**
80
     * Remove all handlers from bag
81
     *
82
     * @return void
83
     */
84 2
    public function removeAll()
85
    {
86 2
        $this->handlers = [];
87 2
    }
88
89
    /**
90
     * Remove all handlers for given event name
91
     *
92
     * @param string $eventName Event name to remove handlers
93
     *
94
     * @return void
95
     */
96 2
    public function removeForEvent($eventName)
97
    {
98 2
        if (isset($this->handlers[$eventName])) {
99 2
            unset($this->handlers[$eventName]);
100 2
        }
101 2
    }
102
103
    /** {@inheritdoc} */
104 130
    public function invokeEvent(
105
        Event $event,
106
        RequestExecutorInterface $executor,
107
        SocketInterface $socket,
108
        ExecutionContext $context
109
    ) {
110 130
        $eventName   = $event->getType();
111 130
        $subscribers = isset($this->handlers[$eventName]) ? $this->handlers[$eventName] : [];
112 130
        foreach ($subscribers as $subscriber) {
113 128
            call_user_func($subscriber, $event, $executor, $socket, $context);
114 122
        }
115 122
    }
116
117
    /** {@inheritdoc} */
118 1
    public function count()
119
    {
120 1
        return count($this->handlers);
121
    }
122
}
123