Completed
Pull Request — master (#12)
by Thierry
02:37
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 Pimple\Container;
6
use Pimple\ServiceProviderInterface;
7
8
use ETNA\Silex\Provider\RabbitMQ\RabbitServiceProvider;
9
10
class RabbitConfig implements ServiceProviderInterface
11
{
12
    private $rmq_config;
13
14
    public function __construct($rmq_config = null)
15
    {
16
        $rmq_config = $rmq_config ?: [];
17
18
        $this->rmq_config['exchanges']   = isset($rmq_config['exchanges'])   ? $rmq_config['exchanges']   : [];
19
        $this->rmq_config['queues']      = isset($rmq_config['queues'])      ? $rmq_config['queues']      : [];
20
        $this->rmq_config['connections'] = isset($rmq_config['connections']) ? $rmq_config['connections'] : [];
21
    }
22
23
    /**
24
     *
25
     * @{inherit doc}
26
     */
27
    public function register(Container $app)
28
    {
29
        if (true !== isset($app["application_env"])) {
30
            throw new \Exception('$app["application_env"] is not set');
31
        }
32
33
        $rmq_url   = getenv('RABBITMQ_URL');
34
        $rmq_vhost = getenv('RABBITMQ_VHOST');
35
36
        if (false === $rmq_url) {
37
            throw new \Exception('RABBITMQ_URL is not defined');
38
        }
39
40
        if (false === $rmq_vhost) {
41
            throw new \Exception('RABBITMQ_VHOST is not defined');
42
        }
43
44
        $config        = parse_url($rmq_url);
45
        $rmq_producers = isset($app["rmq_producers"]) ? $app["rmq_producers"] : [];
46
        $rmq_consumers = isset($app["rmq_consumers"]) ? $app["rmq_consumers"] : [];
47
48
        foreach (["host", "port", "user", "pass"] as $config_key) {
49
            if (!isset($config[$config_key])) {
50
                throw new \Exception("Invalid RABBITMQ_URL : cannot resolve {$config_key}");
51
            }
52
        }
53
54
        $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...
55
            "default" => [
56
                "connection_opt" => [
57
                    "verify_peer" => true,
58
                    "heartbeat" => 30
59
                ]
60
            ]
61
        ];
62
63
        // Set la connection
64
        $rabbit_config = [
65
            "rabbit.connections" => array_replace_recursive(
66
                [
67
                    "default"  => [
68
                        "host"     => $config["host"],
69
                        "port"     => $config["port"],
70
                        "user"     => $config["user"],
71
                        "password" => $config["pass"],
72
                        "vhost"    => $rmq_vhost,
73
                        "ssl"      => "testing" !== $app["application_env"],
74
                        "connection_opt"  => [
75
                            "verify_peer" => true
76
                        ]
77
                    ]
78
                ],
79
                $this->rmq_config['connections']
80
            )
81
        ];
82
83
        // Ajoute les producers
84
        if (!empty($rmq_producers)) {
85
            $rabbit_config["rabbit.producers"] = $rmq_producers;
86
        }
87
88
        // Ajoute les consumers
89
        if (!empty($rmq_consumers)) {
90
            $rabbit_config["rabbit.consumers"] = $rmq_consumers;
91
        }
92
93
        $app["rmq.config"]    = $rabbit_config;
94
        $app['rmq.exchanges'] = $this->rmq_config['exchanges'];
95
        $app['rmq.queues']    = $this->rmq_config['queues'];
96
97
        $app->register(new RabbitServiceProvider(), $app["rmq.config"]);
98
    }
99
}
100