Completed
Push — master ( 371f7d...1c3c02 )
by Daniel
03:19
created

BaseService::getQueueOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
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\RpcInternalErrorException;
7
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcInvalidResponseException;
8
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponse;
9
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollection;
10
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollectionInterface;
11
use PhpAmqpLib\Message\AMQPMessage;
12
13
class BaseService implements RpcServiceInterface
14
{
15
    private $queueName;
16
    private $rpcHandler;
17
    private $rpcMessager;
18
19
    /** @var array */
20
    protected $queueOptions = [
21
        'name'                  => null,
22
        'passive'               => false,
23
        'durable'               => true,
24
        'exclusive'             => false,
25
        'auto_delete'           => false, //Em caso de falha no serviço a filha se mantém para que outro processe.
26
        'nowait'                => false,
27
        'arguments'             => null,
28
        'ticket'                => null
29
    ];
30
31
    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...
32
    {
33
        $this->rpcHandler = $handler;
34
        $this->rpcMessager = $messager;
35
        $this->queueName = $queueOptions['name'];
36
        $this->queueOptions = array_merge($this->queueOptions, $queueOptions);
37
    }
38
39
    /**
40
     * @return \Closure
41
     * @throws InvalidBodyAMQPMessageException
42
     */
43
    public function createCallback()
44
    {
45
        $callback = function (AMQPMessage $message) {
46
            try {
47
                $requestCollection = $this->rpcMessager->parseAMQPMessage($message);
48
                $responseCollection = $this->getHandler()->handle($requestCollection);
49
            } catch (\Exception $e) {
50
                $responseCollection = new RpcResponseCollection();
51
                $exception = new RpcInternalErrorException();
52
                $response = new RpcResponse([], $exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Cmobi\RabbitmqBun...InternalErrorException>, but the function expects a array.

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...
53
                $responseCollection->add($response);
54
            }
55
            $messageResponse = $this->buildResponseMessage($responseCollection, $message);
56
57
            $message->delivery_info['channel']->basic_publish(
58
                $messageResponse,
59
                '',
60
                $message->get('reply_to')
61
            );
62
            $message->delivery_info['channel']->basic_ack(
63
                $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...
64
            );
65
        };
66
67
        return $callback;
68
    }
69
70
    /**
71
     * @param RpcResponseCollectionInterface $responses
72
     * @param AMQPMessage $requestMessage
73
     * @return AMQPMessage
74
     * @throws RpcInvalidResponseException
75
     */
76
    public function buildResponseMessage(RpcResponseCollectionInterface $responses, AMQPMessage $requestMessage)
77
    {
78
        $rpcResponse = [];
79
        /**
80
         * @var RpcResponse $response
81
         */
82
        foreach ($responses->all() as $response) {
83
84
            if (is_null($response->id)) {
85
                $response->id = $requestMessage->get('correlation_id');
86
            }
87
            $rpcResponse[] = $response->toArray();
88
        }
89
        try {
90
            $rpcResponse = json_encode($rpcResponse);
91
        } catch (\Exception $e) {
92
            throw new RpcInvalidResponseException($e);
93
        }
94
        $amqpResponse = new AMQPMessage(
95
            (string)$rpcResponse,
96
            ['correlation_id' => $requestMessage->get('correlation_id')]
97
        );
98
99
        return $amqpResponse;
100
    }
101
102
    public function getQueueName()
103
    {
104
        return $this->queueName;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getQueueOptions()
111
    {
112
        return $this->queueOptions;
113
    }
114
115
    /**
116
     * @return Handler
117
     */
118
    public function getHandler()
119
    {
120
        return $this->rpcHandler;
121
    }
122
123
    /**
124
     * @return RpcMessager
125
     */
126
    public function getMessager()
127
    {
128
        return $this->rpcMessager;
129
    }
130
}