Passed
Branch master (d6c481)
by Cody
01:54
created

Consumer::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Ccovey\RabbitMQ\Consumer;
4
5
use Ccovey\RabbitMQ\ChannelInterface;
6
use Ccovey\RabbitMQ\Connection\ConnectionInterface;
7
use Ccovey\RabbitMQ\QueuedMessage;
8
use Ccovey\RabbitMQ\QueueRestartManagerInterface;
9
use PhpAmqpLib\Message\AMQPMessage;
10
11
class Consumer implements ConsumerInterface
12
{
13
    /**
14
     * @var ConnectionInterface
15
     */
16
    private $connection;
17
18
    /**
19
     * @var QueueRestartManagerInterface
20
     */
21
    private $queueRestartManager;
22
23
    /**
24
     * @var ChannelInterface
25
     */
26
    private $channel;
27
28
    /**
29
     * @var callable
30
     */
31
    private $callback;
32
33 1
    public function __construct(ConnectionInterface $connection, QueueRestartManagerInterface $queueRestartManager, string $channelId = '')
34
    {
35 1
        $this->connection = $connection;
36 1
        $this->queueRestartManager = $queueRestartManager;
37 1
        $this->channel = $this->connection->getChannel($channelId);
38 1
    }
39
40
    public function setCallback(callable $callback)
41
    {
42
        $this->callback = $callback;
43
    }
44
45 1
    public function consume(Consumable $consumable)
46
    {
47 1
        $consumable->setCallback([$this, 'process']);
48 1
        $this->channel->consume($consumable);
49
50 1
        while (count($this->channel->getCallbacks())) {
51 1
            $this->channel->wait();
52
        }
53 1
    }
54
55
    protected function process(AMQPMessage $message)
56
    {
57
        $queuedMessage = new QueuedMessage($message);
58
59
        call_user_func($this->callback, $queuedMessage);
60
61
        $this->queueRestartManager->shouldRestart($queuedMessage->getQueueName());
62
    }
63
}
64