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

FanoutProducer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
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 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 9 1
A __construct() 0 8 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 FanoutProducer 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
    /**
46
     * @param \Jellyfish\Queue\DestinationInterface $destination
47
     * @param \Jellyfish\Queue\MessageInterface $message
48
     *
49
     * @return \Jellyfish\Queue\ProducerInterface
50
     */
51
    public function sendMessage(DestinationInterface $destination, MessageInterface $message): ProducerInterface
52
    {
53
        $json = $this->messageMapper->toJson($message);
54
        $amqpMessage = $this->amqpMessageFactory->create($json);
55
56
        $this->connection->createExchange($destination);
57
        $this->connection->getChannel()->basic_publish($amqpMessage, $destination->getName());
58
59
        return $this;
60
    }
61
}
62