RabbitMQServiceProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 108
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 3
D register() 0 99 10
1
<?php
2
3
namespace ETNA\Silex\Provider\RabbitMQ;
4
5
use Silex\Application;
6
use Silex\ServiceProviderInterface;
7
8
/**
9
 *
10
 */
11
class RabbitMQServiceProvider implements ServiceProviderInterface
12
{
13
    public function boot(Application $app)
14
    {
15
        isset($app['amqp.exchanges.options']) || $app['amqp.exchanges.options'] = [];
16
        isset($app['amqp.queues.options'])    || $app['amqp.queues.options']    = [];
17
    }
18
19
    public function register(Application $app)
20
    {
21
        $app["amqp.chan"] = $app->share(
22
            function (Application $app) {
23
                return $app["amqp.chans"]["default"];
24
            }
25
        );
26
27
        $app["amqp.chans"] = $app->share(
28
            function (Application $app) {
29
                $chans = new \Pimple();
30
31
                foreach ($app['amqp.chans.options'] as $name => $options) {
32
                    $chans[$name] = $app->share(
33
                        function () use ($app, $name, $options) {
34
                            $amqp_args = [
35
                                $options["host"],
36
                                $options["port"],
37
                                $options["user"],
38
                                $options["password"],
39
                                $options["vhost"]
40
                            ];
41
42
                            if (isset($options["ssl"]) && $options["ssl"] === true) {
43
                                $amqp_class  = 'PhpAmqpLib\Connection\AMQPSSLConnection';
44
                                $amqp_args[] = [ 'verify_peer' => false, 'verify_peer_name' => false ];
45
                            } else {
46
                                $amqp_class = 'PhpAmqpLib\Connection\AMQPConnection';
47
                            }
48
49
                            $reflection = new \ReflectionClass($amqp_class);
50
                            $connection = $reflection->newInstanceArgs($amqp_args);
51
52
                            $channel = $connection->channel();
53
                            register_shutdown_function(function ($channel, $connection) use ($name) {
54
                                $channel->close();
55
                                $connection->close();
56
                            }, $channel, $connection);
57
                            return $channel;
58
                        }
59
                    );
60
                }
61
62
                return $chans;
63
            }
64
        );
65
66
        $app["amqp.exchanges"] = $app->share(
67
            function (Application $app) {
68
                $exchanges = new \Pimple();
69
70
                $exchanges["default"] = $app->share(
71
                    function () use ($app) {
72
                        return new Exchange("", $app["amqp.chan"], []);
73
                    }
74
                );
75
76
                foreach ($app['amqp.exchanges.options'] as $name => $options) {
77
                    if ($name == 'default') {
78
                        throw new \Exception("'default' is a reserved Exchange. You can't override it.");
79
                    }
80
                    $exchanges[$name] = $app->share(
81
                        function () use ($app, $name, $options) {
82
                            $channel = (isset($options["channel"])) ? $options["channel"] : "default";
83
                            return new Exchange($name, $app["amqp.chans"][$channel], $options);
84
                        }
85
                    );
86
                }
87
88
                return $exchanges;
89
            }
90
        );
91
92
        $app["amqp.queues"] = $app->share(
93
            function (Application $app) {
94
                $queues = new \Pimple();
95
96
                $queues[""] = $app->share(
97
                    function () use ($app) {
98
                        return new Queue("", $app["amqp.exchanges"]["default"], $app["amqp.exchanges"]["default"]->getChannel());
0 ignored issues
show
Bug introduced by
The call to Queue::__construct() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
99
                    }
100
                );
101
102
                foreach ($app['amqp.queues.options'] as $name => $options) {
103
                    if ($name == "") {
104
                        throw new \Exception("Unamed queue is a reserved Queue. You can't override it.");
105
                    }
106
                    $queues[$name] = $app->share(
107
                        function () use ($app, $name, $options) {
108
                            $exchange = (isset($options["exchange"])) ? $options["exchange"] : "default";
109
                            return new Queue($name, $app["amqp.exchanges"][$exchange], $app["amqp.exchanges"][$exchange]->getChannel(), $options);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
                        }
111
                    );
112
                }
113
114
                return $queues;
115
            }
116
        );
117
    }
118
}
119