1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Dispatcher; |
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
|
|
|
* ObjectEventDispatcher |
15
|
|
|
* Event dispatcher for an abritrary amount of different objects and events. |
16
|
|
|
* Contrary to it�s parent class \PEIP\Dispatcher\ObjectMapDispatcher) this class can |
17
|
|
|
* create event-objects (\PEIP\INF\Event\Event) with the notification subject as content/subject |
18
|
|
|
* and notify it�s listners on the event-objects. Also this class only accepts instaces of |
19
|
|
|
* \PEIP\INF\Event\Event as it�s notification subjects. |
20
|
|
|
* |
21
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
22
|
|
|
* @package PEIP |
23
|
|
|
* @subpackage dispatcher |
24
|
|
|
* @extends \PEIP\Dispatcher\ObjectMapDispatcher |
25
|
|
|
* @implements \PEIP\INF\Dispatcher\ObjectMapDispatcher |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
use PEIP\Event\EventBuilder; |
29
|
|
|
|
30
|
|
|
class ObjectEventDispatcher extends \PEIP\Dispatcher\ObjectMapDispatcher |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Notifies all listeners of a given event-object. |
34
|
|
|
* |
35
|
|
|
* @param string $name name of the event |
36
|
|
|
* @param \PEIP\INF\Event\Event $object an event object |
37
|
|
|
* |
38
|
|
|
* @return bool|null |
39
|
|
|
*/ |
40
|
|
|
public function notify($name, $object) |
41
|
|
|
{ |
42
|
|
|
if ($object instanceof \PEIP\INF\Event\Event) { |
43
|
|
|
if (is_object($object->getContent())) { |
44
|
|
|
if ($this->hasListeners($name, $object->getContent())) { |
45
|
|
|
return self::doNotify($this->getListeners($name, $object->getContent()), $object); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
throw new \InvalidArgumentException('instance of \PEIP\INF\Event\Event must contain subject'); |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
throw new \InvalidArgumentException('object must be instance of \PEIP\INF\Event\Event'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Creates an event-object with given object as content/subject and notifies |
57
|
|
|
* all registers listeners of the event. |
58
|
|
|
* |
59
|
|
|
* @param string $name name of the event |
60
|
|
|
* @param object $object the subject of the event |
61
|
|
|
* @param array $headers headers of the event-object as key/value pairs |
62
|
|
|
* @param string $eventClass event-class to create instances from |
63
|
|
|
* |
64
|
|
|
* @return |
65
|
|
|
* |
66
|
|
|
* @see EventBuilder |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function buildAndNotify($name, $object, array $headers = [], $eventClass = false, $type = false) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (!$this->hasListeners($name, $object)) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
$event = EventBuilder::getInstance($eventClass)->build( |
|
|
|
|
74
|
|
|
$object, |
75
|
|
|
$name, |
76
|
|
|
$headers |
77
|
|
|
); |
78
|
|
|
$this->notify( |
79
|
|
|
$name, |
80
|
|
|
$event |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $event; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.