1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Channel; |
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
|
|
|
* PollableChannel |
15
|
|
|
* Basic concete implementation of a pollable channel. |
16
|
|
|
* |
17
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
18
|
|
|
* @extends \PEIP\ABS\Channel\Channel |
19
|
|
|
* @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\PollableChannel |
20
|
|
|
*/ |
21
|
|
|
class PollableChannel extends \PEIP\ABS\Channel\Channel implements \PEIP\INF\Channel\PollableChannel |
22
|
|
|
{ |
23
|
|
|
const |
24
|
|
|
EVENT_PRE_RECEIVE = 'pre_receive', |
25
|
|
|
EVENT_POST_RECEIVE = 'post_receive', |
26
|
|
|
HEADER_MESSAGE = 'MESSAGE'; |
27
|
|
|
|
28
|
|
|
protected $messages = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sends a message on the channel. |
32
|
|
|
* |
33
|
|
|
* @param \PEIP\INF\Message\Message $message the message to send |
34
|
|
|
* |
35
|
|
|
* @return |
36
|
|
|
*/ |
37
|
|
|
protected function doSend(\PEIP\INF\Message\Message $message) |
38
|
|
|
{ |
39
|
|
|
$this->messages[] = $message; |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Receives a message from the channel. |
46
|
|
|
* |
47
|
|
|
* @event preReceive |
48
|
|
|
* @event postReceive |
49
|
|
|
* |
50
|
|
|
* @param int $timeout timout for receiving a message |
51
|
|
|
* |
52
|
|
|
* @return |
53
|
|
|
*/ |
54
|
|
|
public function receive($timeout = 0) |
55
|
|
|
{ |
56
|
|
|
$this->doFireEvent(self::EVENT_PRE_RECEIVE); |
57
|
|
|
$message = null; |
58
|
|
View Code Duplication |
if ($timeout == 0) { |
|
|
|
|
59
|
|
|
$message = $this->getMessage(); |
60
|
|
|
} elseif ($timeout < 0) { |
61
|
|
|
while (!$message = $this->getMessage()) { |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$time = time() + $timeout; |
65
|
|
|
while (($time > time()) && !$message = $this->getMessage()) { |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
$this->doFireEvent( |
69
|
|
|
self::EVENT_PRE_RECEIVE, [ |
70
|
|
|
self::HEADER_MESSAGE => $message, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $message; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a message from top of the message stack. |
79
|
|
|
* |
80
|
|
|
* @return \PEIP\INF\Message\Message message from top of the message stack |
81
|
|
|
*/ |
82
|
|
|
protected function getMessage() |
83
|
|
|
{ |
84
|
|
|
return array_shift($this->messages); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Deletes all messages on the message stack. |
89
|
|
|
* |
90
|
|
|
* @return |
91
|
|
|
*/ |
92
|
|
|
public function clear() |
93
|
|
|
{ |
94
|
|
|
$this->messages = []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Removes all messages not accepted by a given message-selector from the message-stack. |
99
|
|
|
* |
100
|
|
|
* @param \PEIP\INF\Message\Message_Selector $selector the selector to accept messages |
101
|
|
|
* |
102
|
|
|
* @return array accepted messages |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function purge(\PEIP\INF\Selector\MessageSelector $selector) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
foreach ($this->messages as $key => $message) { |
107
|
|
|
if (!$selector->acceptMessage($message)) { |
108
|
|
|
unset($this->messages[$key]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->messages; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.