Completed
Push — master ( 2ef464...a5d107 )
by Daniel
18s queued 11s
created

AbstractConsumer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\QueueRabbitMq;
6
7
use Jellyfish\Queue\ConsumerInterface;
8
use Jellyfish\Queue\DestinationInterface;
9
use Jellyfish\Queue\MessageInterface;
10
use Jellyfish\Queue\MessageMapperInterface;
11
use PhpAmqpLib\Message\AMQPMessage;
12
13
abstract class AbstractConsumer implements ConsumerInterface
14
{
15
    /**
16
     * @var \Jellyfish\QueueRabbitMq\ConnectionInterface
17
     */
18
    protected $connection;
19
20
    /**
21
     * @var \Jellyfish\Queue\MessageMapperInterface
22
     */
23
    protected $messageMapper;
24
25
    /**
26
     * @param \Jellyfish\QueueRabbitMq\ConnectionInterface $connection
27
     * @param \Jellyfish\Queue\MessageMapperInterface $messageMapper
28
     */
29
    public function __construct(
30
        ConnectionInterface $connection,
31
        MessageMapperInterface $messageMapper
32
    ) {
33
        $this->connection = $connection;
34
        $this->messageMapper = $messageMapper;
35
    }
36
37
    /**
38
     * @param \Jellyfish\Queue\DestinationInterface $destination
39
     * @return \Jellyfish\Queue\MessageInterface|null
40
     */
41
    protected function doReceiveMessage(DestinationInterface $destination): ?MessageInterface
42
    {
43
        $messageAsJson = $this->connection->getChannel()->basic_get($destination->getName(), true);
44
45
        if ($messageAsJson === null || !($messageAsJson instanceof AMQPMessage)) {
46
            return null;
47
        }
48
49
        return $this->messageMapper->fromJson($messageAsJson->getBody());
50
    }
51
}
52