Completed
Pull Request — master (#12)
by Thierry
02:37
created

RabbitServiceProvider::loadMultipleConsumers()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 37
Code Lines 21

Duplication

Lines 7
Ratio 18.92 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 37
rs 6.7272
cc 7
eloc 21
nc 1
nop 1
1
<?php
2
3
namespace ETNA\Silex\Provider\RabbitMQ;
4
5
use Pimple\ServiceProviderInterface;
6
use Pimple\Container;
7
use Symfony\Component\Routing\Generator\UrlGenerator;
8
use OldSound\RabbitMqBundle\RabbitMq\Producer;
9
use OldSound\RabbitMqBundle\RabbitMq\Consumer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ETNA\Silex\Provider\RabbitMQ\Consumer.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use OldSound\RabbitMqBundle\RabbitMq\AnonConsumer;
11
use OldSound\RabbitMqBundle\RabbitMq\MultipleConsumer;
12
use OldSound\RabbitMqBundle\RabbitMq\RpcClient;
13
use OldSound\RabbitMqBundle\RabbitMq\RpcServer;
14
use PhpAmqpLib\Connection\AMQPConnection;
15
use PhpAmqpLib\Connection\AMQPLazyConnection;
16
use PhpAmqpLib\Connection\AMQPSSLConnection;
17
18
/**
19
 * Code repris de fiunchinho/rabbitmq-service-provider en attendant la compatibilité
20
 * avec silex 2
21
 */
22
class RabbitServiceProvider implements ServiceProviderInterface
23
{
24
    const DEFAULT_CONNECTION = 'default';
25
26
    public function register(Container $app)
27
    {
28
        $this->loadConnections($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...ider::loadConnections() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
29
        $this->loadProducers($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...ovider::loadProducers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
30
        $this->loadConsumers($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...ovider::loadConsumers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
31
        $this->loadAnonymousConsumers($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...oadAnonymousConsumers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
32
        $this->loadMultipleConsumers($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...loadMultipleConsumers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
33
        $this->loadRpcClients($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...vider::loadRpcClients() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
34
        $this->loadRpcServers($app);
0 ignored issues
show
Unused Code introduced by
The call to the method ETNA\Silex\Provider\Rabb...vider::loadRpcServers() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
35
    }
36
37
    /**
38
     * Return the name of the connection to use.
39
     *
40
     * @param  array     $options     Options for the Producer or Consumer.
41
     * @param  array     $connections Connections defined in the config file.
42
     * @return string                 The connection name that will be used
43
     */
44
    private function getConnection($app, $options, $connections)
45
    {
46
        $connection_name = @$options['connection']?: self::DEFAULT_CONNECTION;
47
48
        if (!isset($connections[$connection_name])) {
49
            throw new \InvalidArgumentException('Configuration for connection [' . $connection_name . '] not found');
50
        }
51
52
        return $app['rabbit.connection'][$connection_name];
53
    }
54
55
    /**
56
     * @param Container $app
57
     */
58
    private function loadConnections($app)
59
    {
60
        $app['rabbit.connection'] = function ($app) {
61
            if (!isset($app['rabbit.connections'])) {
62
                throw new \InvalidArgumentException('You need to specify at least a connection in your configuration.');
63
            }
64
65
            $connections = [];
66
            foreach ($app["rabbit.connections"] as $name => $options) {
67
                $options    = $app["rabbit.connections"][$name];
68
                $amqp_class = (isset($options["ssl"]) && true === $options["ssl"]) ?
69
                    "PhpAmqpLib\Connection\AMQPSSLConnection" :
70
                    "PhpAmqpLib\Connection\AMQPConnection";
71
72
                $connection = new $amqp_class(
73
                    $options["host"],
74
                    $options["port"],
75
                    $options["user"],
76
                    $options["password"],
77
                    $options["vhost"],
78
                    $options["connection_opt"],
79
                    [
80
                        'read_write_timeout' => 60,
81
                        'heartbeat'          => 30
82
                    ]
83
                );
84
85
                $connections[$name] = $connection;
86
            }
87
88
            return $connections;
89
        };
90
    }
91
92
    /**
93
     * @param Container $app
94
     */
95
    private function loadProducers($app)
96
    {
97
        $app['rabbit.producer'] = function ($app) {
98
            if (!isset($app['rabbit.producers'])) {
99
                return;
100
            }
101
            $producers = [];
102
            foreach ($app['rabbit.producers'] as $name => $options) {
103
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
104
105
                $producer = new Producer($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AbstractConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
                $producer->setExchangeOptions($options['exchange_options']);
107
108
                //this producer doesn't define a queue
109
                if (!isset($options['queue_options'])) {
110
                    $options['queue_options']['name'] = null;
111
                }
112
                $producer->setQueueOptions($options['queue_options']);
113
114
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
115
                    $producer->disableAutoSetupFabric();
116
                }
117
118
                $producers[$name] = $producer;
119
            }
120
121
            return $producers;
122
        };
123
    }
124
125
    /**
126
     * @param Container $app
127
     */
128
    private function loadConsumers($app)
129
    {
130
        $app['rabbit.consumer'] = function ($app) {
131
            if (!isset($app['rabbit.consumers'])) {
132
                return;
133
            }
134
135
            $consumers = [];
136
            foreach ($app['rabbit.consumers'] as $name => $options) {
137
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
138
                $consumer = new Consumer($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AbstractConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
139
                $consumer->setExchangeOptions($options['exchange_options']);
140
                $consumer->setQueueOptions($options['queue_options']);
141
                $consumer->setCallback(array($app[$options['callback']], 'execute'));
142
143 View Code Duplication
                if (array_key_exists('qos_options', $options)) {
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...
144
                    $consumer->setQosOptions(
145
                        $options['qos_options']['prefetch_size'],
146
                        $options['qos_options']['prefetch_count'],
147
                        $options['qos_options']['global']
148
                    );
149
                }
150
151
                if (array_key_exists('qos_options', $options)) {
152
                    $consumer->setIdleTimeout($options['idle_timeout']);
153
                }
154
155
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
156
                    $consumer->disableAutoSetupFabric();
157
                }
158
159
                $consumers[$name] = $consumer;
160
            }
161
162
            return $consumers;
163
        };
164
    }
165
166
    /**
167
     * @param Container $app
168
     */
169
    private function loadAnonymousConsumers($app)
170
    {
171
        $app['rabbit.anonymous_consumer'] = function ($app) {
172
            if (!isset($app['rabbit.anon_consumers'])) {
173
                return;
174
            }
175
176
            $consumers = [];
177
            foreach ($app['rabbit.anon_consumers'] as $name => $options) {
178
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
179
                $consumer = new AnonConsumer($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AMQPConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180
                $consumer->setExchangeOptions($options['exchange_options']);
181
                $consumer->setCallback(array($options['callback'], 'execute'));
182
183
                $consumers[$name] = $consumer;
184
            }
185
186
            return $consumers;
187
        };
188
    }
189
190
    /**
191
     * @param Container $app
192
     */
193
    private function loadMultipleConsumers($app)
194
    {
195
        $app['rabbit.multiple_consumer'] = function ($app) {
196
            if (!isset($app['rabbit.multiple_consumers'])) {
197
                return;
198
            }
199
200
            $consumers = [];
201
            foreach ($app['rabbit.multiple_consumers'] as $name => $options) {
202
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
203
                $consumer = new MultipleConsumer($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AbstractConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
204
                $consumer->setExchangeOptions($options['exchange_options']);
205
                $consumer->setQueues($options['queues']);
206
207 View Code Duplication
                if (array_key_exists('qos_options', $options)) {
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...
208
                    $consumer->setQosOptions(
209
                        $options['qos_options']['prefetch_size'],
210
                        $options['qos_options']['prefetch_count'],
211
                        $options['qos_options']['global']
212
                    );
213
                }
214
215
                if (array_key_exists('qos_options', $options)) {
216
                    $consumer->setIdleTimeout($options['idle_timeout']);
217
                }
218
219
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
220
                    $consumer->disableAutoSetupFabric();
221
                }
222
223
                $consumers[$name] = $consumer;
224
            }
225
226
            return $consumers;
227
        };
228
229
    }
230
231
    /**
232
     * @param Container $app
233
     */
234
    private function loadRpcClients($app)
235
    {
236
        $app['rabbit.rpc_client'] = function ($app) {
237
            if (!isset($app['rabbit.rpc_clients'])) {
238
                return;
239
            }
240
241
            $clients = [];
242
            foreach ($app['rabbit.rpc_clients'] as $name => $options) {
243
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
244
                $client = new RpcClient($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AbstractConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
245
246
                if (array_key_exists('expect_serialized_response', $options)) {
247
                    $client->initClient($options['expect_serialized_response']);
248
                }
249
250
                $clients[$name] = $client;
251
            }
252
253
            return $clients;
254
        };
255
    }
256
257
    /**
258
     * @param Container $app
259
     */
260
    private function loadRpcServers($app)
261
    {
262
        $app['rabbit.rpc_server'] = function ($app) {
263
            if (!isset($app['rabbit.rpc_servers'])) {
264
                return;
265
            }
266
267
            $servers = [];
268
            foreach ($app['rabbit.rpc_servers'] as $name => $options) {
269
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
270
                $server = new RpcServer($connection);
0 ignored issues
show
Documentation introduced by
$connection is of type string, but the function expects a object<PhpAmqpLib\Connection\AbstractConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
271
                $server->initServer($name);
272
                $server->setCallback(array($options['callback'], 'execute'));
273
274 View Code Duplication
                if (array_key_exists('qos_options', $options)) {
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...
275
                    $server->setQosOptions(
276
                        $options['qos_options']['prefetch_size'],
277
                        $options['qos_options']['prefetch_count'],
278
                        $options['qos_options']['global']
279
                    );
280
                }
281
282
                $servers[$name] = $server;
283
            }
284
285
            return $servers;
286
        };
287
    }
288
}
289