Completed
Push — master ( 4b7d76...9c0fcf )
by Tobias
12:59 queued 09:16
created

RabbitMQPublisher::publish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 6
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