Completed
Branch master (1d1574)
by Evgenij
18:38
created

CallbackEventHandler::removeHandler()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 8.8571
cc 6
eloc 9
nc 4
nop 1
crap 6
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2016, 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
14
/**
15
 * Class CallbackEventHandler
16
 */
17
class CallbackEventHandler implements \Countable, EventHandlerInterface
18
{
19
    /**
20
     * List of callables in this bag indexed by event name
21
     *
22
     * @var array
23
     */
24
    private $handlers = [];
25
26
    /**
27
     * CallbackEventHandler constructor.
28
     *
29
     * @param array $events Events to handle: [ "eventName" => callable|callable[], ... ]
30
     */
31 123
    public function __construct(array $events = [])
32
    {
33 123
        $this->handlers = [];
34 123
        $this->addHandler($events);
35 123
    }
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 123
    public function addHandler(array $events)
46
    {
47 123
        foreach ($events as $eventName => $subscriber) {
48 120
            $this->handlers[$eventName] = array_merge(
49 120
                isset($this->handlers[$eventName]) ? $this->handlers[$eventName] : [],
50 120
                is_callable($subscriber) ? [$subscriber] : $subscriber
51 120
            );
52 123
        }
53 123
    }
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 120
    public function invokeEvent(Event $event)
105
    {
106 120
        $eventName   = $event->getType();
107 120
        $subscribers = isset($this->handlers[$eventName]) ? $this->handlers[$eventName] : [];
108 120
        foreach ($subscribers as $subscriber) {
109 118
            call_user_func($subscriber, $event);
110 112
        }
111 112
    }
112
113
    /** {@inheritdoc} */
114 1
    public function count()
115
    {
116 1
        return count($this->handlers);
117
    }
118
}
119