Connectable::disconnectAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PEIP\ABS\Base;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * PEIP\ABS\Base\Connectable.
15
 *
16
 * @author Timo Michna <timomichna/yahoo.de>
17
 */
18
abstract class Connectable implements \PEIP\INF\Event\Connectable
19
{
20
    const
21
        DEFAULT_CLASS_MESSAGE_DISPATCHER = '\PEIP\Dispatcher\Dispatcher',
22
        DEFAULT_CLASS_EVENT_DISPATCHER = '\PEIP\Dispatcher\ObjectEventDispatcher',
23
        DEFAULT_EVENT_CLASS = '\PEIP\Event\Event',
24
        EVENT_CONNECT = 'connect',
25
        EVENT_DISCONNECT = 'disconnect',
26
        EVENT_SET_EVENT_DISPATCHER = 'setEventDispatcher',
27
        HEADER_DISPATCHER = 'DISPATCHER',
28
        HEADER_LISTENER = 'LISTENER',
29
        HEADER_EVENT = 'EVENT';
30
31
    protected static $sharedEventDispatcher;
32
33
    protected $eventDispatcher;
34
35
    /**
36
     * @param string                            $name
37
     * @param callable|PEIP\INF\Handler\Handler $listener
38
     *
39
     * @return
40
     */
41 View Code Duplication
    public function connect($name, $listener)
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...
42
    {
43
        \PEIP\Util\Test::ensureHandler($listener);
44
        $this->getEventDispatcher()->connect($name, $this, $listener);
45
        $this->doFireEvent(
46
            self::EVENT_CONNECT,
47
            [
48
                self::HEADER_EVENT    => $name,
49
                self::HEADER_LISTENER => $listener,
50
            ]
51
        );
52
    }
53
54
    /**
55
     * @param $name
56
     * @param callable|PEIP\INF\Handler\Handler $listener
57
     *
58
     * @return
59
     */
60 View Code Duplication
    public function disconnect($name, $listener)
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...
61
    {
62
        \PEIP\Util\Test::ensureHandler($listener);
63
        $this->getEventDispatcher()->disconnect($name, $this, $listener);
64
        $this->doFireEvent(
65
            self::EVENT_DISCONNECT,
66
            [
67
                self::HEADER_EVENT    => $name,
68
                self::HEADER_LISTENER => $listener,
69
            ],
70
            false,
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
            ''
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
73
        );
74
    }
75
76
    /**
77
     * @param $name
78
     *
79
     * @return
80
     */
81
    public function hasListeners($name)
82
    {
83
        return $this->getEventDispatcher()->hasListeners($name, $this);
84
    }
85
86
    /**
87
     * @param $name
88
     *
89
     * @return
90
     */
91
    public function getListeners($name)
92
    {
93
        return $this->getEventDispatcher()->getListeners($name, $this);
94
    }
95
96
    /**
97
     * @param $eventName
98
     * @param $callable
99
     *
100
     * @return
101
     */
102
    public function disconnectAll($name)
103
    {
104
        $this->getEventDispatcher()->disconnectAll($name, $this);
105
    }
106
107
    /**
108
     * @param $dispatcher
109
     * @param $transferListners
110
     *
111
     * @return
112
     */
113
    public function setEventDispatcher(\PEIP\Dispatcher\ObjectEventDispatcher $dispatcher, $transferListners = true)
114
    {
115
        if ($transferListners) {
116
            foreach ($this->getEventDispatcher()->getEventNames($this) as $name) {
117
                if ($this->getEventDispatcher()->hasListeners($name, $this)) {
118
                    foreach ($this->getEventDispatcher()->getListeners($name, $this) as $listener) {
119
                        $dispatcher->connect($name, $this, $listener);
120
                    }
121
                }
122
            }
123
        }
124
        $this->eventDispatcher = $dispatcher;
125
        $this->doFireEvent(self::EVENT_SET_EVENT_DISPATCHER, [self::HEADER_DISPATCHER => $dispatcher]);
126
    }
127
128
    /**
129
     * @return
130
     */
131
    public function getEventDispatcher()
132
    {
133
        return $this->eventDispatcher ? $this->eventDispatcher : $this->eventDispatcher = self::getSharedEventDispatcher();
134
    }
135
136
    protected static function getSharedEventDispatcher()
137
    {
138
        $defaultDispatcher = self::DEFAULT_CLASS_EVENT_DISPATCHER;
139
140
        return self::$sharedEventDispatcher ? self::$sharedEventDispatcher : self::$sharedEventDispatcher = new $defaultDispatcher();
141
    }
142
143
    /**
144
     * @param $name
145
     * @param $headers
146
     * @param $eventClass
147
     *
148
     * @return
149
     */
150
    protected function doFireEvent($name, array $headers = [], $eventClass = '', $type = false)
151
    {
152
        $eventClass = trim($eventClass) == '' ? static::DEFAULT_EVENT_CLASS : $eventClass;
153
154
        return $this->getEventDispatcher()->buildAndNotify($name, $this, $headers, $eventClass, $type);
155
    }
156
157
    protected static function getDefaultEventClass()
158
    {
159
        return self::DEFAULT_EVENT_CLASS;
160
    }
161
}
162