Completed
Push — master ( 70b387...557615 )
by Daniel
03:07
created

RpcBaseService::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 0
Metric Value
c 1
b 0
f 0
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 PhpAmqpLib\Message\AMQPMessage;
7
8
abstract class RpcBaseService implements RpcServiceInterface
9
{
10
    private $request;
11
    private $queueName;
12
13
    /** @var array */
14
    protected $queueOptions = [
15
        'name'                  => null,
16
        'passive'               => false,
17
        'durable'               => true,
18
        'exclusive'             => false,
19
        'auto_delete'           => false, //Em caso de falha no serviço a filha se mantém para que outro processe.
20
        'nowait'                => false,
21
        'arguments'             => null,
22
        'ticket'                => null
23
    ];
24
25
    public function __construct(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...
26
    {
27
        $this->queueName = $queueOptions['name'];
28
        $this->queueOptions = array_merge($this->queueOptions, $queueOptions);
29
    }
30
31
    /**
32
     * Build response for rpc server
33
     *
34
     * @return string
35
     */
36
    protected abstract function buildResponse();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
37
38
    /**
39
     * @return \Closure
40
     * @throws InvalidBodyAMQPMessageException
41
     */
42
    public function createCallback()
43
    {
44
        $callback = function (AMQPMessage $request) {
45
46
            $this->request = $request;
47
            $body = $this->buildResponse();
48
49
            if (!is_string($body) || is_null($body)) {
50
                throw new InvalidBodyAMQPMessageException('Invalid Body: Content should be string and not null.');
51
            }
52
            $message = new AMQPMessage(
53
                $body,
54
                ['correlation_id' => $request->get('correlation_id')]
55
            );
56
57
            $request->delivery_info['channel']->basic_publish(
58
                $message,
59
                '',
60
                $request->get('reply_to')
61
            );
62
            $request->delivery_info['channel']->basic_ack(
63
                $request->delivery_info['delivery_tag']
0 ignored issues
show
Documentation introduced by
$request->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
    public function getQueueName()
71
    {
72
        return $this->queueName;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getQueueOptions()
79
    {
80
        return $this->queueOptions;
81
    }
82
83
    /**
84
     * @return AMQPMessage
85
     */
86
    public function getRequest()
87
    {
88
        return $this->request;
89
    }
90
}