MaikuroDistributedConfigurationExtension   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 160
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 7
dl 8
loc 160
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 8 1
B loadStore() 0 20 5
C loadRedisStore() 8 36 7
B loadPredisStore() 0 37 6
A loadJsonStore() 0 7 1
A loadDbalStore() 0 12 2
B loadStoreHandler() 0 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
                [$storeDef, new Reference($config['cache']['service_id'])]
62
            );
63
            $storeHandlerDef->replaceArgument(0, $cachingDecoratorDef);
64
        }
65
        $container->setDefinition('maikuro_distributed_configuration.store_handler', $storeHandlerDef);
66
    }
67
68
    private function loadStore(ContainerBuilder $container, array $config)
69
    {
70
        switch ($config['type']) {
71
            case 'redis':
72
                return $this->loadRedisStore($container, $config);
73
                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...
74
            case 'predis':
75
                return $this->loadPredisStore($container, $config);
76
                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...
77
            case 'dbal':
78
                return $this->loadDbalStore($container, $config);
79
                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...
80
            case 'json':
81
                return $this->loadJsonStore($container, $config);
82
                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...
83
            default:
84
                return;
85
                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...
86
        }
87
    }
88
89
    private function loadRedisStore(ContainerBuilder $container, array $config)
90
    {
91
        $redisStoreDef = new Definition('Webmozart\KeyValueStore\PhpRedisStore');
92
93
        if (isset($config['store']['connection_id'])) {
94
            $redisStoreDef->addArgument(new Reference($config['connection_id']));
95
        } else {
96
            $host = $config['host'];
97
            $port = $config['port'];
98
            $connId = 'maikuro_distributed_configuration.redis.connection';
99
            $connDef = new Definition('Redis');
100
            $connParams = array($host, $port);
101
            if (isset($config['timeout'])) {
102
                $connParams[] = $config['timeout'];
103
            }
104
            $connMethod = 'connect';
105
            if (isset($config['persistent']) && $config['persistent']) {
106
                $connMethod = 'pconnect';
107
            }
108
            $connDef->setPublic(false);
109
            $connDef->addMethodCall($connMethod, $connParams);
110 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...
111
                $password = $config['password'];
112
                $connDef->addMethodCall('auth', array($password));
113
            }
114 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...
115
                $database = (int) $config['database'];
116
                $connDef->addMethodCall('select', array($database));
117
            }
118
            $container->setDefinition($connId, $connDef);
119
120
            $redisStoreDef->addArgument(new Reference($connId));
121
        }
122
123
        return $redisStoreDef;
124
    }
125
126
    private function loadPredisStore(ContainerBuilder $container, array $config)
127
    {
128
        $predisStoreDef = new Definition('Webmozart\KeyValueStore\PredisStore');
129
130
        if (isset($config['client_id'])) {
131
            $predisStoreDef->addArgument(new Reference($config['connection_id']));
132
        } else {
133
            $parameters = array(
134
                'scheme' => $config['scheme'],
135
                'host' => $config['host'],
136
                'port' => $config['port'],
137
            );
138
            if ($config['password']) {
139
                $parameters['password'] = $config['password'];
140
            }
141
            if ($config['timeout']) {
142
                $parameters['timeout'] = $config['timeout'];
143
            }
144
            if ($config['database']) {
145
                $parameters['database'] = $config['database'];
146
            }
147
            $options = null;
148
            if (isset($config['options'])) {
149
                $options = $config['options'];
150
            }
151
            $clientId = 'maikuro_distributed_configuration.predis.client';
152
            $clientDef = new Definition('Predis\Client');
153
            $clientDef->addArgument($parameters);
154
            $clientDef->addArgument($options);
155
            $clientDef->setPublic(false);
156
            $container->setDefinition($clientId, $clientDef);
157
158
            $predisStoreDef->addArgument(new Reference($clientId));
159
        }
160
161
        return $predisStoreDef;
162
    }
163
164
    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...
165
    {
166
        $jsonStoreDef = new Definition('Webmozart\KeyValueStore\JsonFileStore');
167
        $jsonStoreDef->addArgument($config['json']['path']);
168
169
        return $jsonStoreDef;
170
    }
171
172
    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...
173
    {
174
        $dbalStoreDef = new Definition('Webmozart\KeyValueStore\DbalStore');
175
176
        if (!isset($config['connection_id'])) {
177
            throw new InvalidArgumentException('No dbal connection configured');
178
        }
179
180
        $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...
181
182
        return $dbalStoreDef;
183
    }
184
}
185