Completed
Push — master ( f743de...f8a04a )
by Daniel
03:18
created

BaseService::createCallback()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 2
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\InvalidBodyAMQPMessageException;
6
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcInternalErrorException;
7
use Cmobi\RabbitmqBundle\Rpc\Response\JsonRpcResponse;
8
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollectionInterface;
9
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseInterface;
10
use PhpAmqpLib\Message\AMQPMessage;
11
12
class BaseService implements RpcServiceInterface
13
{
14
    private $queueName;
15
    private $rpcHandler;
16
    private $rpcMessager;
17
18
    /** @var array */
19
    protected $queueOptions = [
20
        'name'                  => null,
21
        'passive'               => false,
22
        'durable'               => true,
23
        'exclusive'             => false,
24
        'auto_delete'           => false, //Em caso de falha no serviço a filha se mantém para que outro processe.
25
        'nowait'                => false,
26
        'arguments'             => null,
27
        'ticket'                => null
28
    ];
29
30
    public function __construct(Handler $handler, RpcMessager $messager, array $queueOptions, array $parameters = null)
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $this->rpcHandler = $handler;
33
        $this->rpcMessager = $messager;
34
        $this->queueName = $queueOptions['name'];
35
        $this->queueOptions = array_merge($this->queueOptions, $queueOptions);
36
    }
37
38
    /**
39
     * @return \Closure
40
     * @throws InvalidBodyAMQPMessageException
41
     */
42
    public function createCallback()
43
    {
44
        $callback = function (AMQPMessage $message) {
45
46
            $responseCollection = $this->rpcMessager->getResponseCollection();
47
            $requestCollection = $this->rpcMessager->getRequestCollection();
48
            try {
49
                $this->rpcMessager->parseAMQPMessage($message);
50
            } catch (\Exception $e) {
51
                $exception = new JsonRpcInternalErrorException();
52
                $response = new JsonRpcResponse([], $exception);
53
                $responseCollection->add(null, $response);
54
            }
55
            $response = $this->getHandler()->handle($requestCollection, $responseCollection);
56
            $messageResponse = $this->buildResponseMessage($response, $message);
57
58
            $message->delivery_info['channel']->basic_publish(
59
                $messageResponse,
60
                '',
61
                $message->get('reply_to')
62
            );
63
            $message->delivery_info['channel']->basic_ack(
64
                $message->delivery_info['delivery_tag']
0 ignored issues
show
Documentation introduced by
$message->delivery_info['delivery_tag'] is of type object<PhpAmqpLib\Channel\AMQPChannel>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
            );
66
        };
67
68
        return $callback;
69
    }
70
71
    public function buildResponseMessage(RpcResponseCollectionInterface $response, AMQPMessage $requestMessage)
72
    {
73
        $amqpResponse = new AMQPMessage(
74
            (string)$response,
75
            ['correlation_id' => $requestMessage->get('correlation_id')]
76
        );
77
78
        return $amqpResponse;
79
    }
80
81
    public function getQueueName()
82
    {
83
        return $this->queueName;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getQueueOptions()
90
    {
91
        return $this->queueOptions;
92
    }
93
94
    /**
95
     * @return Handler
96
     */
97
    public function getHandler()
98
    {
99
        return $this->rpcHandler;
100
    }
101
102
    /**
103
     * @return RpcMessager
104
     */
105
    public function getMessager()
106
    {
107
        return $this->rpcMessager;
108
    }
109
}