1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\ABS\Pipe; |
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\Pipe\EventPipe |
15
|
|
|
* Abstract base class for all event handling Pipes. |
16
|
|
|
* |
17
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
18
|
|
|
* @package PEIP |
19
|
|
|
* @subpackage pipe |
20
|
|
|
* @extends \PEIP\Pipe\Pipe |
21
|
|
|
* @abstract |
22
|
|
|
* @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Channel\Channel, \PEIP\INF\Handler\Handler, \PEIP\INF\Message\MessageBuilder |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
use PEIP\Pipe\Pipe; |
26
|
|
|
|
27
|
|
|
abstract class EventPipe extends \PEIP\Pipe\Pipe |
28
|
|
|
{ |
29
|
|
|
protected $connections = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Connects the event-pipe to a given \PEIP\INF\Event\Connectable instance by listening |
33
|
|
|
* to a given event on the connectable. |
34
|
|
|
* |
35
|
|
|
* @param string $eventName name of the event to listen to |
36
|
|
|
* @param \PEIP\INF\Event\Connectable $connectable instance of \PEIP\INF\Event\Connectable to listen to |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
protected function doListen($eventName, \PEIP\INF\Event\Connectable $connectable) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if (!$connectable->hasListener($eventName, $this)) { |
|
|
|
|
41
|
|
|
$connectable->connect($eventName, $this); |
42
|
|
|
$this->connections[spl_object_hash($connectable)] = $connectable; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Disonnects the event-pipe from listening to a given event on a \PEIP\INF\Event\Connectable instance. |
48
|
|
|
* |
49
|
|
|
* @param string $eventName name of the event to unlisten |
50
|
|
|
* @param \PEIP\INF\Event\Connectable $connectable instance of \PEIP\INF\Event\Connectable to unlisten to |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
protected function doUnlisten($eventName, \PEIP\INF\Event\Connectable $connectable) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
if (!$connectable->hasListener($eventName, $this)) { |
|
|
|
|
55
|
|
|
$connectable->disconnect($eventName, $this); |
56
|
|
|
unset($this->connections[spl_object_hash($connectable)]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the instances of \PEIP\INF\Event\Connectable the event-pipe is litening to. |
62
|
|
|
* |
63
|
|
|
* @return array array of \PEIP\INF\Event\Connectable instances |
64
|
|
|
*/ |
65
|
|
|
public function doGetConnected() |
66
|
|
|
{ |
67
|
|
|
return array_values($this->connections); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.