Completed
Push — master ( 0f3db4...6e7c4d )
by Guillaume
16:16
created

MaikuroDistributedConfigurationExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the distributed-configuration-bundle package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Maikuro\DistributedConfigurationBundle\DependencyInjection;
17
18
use Symfony\Component\Config\Definition\Processor;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
24
25
class MaikuroDistributedConfigurationExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $processor = new Processor();
33
        $configuration = new Configuration();
34
        $config = $processor->processConfiguration($configuration, $configs);
35
36
        $this->loadStoreHandler($container, $config);
37
    }
38
39
    /**
40
     * loadStoreHandler.
41
     *
42
     * @param ContainerBuilder $container
43
     * @param array            $config
44
     */
45
    private function loadStoreHandler(ContainerBuilder $container, array $config)
46
    {
47
        $storeHandlerDef = new Definition('Maikuro\DistributedConfigurationBundle\Handler\StoreHandler');
48
        $storeDef = $this->loadStore($container, $config['store']);
49
        if (null === $storeDef) {
50
            throw new InvalidArgumentException('No store found');
51
        }
52
        $storeHandlerDef->addArgument($storeDef);
53
        //Enabled caching
54
        if ($config['cache']['enabled']) {
55
            if (null === $config['cache']['service_id']) {
56
                throw new InvalidArgumentException(
57
                    'You need to install DoctrineCacheBundle and configure it to use cache feature'
58
                );
59
            }
60
            $cachingDecoratorDef = new Definition('Webmozart\KeyValueStore\Decorator\CachingDecorator');
61
            $cachingDecoratorDef->addArgument($storeDef, new Reference($config['cache']['service_id']));
0 ignored issues
show
Unused Code introduced by
The call to Definition::addArgument() has too many arguments starting with new \Symfony\Component\D...'cache']['service_id']).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
            $storeHandlerDef->replaceArgument(0, $cachingDecoratorDef);
63
        }
64
        $container->setDefinition('gundan_distributed_configuration.store_handler', $storeHandlerDef);
65
    }
66
67
    private function loadStore(ContainerBuilder $container, array $config)
68
    {
69
        switch ($config['type']) {
70
            case 'redis':
71
                return $this->loadRedisStore($container, $config);
72
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
73
            case 'predis':
74
                return $this->loadPredisStore($container, $config);
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
            case 'dbal':
77
                return $this->loadDbalStore($container, $config);
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
            case 'json':
80
                return $this->loadJsonStore($container, $config);
81
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
82
            default:
83
                return;
84
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
85
        }
86
    }
87
88
    private function loadRedisStore(ContainerBuilder $container, array $config)
89
    {
90
        $redisStoreDef = new Definition('Webmozart\KeyValueStore\PhpRedisStore');
91
92
        if (isset($config['store']['connection_id'])) {
93
            $redisStoreDef->addArgument(new Reference($config['connection_id']));
94
        } else {
95
            $host = $config['host'];
96
            $port = $config['port'];
97
            $connId = 'gundan_distributed_configuration.redis.connection';
98
            $connDef = new Definition('Redis');
99
            $connParams = array($host, $port);
100
            if (isset($config['timeout'])) {
101
                $connParams[] = $config['timeout'];
102
            }
103
            $connMethod = 'connect';
104
            if (isset($config['persistent']) && $config['persistent']) {
105
                $connMethod = 'pconnect';
106
            }
107
            $connDef->setPublic(false);
108
            $connDef->addMethodCall($connMethod, $connParams);
109 View Code Duplication
            if (isset($config['password'])) {
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...
110
                $password = $config['password'];
111
                $connDef->addMethodCall('auth', array($password));
112
            }
113 View Code Duplication
            if (isset($config['database'])) {
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...
114
                $database = (int) $config['database'];
115
                $connDef->addMethodCall('select', array($database));
116
            }
117
            $container->setDefinition($connId, $connDef);
118
119
            $redisStoreDef->addArgument(new Reference($connId));
120
        }
121
122
        return $redisStoreDef;
123
    }
124
125
    private function loadPredisStore(ContainerBuilder $container, array $config)
126
    {
127
        $predisStoreDef = new Definition('Webmozart\KeyValueStore\PredisStore');
128
129
        if (isset($config['client_id'])) {
130
            $predisStoreDef->addArgument(new Reference($config['connection_id']));
131
        } else {
132
            $parameters = array(
133
                'scheme' => $config['scheme'],
134
                'host' => $config['host'],
135
                'port' => $config['port'],
136
            );
137
            if ($config['password']) {
138
                $parameters['password'] = $config['password'];
139
            }
140
            if ($config['timeout']) {
141
                $parameters['timeout'] = $config['timeout'];
142
            }
143
            if ($config['database']) {
144
                $parameters['database'] = $config['database'];
145
            }
146
            $options = null;
147
            if (isset($config['options'])) {
148
                $options = $config['options'];
149
            }
150
            $clientId = 'gundan_distributed_configuration.predis.client';
151
            $clientDef = new Definition('Predis\Client');
152
            $clientDef->addArgument($parameters);
153
            $clientDef->addArgument($options);
154
            $clientDef->setPublic(false);
155
            $container->setDefinition($clientId, $clientDef);
156
157
            $predisStoreDef->addArgument(new Reference($clientId));
158
        }
159
160
        return $predisStoreDef;
161
    }
162
163
    private function loadJsonStore(ContainerBuilder $container, array $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        $jsonStoreDef = new Definition('Webmozart\KeyValueStore\JsonFileStore');
166
        $jsonStoreDef->addArgument($config['json']['path']);
167
168
        return $jsonStoreDef;
169
    }
170
171
    private function loadDbalStore(ContainerBuilder $container, array $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        $dbalStoreDef = new Definition('Webmozart\KeyValueStore\DbalStore');
174
175
        if (!isset($config['connection_id'])) {
176
            throw new InvalidArgumentException('No dbal connection configured');
177
        }
178
179
        $dbalStoreDef->addArgument(new Reference($config['connection_id']), $config['table_name']);
0 ignored issues
show
Unused Code introduced by
The call to Definition::addArgument() has too many arguments starting with $config['table_name'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
180
181
        return $dbalStoreDef;
182
    }
183
}
184