Producer::publish()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 8
b 0
f 0
nc 4
nop 4
dl 0
loc 20
ccs 0
cts 17
cp 0
crap 12
rs 9.8333
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
4
5
use PhpAmqpLib\Message\AMQPMessage;
6
use PhpAmqpLib\Wire\AMQPTable;
7
8
/**
9
 * Producer, that publishes AMQP Messages
10
 */
11
class Producer extends BaseAmqp implements ProducerInterface
12
{
13
    protected $contentType = 'text/plain';
14
    protected $deliveryMode = 2;
15
16
    public function setContentType($contentType)
17
    {
18
        $this->contentType = $contentType;
19
20
        return $this;
21
    }
22
23
    public function setDeliveryMode($deliveryMode)
24
    {
25
        $this->deliveryMode = $deliveryMode;
26
27
        return $this;
28
    }
29
30
    protected function getBasicProperties()
31
    {
32
        return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode);
33
    }
34
35
    /**
36
     * Publishes the message and merges additional properties with basic properties
37
     *
38
     * @param string $msgBody
39
     * @param string $routingKey
40
     * @param array $additionalProperties
41
     * @param array $headers
42
     */
43
    public function publish($msgBody, $routingKey = '', $additionalProperties = array(), array $headers = null)
44
    {
45
        if ($this->autoSetupFabric) {
46
            $this->setupFabric();
47
        }
48
49
        $msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties));
50
51
        if (!empty($headers)) {
52
            $headersTable = new AMQPTable($headers);
53
            $msg->set('application_headers', $headersTable);
54
        }
55
56
        $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey);
57
        $this->logger->debug('AMQP message published', array(
58
            'amqp' => array(
59
                'body' => $msgBody,
60
                'routingkeys' => $routingKey,
61
                'properties' => $additionalProperties,
62
                'headers' => $headers
63
            )
64
        ));
65
    }
66
}
67