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

AbstractConsumer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 9
c 1
b 0
f 1
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A doReceiveMessage() 0 9 3
A __construct() 0 6 1
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