|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PEIP\ABS\Handler; |
|
4
|
|
|
|
|
5
|
|
|
namespace PEIP\ABS\Handler; |
|
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\Handler\MessageHandler |
|
17
|
|
|
* Base class for all message handling classes. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
|
20
|
|
|
* @package PEIP |
|
21
|
|
|
* @subpackage handler |
|
22
|
|
|
* @implements \PEIP\INF\Handler\Handler |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
use PEIP\Pipe\Pipe; |
|
26
|
|
|
|
|
27
|
|
|
abstract class MessageHandler extends \PEIP\ABS\Base\Connectable implements \PEIP\INF\Handler\Handler |
|
28
|
|
|
{ |
|
29
|
|
|
protected $inputChannel, |
|
30
|
|
|
$unwrapEvents = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Handles a message. Delegates the handling of the message to |
|
34
|
|
|
* abstract 'doHandle' method which must be implemented by extending classes. |
|
35
|
|
|
* |
|
36
|
|
|
* @see PEIP\ABS\Handler\MessageHandler::doHandle |
|
37
|
|
|
* @implements \PEIP\INF\Handler\Handler |
|
38
|
|
|
* |
|
39
|
|
|
* @param object $message the message to handle |
|
40
|
|
|
* |
|
41
|
|
|
* @return |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle($message) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->doHandle($this->getMessageFromObject($message)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the input channel for the message handler. |
|
50
|
|
|
* Delegates connecting of input-channel to protected method 'doSetInputChannel', |
|
51
|
|
|
* which can be overwritten by extending classes. |
|
52
|
|
|
* |
|
53
|
|
|
* @see PEIP\ABS\Handler\MessageHandler::doSetInputChannel |
|
54
|
|
|
* |
|
55
|
|
|
* @param \PEIP\INF\Channel\Channel $inputChannel the input-channel |
|
56
|
|
|
* |
|
57
|
|
|
* @return MessageHandler $this; |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setInputChannel(\PEIP\INF\Channel\Channel $inputChannel) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->doSetInputChannel($inputChannel); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Connects the handler to the input channel. |
|
68
|
|
|
* When input-channel is instance of \PEIP\INF\Channel\SubscribableChannel, |
|
69
|
|
|
* the handler subscribes to the channel. |
|
70
|
|
|
* When input-channel is instance of \PEIP\INF\Channel\PollableChannel, the |
|
71
|
|
|
* handler listens to the 'preSend' event of the channel and receives |
|
72
|
|
|
* a message, when the event occures. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \PEIP\INF\Channel\Channel $inputChannel the input-channel to connect the handler to |
|
75
|
|
|
* |
|
76
|
|
|
* @return |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function doSetInputChannel(\PEIP\INF\Channel\Channel $inputChannel) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->inputChannel = $inputChannel; |
|
81
|
|
|
if ($this->inputChannel instanceof \PEIP\INF\Channel\SubscribableChannel) { |
|
82
|
|
|
$this->inputChannel->subscribe($this); |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->unwrapEvents = true; |
|
85
|
|
|
$this->inputChannel->connect('postSend', $this); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function getMessageFromObject($object) |
|
90
|
|
|
{ |
|
91
|
|
|
$content = $object->getContent(); |
|
92
|
|
|
if ($this->unwrapEvents |
|
93
|
|
|
&& $object instanceof \PEIP\INF\Event\Event |
|
94
|
|
|
&& $object->getName() == 'postSend' |
|
95
|
|
|
&& $object->hasHeader(Pipe::HEADER_MESSAGE) |
|
96
|
|
|
&& $content instanceof \PEIP\INF\Channel\PollableChannel |
|
97
|
|
|
) { |
|
98
|
|
|
$object = $content->receive(); |
|
99
|
|
|
} |
|
100
|
|
|
if (!($object instanceof \PEIP\INF\Message\Message)) { |
|
101
|
|
|
throw new \Exception('Could not retrieve Message from Message-Argument'.print_r($object, 1)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $object; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the input-channel for this handler. |
|
109
|
|
|
* |
|
110
|
|
|
* @return \PEIP\INF\Channel\Channel input-channel for this handler |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getInputChannel() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->inputChannel; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Does the message handling logic for the handler. |
|
119
|
|
|
* Must be implemented by extending classes. |
|
120
|
|
|
* |
|
121
|
|
|
* @abstract |
|
122
|
|
|
* |
|
123
|
|
|
* @param \PEIP\INF\Message\Message $message the message to handle |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract protected function doHandle(\PEIP\INF\Message\Message $message); |
|
126
|
|
|
} |
|
127
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: