Completed
Push — master ( 0513f3...46cdfb )
by Daniel
03:05
created

BaseService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\InvalidBodyAMQPMessageException;
6
use PhpAmqpLib\Message\AMQPMessage;
7
8
class BaseService implements RpcServiceInterface
9
{
10
    private $request;
11
    private $queueName;
12
    private $rpcHandler;
13
14
    /** @var array */
15
    protected $queueOptions = [
16
        'name'                  => null,
17
        'passive'               => false,
18
        'durable'               => true,
19
        'exclusive'             => false,
20
        'auto_delete'           => false, //Em caso de falha no serviço a filha se mantém para que outro processe.
21
        'nowait'                => false,
22
        'arguments'             => null,
23
        'ticket'                => null
24
    ];
25
26
    public function __construct(Handler $handler, 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...
27
    {
28
        $this->rpcHandler = $handler;
29
        $this->queueName = $queueOptions['name'];
30
        $this->queueOptions = array_merge($this->queueOptions, $queueOptions);
31
    }
32
33
    /**
34
     * @return \Closure
35
     * @throws InvalidBodyAMQPMessageException
36
     */
37
    public function createCallback()
38
    {
39
        $callback = function (AMQPMessage $request) {
40
41
            $this->request = $request;
42
            $message = $this->getHandler()->handle($request);
43
44
            $request->delivery_info['channel']->basic_publish(
45
                $message,
46
                '',
47
                $request->get('reply_to')
48
            );
49
            $request->delivery_info['channel']->basic_ack(
50
                $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...
51
            );
52
        };
53
54
        return $callback;
55
    }
56
57
    public function getQueueName()
58
    {
59
        return $this->queueName;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getQueueOptions()
66
    {
67
        return $this->queueOptions;
68
    }
69
70
    /**
71
     * @return AMQPMessage
72
     */
73
    public function getRequest()
74
    {
75
        return $this->request;
76
    }
77
78
    /**
79
     * @return Handler
80
     */
81
    public function getHandler()
82
    {
83
        return $this->rpcHandler;
84
    }
85
}