Completed
Push — v2 ( 7ffcf2...8a0112 )
by Faouzi
02:14
created

RabbitConfig::__construct()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 8.8571
cc 5
eloc 5
nc 16
nop 1
1
<?php
2
3
namespace ETNA\Silex\Provider\RabbitMQ;
4
5
use Silex\Application;
6
use Pimple\Container;
7
use Pimple\ServiceProviderInterface;
8
9
use ETNA\Silex\Provider\RabbitMQ\RabbitServiceProvider;
10
11
class RabbitConfig implements ServiceProviderInterface
12
{
13
    private $rmq_config;
14
15
    public function __construct($rmq_config = null)
16
    {
17
        $rmq_config = $rmq_config ?: [];
18
19
        $this->rmq_config['exchanges']   = isset($rmq_config['exchanges'])   ? $rmq_config['exchanges']   : [];
20
        $this->rmq_config['queues']      = isset($rmq_config['queues'])      ? $rmq_config['queues']      : [];
21
        $this->rmq_config['connections'] = isset($rmq_config['connections']) ? $rmq_config['connections'] : [];
22
    }
23
24
    /**
25
     *
26
     * @{inherit doc}
27
     */
28
    public function register(Container $app)
29
    {
30
        if (true !== isset($app["application_env"])) {
31
            throw new \Exception('$app["application_env"] is not set');
32
        }
33
34
        $rmq_url   = getenv('RABBITMQ_URL');
35
        $rmq_vhost = getenv('RABBITMQ_VHOST');
36
37
        if (false === $rmq_url) {
38
            throw new \Exception('RABBITMQ_URL is not defined');
39
        }
40
41
        if (false === $rmq_vhost) {
42
            throw new \Exception('RABBITMQ_VHOST is not defined');
43
        }
44
45
        $config        = parse_url($rmq_url);
46
        $rmq_producers = isset($app["rmq_producers"]) ? $app["rmq_producers"] : [];
47
        $rmq_consumers = isset($app["rmq_consumers"]) ? $app["rmq_consumers"] : [];
48
49
        foreach (["host", "port", "user", "pass"] as $config_key) {
50
            if (!isset($config[$config_key])) {
51
                throw new \Exception("Invalid RABBITMQ_URL : cannot resolve {$config_key}");
52
            }
53
        }
54
55
        $connections = [
0 ignored issues
show
Unused Code introduced by
$connections is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
            "default" => [
57
                "connection_opt" => [
58
                    "verify_peer" => true,
59
                    "heartbeat" => 30
60
                ]
61
            ]
62
        ];
63
64
        // Set la connection
65
        $rabbit_config = [
66
            "rabbit.connections" => array_replace_recursive(
67
                [
68
                    "default"  => [
69
                        "host"     => $config["host"],
70
                        "port"     => $config["port"],
71
                        "user"     => $config["user"],
72
                        "password" => $config["pass"],
73
                        "vhost"    => $rmq_vhost,
74
                        "ssl"      => "testing" !== $app["application_env"],
75
                        "connection_opt"  => [
76
                            "verify_peer" => true
77
                        ]
78
                    ]
79
                ],
80
                $this->rmq_config['connections']
81
            )
82
        ];
83
84
        // Ajoute les producers
85
        if (!empty($rmq_producers)) {
86
            $rabbit_config["rabbit.producers"] = $rmq_producers;
87
        }
88
89
        // Ajoute les consumers
90
        if (!empty($rmq_consumers)) {
91
            $rabbit_config["rabbit.consumers"] = $rmq_consumers;
92
        }
93
94
        $app["rmq.config"]    = $rabbit_config;
95
        $app['rmq.exchanges'] = $this->rmq_config['exchanges'];
96
        $app['rmq.queues']    = $this->rmq_config['queues'];
97
98
        $app->register(new RabbitServiceProvider(), $app["rmq.config"]);
99
    }
100
}
101