Completed
Push — master ( 06d1d5...fff99a )
by Daniel
02:51
created

RpcBaseService::createCallback()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 8.8571
nc 1
cc 3
eloc 15
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 $queueName;
11
12
    /** @var array */
13
    protected $queueOptions = [
14
        'name'                  => null,
15
        'passive'               => false,
16
        'durable'               => true,
17
        'exclusive'             => false,
18
        'auto_delete'           => false, //Em caso de falha no serviço a filha se mantém para que outro processe.
19
        'nowait'                => false,
20
        'arguments'             => null,
21
        'ticket'                => null
22
    ];
23
24
    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...
25
    {
26
        $this->queueName = $queueOptions['name'];
27
        $this->queueOptions = array_merge($this->queueOptions, $queueOptions);
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    protected abstract function buildService();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
34
    
35
    /**
36
     * @return \Closure
37
     * @throws InvalidBodyAMQPMessageException
38
     */
39
    public function createCallback()
40
    {
41
        $callback = function (AMQPMessage $request) use ($this) {
42
43
            $body = $this->buildService();
44
45
            if (!is_string($body) || is_null($body)) {
46
                throw new InvalidBodyAMQPMessageException('Invalid Body: Content should be string and not null.');
47
            }
48
            $message = new AMQPMessage(
49
                $body,
50
                ['correlation_id' => $request->get('correlation_id')]
51
            );
52
53
            $request->delivery_info['channel']->basic_publish(
54
                $message,
55
                '',
56
                $request->get('reply_to')
57
            );
58
            $request->delivery_info['channel']->basic_ack(
59
                $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...
60
            );
61
        };
62
63
        return $callback;
64
    }
65
66
    public function getQueueName()
67
    {
68
        return $this->queueName;
69
    }
70
}