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

QueueProducer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
eloc 12
c 1
b 0
f 1
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A sendMessage() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\QueueRabbitMq;
6
7
use Jellyfish\Queue\DestinationInterface;
8
use Jellyfish\Queue\MessageInterface;
9
use Jellyfish\Queue\MessageMapperInterface;
10
use Jellyfish\Queue\ProducerInterface;
11
12
class QueueProducer implements ProducerInterface
13
{
14
    /**
15
     * @var \Jellyfish\QueueRabbitMq\ConnectionInterface
16
     */
17
    protected $connection;
18
19
    /**
20
     * @var \Jellyfish\Queue\MessageMapperInterface
21
     */
22
    protected $messageMapper;
23
24
    /**
25
     * @var \Jellyfish\QueueRabbitMq\AmqpMessageFactoryInterface
26
     */
27
    protected $amqpMessageFactory;
28
29
    /**
30
     * @param \Jellyfish\QueueRabbitMq\ConnectionInterface $connection
31
     * @param \Jellyfish\Queue\MessageMapperInterface $messageMapper
32
     * @param \Jellyfish\QueueRabbitMq\AmqpMessageFactoryInterface $amqpMessageFactory
33
     */
34
    public function __construct(
35
        ConnectionInterface $connection,
36
        MessageMapperInterface $messageMapper,
37
        AmqpMessageFactoryInterface $amqpMessageFactory
38
    ) {
39
        $this->connection = $connection;
40
        $this->messageMapper = $messageMapper;
41
        $this->amqpMessageFactory = $amqpMessageFactory;
42
    }
43
44
    /**
45
     * @param \Jellyfish\Queue\DestinationInterface $destination
46
     * @param \Jellyfish\Queue\MessageInterface $message
47
     *
48
     * @return \Jellyfish\Queue\ProducerInterface
49
     */
50
    public function sendMessage(DestinationInterface $destination, MessageInterface $message): ProducerInterface
51
    {
52
        $json = $this->messageMapper->toJson($message);
53
        $amqpMessage = $this->amqpMessageFactory->create($json);
54
55
        $this->connection->createQueue($destination);
56
        $this->connection->getChannel()->basic_publish($amqpMessage, '', $destination->getName());
57
58
        return $this;
59
    }
60
}
61