1 | <?php |
||
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) |
||
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) |
||
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) |
||
88 | |||
89 | protected function getMessageFromObject($object) |
||
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() |
||
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: