Completed
Push — master ( e91c7a...7c1ddd )
by Daniel
03:14
created

BaseService::buildResponseMessage()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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