| Total Complexity | 5 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Coverage | 50% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class RpcServer extends BaseConsumer |
||
| 8 | { |
||
| 9 | private $serializer = 'serialize'; |
||
| 10 | |||
| 11 | public function initServer($name) |
||
| 12 | { |
||
| 13 | $this->setExchangeOptions(['name' => $name, 'type' => 'direct']); |
||
| 14 | $this->setQueueOptions(['name' => $name . '-queue']); |
||
| 15 | } |
||
| 16 | |||
| 17 | 1 | public function processMessage(AMQPMessage $msg) |
|
| 18 | { |
||
| 19 | try { |
||
| 20 | 1 | $msg->ack(); |
|
| 21 | 1 | $result = call_user_func($this->callback, $msg); |
|
| 22 | 1 | $result = call_user_func($this->serializer, $result); |
|
| 23 | 1 | $this->sendReply($result, $msg->get('reply_to'), $msg->get('correlation_id')); |
|
| 24 | 1 | $this->consumed++; |
|
| 25 | 1 | $this->maybeStopConsumer(); |
|
| 26 | } catch (\Exception $e) { |
||
| 27 | $this->sendReply('error: ' . $e->getMessage(), $msg->get('reply_to'), $msg->get('correlation_id')); |
||
| 28 | } |
||
| 29 | 1 | } |
|
| 30 | |||
| 31 | protected function sendReply($result, $client, $correlationId) |
||
| 32 | { |
||
| 33 | $reply = new AMQPMessage($result, ['content_type' => 'text/plain', 'correlation_id' => $correlationId]); |
||
| 34 | $this->getChannel()->basic_publish($reply, '', $client); |
||
| 35 | } |
||
| 36 | |||
| 37 | 1 | public function setSerializer($serializer) |
|
| 40 | 1 | } |
|
| 41 | } |
||
| 42 |