RabbitMQPublisher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 41.18%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 57
ccs 7
cts 17
cp 0.4118
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A publish() 0 13 2
1
<?php
2
3
namespace Happyr\SimpleBusBundle\Message\Publisher;
4
5
use OldSound\RabbitMqBundle\RabbitMq\Producer;
6
use OldSound\RabbitMqBundle\RabbitMq\Fallback;
7
use Happyr\SimpleBusBundle\Message\DelayedMessage;
8
use SimpleBus\Asynchronous\Properties\AdditionalPropertiesResolver;
9
use SimpleBus\Asynchronous\Publisher\Publisher;
10
use SimpleBus\Asynchronous\Routing\RoutingKeyResolver;
11
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopSerializer;
12
13
/**
14
 * This publisher supports delayed messages.
15
 *
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
class RabbitMQPublisher implements Publisher
19
{
20
    /**
21
     * @var MessageInEnvelopSerializer
22
     */
23
    private $serializer;
24
25
    /**
26
     * @var Producer|Fallback
27
     */
28
    private $producer;
29
30
    /**
31
     * @var RoutingKeyResolver
32
     */
33
    private $routingKeyResolver;
34
35
    /**
36
     * @var AdditionalPropertiesResolver
37
     */
38
    private $additionalPropertiesResolver;
39
40 1
    public function __construct(
41
        MessageInEnvelopSerializer $messageSerializer,
42
        $producer,
43
        RoutingKeyResolver $routingKeyResolver,
44
        AdditionalPropertiesResolver $additionalPropertiesResolver
45
    ) {
46 1
        if (!$producer instanceof Producer && !$producer instanceof Fallback) {
47
            throw new \LogicException('Producer must be an instance of OldSound\RabbitMqBundle\RabbitMq\Producer or OldSound\RabbitMqBundle\RabbitMq\Fallback');
48
        }
49
50 1
        $this->serializer = $messageSerializer;
51 1
        $this->producer = $producer;
52 1
        $this->routingKeyResolver = $routingKeyResolver;
53 1
        $this->additionalPropertiesResolver = $additionalPropertiesResolver;
54 1
    }
55
56
    /**
57
     * Publish the given Message by serializing it and handing it over to a RabbitMQ producer.
58
     *
59
     * {@inheritdoc}
60
     */
61
    public function publish($message)
62
    {
63
        $serializedMessage = $this->serializer->wrapAndSerialize($message);
64
        $routingKey = $this->routingKeyResolver->resolveRoutingKeyFor($message);
65
        $additionalProperties = $this->additionalPropertiesResolver->resolveAdditionalPropertiesFor($message);
66
67
        $headers = [];
68
        if ($message instanceof DelayedMessage) {
69
            $headers['x-delay'] = $message->getDelayedTime();
70
        }
71
72
        $this->producer->publish($serializedMessage, $routingKey, $additionalProperties, $headers);
0 ignored issues
show
Unused Code introduced by
The call to Fallback::publish() has too many arguments starting with $headers.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
    }
74
}
75