Passed
Push — master ( eac457...2fb50e )
by Mr
01:49
created

RabbitMq3Transport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Daikon\RabbitMq3\Transport;
4
5
use Assert\Assertion;
6
use Daikon\MessageBus\Channel\Subscription\Transport\TransportInterface;
7
use Daikon\MessageBus\EnvelopeInterface;
8
use Daikon\MessageBus\MessageBusInterface;
9
use Daikon\RabbitMq3\Connector\RabbitMq3Connector;
10
use PhpAmqpLib\Message\AMQPMessage;
11
12
final class RabbitMq3Transport implements TransportInterface
13
{
14
    private $key;
15
16
    private $connector;
17
18
    public function __construct(string $key, RabbitMq3Connector $connector)
19
    {
20
        $this->key = $key;
21
        $this->connector = $connector;
22
    }
23
24
    public function send(EnvelopeInterface $envelope, MessageBusInterface $messageBus): bool
25
    {
26
        $metadata = $envelope->getMetadata();
27
        $exchange = $metadata->get('_exchange');
28
        $routingKey = $metadata->get('_routing_key', $metadata->get('_aggregate_alias', ''));
29
30
        Assertion::notBlank($exchange);
31
        Assertion::string($routingKey);
32
33
        $payload = json_encode($envelope->toArray(), true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $payload = json_encode($envelope->toArray(), /** @scrutinizer ignore-type */ true);
Loading history...
34
        $message = new AMQPMessage($payload, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]);
35
36
        $channel = $this->connector->getConnection()->channel();
37
        $channel->basic_publish($message, $exchange, $routingKey);
38
39
        return true;
40
    }
41
42
    public function getKey(): string
43
    {
44
        return $this->key;
45
    }
46
}
47