Completed
Push — master ( 613d98...fa04c9 )
by Rémi
20:40
created

SyncConsumerHandler::handleSyncMessage()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 2
nop 2
1
<?php
2
3
namespace Burrow\Handler;
4
5
use Burrow\ConsumeOptions;
6
use Burrow\Driver;
7
use Burrow\Exception\ConsumerException;
8
use Burrow\Message;
9
use Burrow\QueueConsumer;
10
use Burrow\QueueHandler;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
use Psr\Log\NullLogger;
14
15
class SyncConsumerHandler implements QueueHandler, LoggerAwareInterface
16
{
17
    use LoggerAwareTrait;
18
19
    /** @var QueueConsumer */
20
    private $consumer;
21
22
    /** @var Driver */
23
    private $driver;
24
25
    /**
26
     * ConsumerHandler constructor.
27
     *
28
     * @param QueueConsumer $consumer
29
     * @param Driver        $driver
30
     */
31
    public function __construct(QueueConsumer $consumer, Driver $driver)
32
    {
33
        $this->consumer = $consumer;
34
        $this->driver = $driver;
35
36
        $this->logger = new NullLogger();
37
    }
38
39
    /**
40
     * Handle a message.
41
     *
42
     * @param Message $message
43
     *
44
     * @return bool
45
     */
46
    public function handle(Message $message)
47
    {
48
        $returnValue = $this->consumer->consume($message->getBody(), $message->getHeaders());
49
        $this->handleSyncMessage($message, $returnValue);
50
51
        return self::CONTINUE_CONSUMING;
52
    }
53
54
    /**
55
     * Modify and return the options for consumption.
56
     *
57
     * @param ConsumeOptions $options
58
     *
59
     * @return ConsumeOptions
60
     */
61
    public function options(ConsumeOptions $options)
62
    {
63
        return $options;
64
    }
65
66
    /**
67
     * Handle the return value.
68
     *
69
     * @param Message $message
70
     * @param string  $returnValue
71
     */
72
    private function handleSyncMessage(Message $message, $returnValue)
73
    {
74
        if ($message->getCorrelationId() == '' && $message->getReplyTo() == '') {
75
            throw ConsumerException::invalidSyncMessage();
76
        }
77
78
        $this->logger->debug(
79
            'Send return value back!',
80
            [
81
                'returnValue' => $returnValue,
82
                'correlationId' => $message->getCorrelationId(),
83
                'replyTo' => $message->getReplyTo()
84
            ]
85
        );
86
87
        $this->driver->publish(
88
            '',
89
            new Message(
90
                $returnValue,
91
                $message->getReplyTo(),
92
                $message->getHeaders(),
0 ignored issues
show
Documentation introduced by
$message->getHeaders() is of type array<integer,string>, but the function expects a array<integer,object<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...
93
                $message->getCorrelationId()
94
            )
95
        );
96
    }
97
}
98