|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Burrow\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Burrow\ConsumeOptions; |
|
6
|
|
|
use Burrow\Driver; |
|
7
|
|
|
use Burrow\Exception\ConsumerException; |
|
8
|
|
|
use Burrow\Message; |
|
9
|
|
|
use Burrow\QueueConsumer; |
|
10
|
|
|
use Burrow\QueueHandler; |
|
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
12
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
13
|
|
|
use Psr\Log\NullLogger; |
|
14
|
|
|
|
|
15
|
|
|
class SyncConsumerHandler implements QueueHandler, LoggerAwareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use LoggerAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** @var QueueConsumer */ |
|
20
|
|
|
private $consumer; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Driver */ |
|
23
|
|
|
private $driver; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* ConsumerHandler constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param QueueConsumer $consumer |
|
29
|
|
|
* @param Driver $driver |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(QueueConsumer $consumer, Driver $driver) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->consumer = $consumer; |
|
34
|
|
|
$this->driver = $driver; |
|
35
|
|
|
|
|
36
|
|
|
$this->logger = new NullLogger(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle a message. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Message $message |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function handle(Message $message) |
|
47
|
|
|
{ |
|
48
|
|
|
$returnValue = $this->consumer->consume($message->getBody(), $message->getHeaders()); |
|
49
|
|
|
$this->handleSyncMessage($message, $returnValue); |
|
50
|
|
|
|
|
51
|
|
|
return self::CONTINUE_CONSUMING; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Modify and return the options for consumption. |
|
56
|
|
|
* |
|
57
|
|
|
* @param ConsumeOptions $options |
|
58
|
|
|
* |
|
59
|
|
|
* @return ConsumeOptions |
|
60
|
|
|
*/ |
|
61
|
|
|
public function options(ConsumeOptions $options) |
|
62
|
|
|
{ |
|
63
|
|
|
return $options; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Handle the return value. |
|
68
|
|
|
* |
|
69
|
|
|
* @param Message $message |
|
70
|
|
|
* @param string $returnValue |
|
71
|
|
|
*/ |
|
72
|
|
|
private function handleSyncMessage(Message $message, $returnValue) |
|
73
|
|
|
{ |
|
74
|
|
|
self::checkMessageIsSync($message); |
|
75
|
|
|
|
|
76
|
|
|
$this->logger->debug( |
|
77
|
|
|
'Send return value back!', |
|
78
|
|
|
[ |
|
79
|
|
|
'returnValue' => $returnValue, |
|
80
|
|
|
'correlationId' => $message->getCorrelationId(), |
|
81
|
|
|
'replyTo' => $message->getReplyTo() |
|
82
|
|
|
] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$this->driver->publish( |
|
86
|
|
|
'', |
|
87
|
|
|
new Message( |
|
88
|
|
|
$returnValue, |
|
89
|
|
|
$message->getReplyTo(), |
|
90
|
|
|
$message->getHeaders(), |
|
91
|
|
|
$message->getCorrelationId() |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Message $message |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function checkMessageIsSync(Message $message) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($message->getCorrelationId() == '' && $message->getReplyTo() == '') { |
|
102
|
|
|
throw ConsumerException::invalidSyncMessage(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|