MapDispatcher::getListeners()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PEIP\ABS\Dispatcher;
4
5
namespace PEIP\ABS\Dispatcher;
6
7
/*
8
 * This file is part of the PEIP package.
9
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
/*
16
 * PEIP\ABS\Dispatcher\MapDispatcher
17
 * Abstract base class for namespaced dispatcher.
18
 * Derived concrete classes can be used (for example) as event dispatchers.
19
 *
20
 * @author Timo Michna <timomichna/yahoo.de>
21
 * @package PEIP
22
 * @subpackage dispatcher
23
 * @extends \PEIP\ABS\Dispatcher\Dispatcher
24
 * @implements \PEIP\INF\Event\Connectable
25
 */
26
27
use PEIP\Util\Test;
28
29
abstract class MapDispatcher extends \PEIP\ABS\Dispatcher\Dispatcher implements //PEIP\INF\Dispatcher\MapDispatcher,
30
        \PEIP\INF\Event\Connectable
31
{
32
    protected $listeners = [];
33
34
    /**
35
     * Connects a listener to a given event-name.
36
     *
37
     * @param string                            $name     name of the event
38
     * @param callable|PEIP\INF\Handler\Handler $listener listener to connect
39
     *
40
     * @return
41
     */
42
    public function connect($name, $listener)
43
    {
44
        Test::ensureHandler($listener);
45
        if (!$this->hasListeners($name)) {
46
            $this->listeners[$name] = [];
47
        }
48
        $this->listeners[$name][] = $listener;
49
50
        return true;
51
    }
52
53
    /**
54
     * Disconnects a listener from a given event-name.
55
     *
56
     * @param string                            $name     name of the event
57
     * @param callable|PEIP\INF\Handler\Handler $listener listener to connect
58
     *
59
     * @return
60
     */
61
    public function disconnect($name, $listener)
62
    {
63
        if (!$this->hasListeners($name)) {
64
            return false;
65
        }
66
        $res = false;
67
        foreach ($this->listeners[$name] as $i => $callable) {
68
            if ($listener === $callable) {
69
                unset($this->listeners[$name][$i]);
70
                $res = true;
71
            }
72
        }
73
74
        return $res;
75
    }
76
77
    /**
78
     * Disconnects a listener from a given event-name.
79
     *
80
     * @param string $name name of the event
81
     *
82
     * @return
83
     */
84 View Code Duplication
    public function disconnectAll($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (!isset($this->listeners[$name])) {
87
            return false;
88
        }
89
        $this->listeners[$name] = [];
90
91
        return true;
92
    }
93
94
    /**
95
     * Checks wether any listener is registered for a given event-name.
96
     *
97
     * @param string $name name of the event
98
     *
99
     * @return bool wether any listener is registered for event-name
100
     */
101 View Code Duplication
    public function hasListeners($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        if (!isset($this->listeners[$name])) {
104
            return false;
105
        }
106
107
        return (bool) count($this->listeners[$name]);
108
    }
109
110
    /**
111
     * notifies all listeners on a event on a subject.
112
     *
113
     * @param string $name    name of the event
114
     * @param mixed  $subject the subject
115
     *
116
     * @return bool success
117
     */
118 View Code Duplication
    public function notify($name, $subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        if ($this->hasListeners($name)) {
121
            self::doNotify($this->getListeners($name), $subject);
122
123
            return true;
124
        }
125
126
        return false;
127
    }
128
129
    /**
130
     * notifies all listeners on a event on a subject until one returns a boolean true value.
131
     *
132
     * @param string $name    name of the event
133
     * @param mixed  $subject the subject
134
     *
135
     * @return \PEIP\INF\Handler\Handler listener which returned a boolean true value
136
     */
137
    public function notifyUntil($name, $subject)
138
    {
139
        if ($this->hasListeners($name)) {
140
            return self::doNotifyUntil($this->getListeners($name), $subject);
141
        }
142
    }
143
144
    /**
145
     * Returns all listeners registered for a given event-name.
146
     *
147
     * @param $name
148
     *
149
     * @return array array of \PEIP\INF\Handler\Handler instances
150
     */
151
    public function getListeners($name)
152
    {
153
        if (!isset($this->listeners[$name])) {
154
            return [];
155
        }
156
157
        return $this->listeners[$name];
158
    }
159
}
160