RpcServer::initServer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
4
5
use PhpAmqpLib\Message\AMQPMessage;
6
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)
38
    {
39 1
        $this->serializer = $serializer;
40 1
    }
41
}
42