Stomp::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Kaliop\Queueing\Plugins\StompBundle\Adapter\Stomp;
4
5
/// @todo find a better name for this class
6
class Stomp
7
{
8
    protected $client;
9
    protected $username;
10
    protected $password;
11
    protected $stompQueueName;
12
    protected $connected = false;
13
14
    /**
15
     * @param array $connectionConfig keys:
16
     *                                - connect_string
17
     *                                - credentials (optional, must be an array with 'user' and 'password'
18
     */
19
    public function __construct(array $connectionConfig)
20
    {
21
        $connectString = $connectionConfig['connect_string'];
22
        $this->client = new Client($connectString);
23
        if (isset($connectionConfig['credentials'])) {
24
            $this->setCredentials($connectionConfig['credentials']['user'], $connectionConfig['credentials']['password']);
25
        }
26
    }
27
28
    public function __destruct()
29
    {
30
        if ($this->connected) {
31
            $this->client->disconnect();
32
        }
33
    }
34
35
    public function setCredentials($username, $password)
36
    {
37
        $this->username = $username;
38
        $this->password = $password;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param bool
45
     * @return Consumer
46
     */
47
    public function setDebug($debug)
48
    {
49
        $this->client->debug = $debug;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $queueName NB: queue name as used by Stomp, including the prefix used by the broker to specify the
56
     *                          message delivery pattern
57
     * @return Producer
58
     */
59
    public function setStompQueueName($queueName)
60
    {
61
        $this->stompQueueName = $queueName;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return string the queue name as used by Stomp
68
     */
69
    public function getStompQueueName()
70
    {
71
        return $this->setompQueueName;
0 ignored issues
show
Bug introduced by
The property setompQueueName does not seem to exist. Did you mean stompQueueName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72
    }
73
74
    /**
75
     * Returns the full queue name to be used in stomp messages.
76
     * - Apache Apollo supports using routing keys at end of queue name @see https://activemq.apache.org/apollo/documentation/stomp-manual.html#Destination_Wildcards
77
     * - ActiveMQ: @see http://activemq.apache.org/wildcards.html
78
     * - RabbitMQ: @see https://www.rabbitmq.com/stomp.html
79
     * - Artemis: @see https://activemq.apache.org/components/artemis/documentation/latest/stomp.html
80
     *
81
     * @param string $routingKey assumes the amqp pattern: *=1word, #=0-or-more
82
     * @return string;
0 ignored issues
show
Documentation introduced by
The doc-type string; could not be parsed: Expected "|" or "end of type", but got ";" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
83
     */
84
    protected function getFullQueueName($routingKey = '')
85
    {
86
        $queueName = $this->stompQueueName;
87
        if ($routingKey != '') {
88
            switch($this->client->brokerVendor) {
89 View Code Duplication
                case 'Apollo':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
                    $routingKey = str_replace('#', '**', $routingKey);
91
                    $queueName = rtrim($queueName, '.') . '.' . ltrim($routingKey, '.');
92
                    break;
93
                case 'AMQ':
94 View Code Duplication
                default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                    $routingKey = str_replace('#', '>', $routingKey);
96
                    $queueName = rtrim($queueName, '.') . '.' . ltrim($routingKey, '.');
97
            }
98
        }
99
        return $queueName;
100
    }
101
102
    /**
103
     * Connects to the Stomp server. Attempts the connection only once by default, regardless of results
104
     *
105
     * @param bool $onlyOnce
106
     * @throws \FuseSource\Stomp\Exception\StompException
107
     */
108
    protected function connect($onlyOnce = true)
109
    {
110
        if ($onlyOnce && $this->connected) {
111
            return;
112
        }
113
        $this->client->connect($this->username, $this->password);
114
        $this->connected = true;
115
    }
116
117
}
118