Producer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setContentType() 0 6 1
A publish() 0 13 2
A batchPublish() 0 16 3
A getClientProperties() 0 6 1
1
<?php
2
3
namespace Kaliop\Queueing\Plugins\StompBundle\Adapter\Stomp;
4
5
use Kaliop\QueueingBundle\Queue\ProducerInterface;
6
7
class Producer extends Stomp implements ProducerInterface
8
{
9
    protected $debug = false;
10
    protected $contentType = 'text/plain';
11
12
    /**
13
     * @param string $contentType
14
     * @return Producer
15
     * @throws \Exception if unsupported contentType is used
16
     */
17
    public function setContentType($contentType)
18
    {
19
        $this->contentType = $contentType;
20
21
        return $this;
22
    }
23
24
    /**
25
     * Publishes the message
26
     *
27
     * @param string $msgBody
28
     * @param string $routingKey
29
     * @param array $additionalProperties
30
     *
31
     * @todo support custom message attributes
32
     */
33
    public function publish($msgBody, $routingKey = '', $additionalProperties = array())
34
    {
35
        $this->connect();
36
        if (! $this->client->send(
37
            $this->getFullQueueName($routingKey),
38
            $msgBody,
39
            $this->getClientProperties($additionalProperties),
40
            // at least when talking to Apollo we need this flag on or no exception will be thrown on errors
41
            true
42
        )) {
43
            throw new \RuntimeException('Message not delivered to Stomp broker');
44
        }
45
    }
46
47
    /**
48
     * @param array $messages
49
     */
50
    public function batchPublish(array $messages)
51
    {
52
        $this->connect();
53
54
        foreach($messages as $message) {
55
            if (! $this->client->send(
56
                $this->getFullQueueName(@$message['routingKey']),
57
                $message['msgBody'],
58
                $this->getClientProperties(@$message['additionalProperties']),
59
                // at least when talking to Apollo we need this flag on or no exception will be thrown on errors
60
                true
61
            )) {
62
                throw new \RuntimeException('Message not delivered to Stomp broker');
63
            }
64
        }
65
    }
66
67
    /**
68
     * Prepares the extra headers to be injected into the requests
69
     * @param array $additionalProperties
70
     * @return array
71
     */
72
    protected function getClientProperties(array $additionalProperties = array())
73
    {
74
        $result = array_merge(array('content-type' => $this->contentType), $additionalProperties);
75
76
        return $result;
77
    }
78
79
}
80