Completed
Push — v2 ( 3d201d...926424 )
by Faouzi
02:40
created

RabbitServiceProvider::loadConsumers()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 37
Code Lines 22

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 22
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
81
                $connections[$name] = $connection;
82
            }
83
84
            return $connections;
85
        };
86
    }
87
88
    /**
89
     * @param Container $app
90
     */
91
    private function loadProducers($app)
92
    {
93
        $app['rabbit.producer'] = function ($app) {
94
            if (!isset($app['rabbit.producers'])) {
95
                return;
96
            }
97
            $producers = [];
98
            foreach ($app['rabbit.producers'] as $name => $options) {
99
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
100
101
                $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...
102
                $producer->setExchangeOptions($options['exchange_options']);
103
104
                //this producer doesn't define a queue
105
                if (!isset($options['queue_options'])) {
106
                    $options['queue_options']['name'] = null;
107
                }
108
                $producer->setQueueOptions($options['queue_options']);
109
110
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
111
                    $producer->disableAutoSetupFabric();
112
                }
113
114
                $producers[$name] = $producer;
115
            }
116
117
            return $producers;
118
        };
119
    }
120
121
    /**
122
     * @param Container $app
123
     */
124
    private function loadConsumers($app)
125
    {
126
        $app['rabbit.consumer'] = function ($app) {
127
            if (!isset($app['rabbit.consumers'])) {
128
                return;
129
            }
130
131
            $consumers = [];
132
            foreach ($app['rabbit.consumers'] as $name => $options) {
133
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
134
                $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...
135
                $consumer->setExchangeOptions($options['exchange_options']);
136
                $consumer->setQueueOptions($options['queue_options']);
137
                $consumer->setCallback(array($app[$options['callback']], 'execute'));
138
139 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...
140
                    $consumer->setQosOptions(
141
                        $options['qos_options']['prefetch_size'],
142
                        $options['qos_options']['prefetch_count'],
143
                        $options['qos_options']['global']
144
                    );
145
                }
146
147
                if (array_key_exists('qos_options', $options)) {
148
                    $consumer->setIdleTimeout($options['idle_timeout']);
149
                }
150
151
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
152
                    $consumer->disableAutoSetupFabric();
153
                }
154
155
                $consumers[$name] = $consumer;
156
            }
157
158
            return $consumers;
159
        };
160
    }
161
162
    /**
163
     * @param Container $app
164
     */
165
    private function loadAnonymousConsumers($app)
166
    {
167
        $app['rabbit.anonymous_consumer'] = function ($app) {
168
            if (!isset($app['rabbit.anon_consumers'])) {
169
                return;
170
            }
171
172
            $consumers = [];
173
            foreach ($app['rabbit.anon_consumers'] as $name => $options) {
174
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
175
                $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...
176
                $consumer->setExchangeOptions($options['exchange_options']);
177
                $consumer->setCallback(array($options['callback'], 'execute'));
178
179
                $consumers[$name] = $consumer;
180
            }
181
182
            return $consumers;
183
        };
184
    }
185
186
    /**
187
     * @param Container $app
188
     */
189
    private function loadMultipleConsumers($app)
190
    {
191
        $app['rabbit.multiple_consumer'] = function ($app) {
192
            if (!isset($app['rabbit.multiple_consumers'])) {
193
                return;
194
            }
195
196
            $consumers = [];
197
            foreach ($app['rabbit.multiple_consumers'] as $name => $options) {
198
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
199
                $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...
200
                $consumer->setExchangeOptions($options['exchange_options']);
201
                $consumer->setQueues($options['queues']);
202
203 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...
204
                    $consumer->setQosOptions(
205
                        $options['qos_options']['prefetch_size'],
206
                        $options['qos_options']['prefetch_count'],
207
                        $options['qos_options']['global']
208
                    );
209
                }
210
211
                if (array_key_exists('qos_options', $options)) {
212
                    $consumer->setIdleTimeout($options['idle_timeout']);
213
                }
214
215
                if ((array_key_exists('auto_setup_fabric', $options)) && (!$options['auto_setup_fabric'])) {
216
                    $consumer->disableAutoSetupFabric();
217
                }
218
219
                $consumers[$name] = $consumer;
220
            }
221
222
            return $consumers;
223
        };
224
225
    }
226
227
    /**
228
     * @param Container $app
229
     */
230
    private function loadRpcClients($app)
231
    {
232
        $app['rabbit.rpc_client'] = function ($app) {
233
            if (!isset($app['rabbit.rpc_clients'])) {
234
                return;
235
            }
236
237
            $clients = [];
238
            foreach ($app['rabbit.rpc_clients'] as $name => $options) {
239
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
240
                $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...
241
242
                if (array_key_exists('expect_serialized_response', $options)) {
243
                    $client->initClient($options['expect_serialized_response']);
244
                }
245
246
                $clients[$name] = $client;
247
            }
248
249
            return $clients;
250
        };
251
    }
252
253
    /**
254
     * @param Container $app
255
     */
256
    private function loadRpcServers($app)
257
    {
258
        $app['rabbit.rpc_server'] = function ($app) {
259
            if (!isset($app['rabbit.rpc_servers'])) {
260
                return;
261
            }
262
263
            $servers = [];
264
            foreach ($app['rabbit.rpc_servers'] as $name => $options) {
265
                $connection = $this->getConnection($app, $options, $app['rabbit.connections']);
266
                $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...
267
                $server->initServer($name);
268
                $server->setCallback(array($options['callback'], 'execute'));
269
270 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...
271
                    $server->setQosOptions(
272
                        $options['qos_options']['prefetch_size'],
273
                        $options['qos_options']['prefetch_count'],
274
                        $options['qos_options']['global']
275
                    );
276
                }
277
278
                $servers[$name] = $server;
279
            }
280
281
            return $servers;
282
        };
283
    }
284
}
285