Completed
Push — master ( e6f9e9...ade0a3 )
by Peter
03:33
created

mergeDefaultConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.8571
cc 1
eloc 19
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2014, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\DependencyInjection;
10
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
15
16
class AnimeDbCacheTimeKeeperExtension extends Extension
17
{
18
    /**
19
     * @param array $configs
20
     * @param ContainerBuilder $container
21
     */
22 2
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 2
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
25 2
        $loader->load('parameters.yml');
26 2
        $loader->load('services.yml');
27
28 2
        $configuration = new Configuration(
29 2
            $container->getParameter('cache_time_keeper.driver.shmop.salt'),
30 2
            $container->getParameter('cache_time_keeper.driver.file.path')
31 2
        );
32 2
        $config = $this->processConfiguration($configuration, $configs);
33
34 2
        $config = $this->mergeBackwardCompatibilityConfig($config, $container);
35 2
        $config = $this->mergeDefaultConfig($config, $container);
36
37
        // configure drivers
38
        $container
39 2
            ->getDefinition('cache_time_keeper.driver.shmop')
40 2
            ->replaceArgument(0, $config['drivers']['shmop']['salt']);
41
        $container
42 2
            ->getDefinition('cache_time_keeper.driver.file')
43 2
            ->replaceArgument(0, $config['drivers']['file']['path']);
44
        $container
45 2
            ->getDefinition('cache_time_keeper.driver.memcache')
46 2
            ->replaceArgument(1, $config['drivers']['memcache']['prefix']);
47
48
        // configure memcache
49
        $memcache = $container
50 2
            ->getDefinition('cache_time_keeper.memcache')
51 2
            ->replaceArgument(0, $config['drivers']['memcache']['persistent_id']);
52 2
        foreach ($config['drivers']['memcache']['hosts'] as $host) {
53 1
            $memcache->addMethodCall('addServer', $host);
54 2
        }
55
56
        // add service aliases
57 2
        $container->setAlias(
58 2
            'cache_time_keeper.driver',
59 2
            $this->getRealServiceName($config['use_driver'])
60 2
        );
61 2
        $container->setAlias(
62 2
            'cache_time_keeper.driver.multi.fast',
63 2
            $this->getRealServiceName($config['drivers']['multi']['fast'])
64 2
        );
65 2
        $container->setAlias(
66 2
            'cache_time_keeper.driver.multi.slow',
67 2
            $this->getRealServiceName($config['drivers']['multi']['slow'])
68 2
        );
69 2
    }
70
71
    /**
72
     * @param string $name
73
     *
74
     * @return string
75
     */
76 2
    protected function getRealServiceName($name)
77
    {
78 2
        if (strpos($name, '.') === false) {
79 1
            return 'cache_time_keeper.driver.'.$name;
80
        }
81
82 1
        return $name;
83
    }
84
85
    /**
86
     * @param array $config
87
     * @param ContainerBuilder $container
88
     *
89
     * @return array
90
     */
91 2
    protected function mergeBackwardCompatibilityConfig(array $config, ContainerBuilder $container)
92
    {
93 2
        if ($container->hasParameter('cache_time_keeper.driver')) {
94 1
            $config['use_driver'] = $container->getParameter('cache_time_keeper.driver');
95 1
        }
96
97 2 View Code Duplication
        if (empty($config['drivers']['multi']['fast']) &&
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...
98 1
            $container->hasParameter('cache_time_keeper.driver.multi.fast')
99 2
        ) {
100 1
            $config['drivers']['multi']['fast'] = $container->getParameter('cache_time_keeper.driver.multi.fast');
101 1
        }
102
103 2 View Code Duplication
        if (empty($config['drivers']['multi']['slow']) &&
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...
104 1
            $container->hasParameter('cache_time_keeper.driver.multi.slow')
105 2
        ) {
106 1
            $config['drivers']['multi']['slow'] = $container->getParameter('cache_time_keeper.driver.multi.slow');
107 1
        }
108
109 2
        return $config;
110
    }
111
112
    /**
113
     * @param array $config
114
     * @param ContainerBuilder $container
115
     *
116
     * @return array
117
     */
118 2
    protected function mergeDefaultConfig(array $config, ContainerBuilder $container)
119
    {
120 2
        $config = array_merge([
121 2
            'use_driver' => 'file',
122 2
            'drivers' => [],
123 2
        ], $config);
124
125 2
        $config['drivers'] = array_merge([
126
            'multi' => [
127 2
                'fast' => 'shmop',
128 2
                'slow' => 'file',
129 2
            ],
130
            'shmop' => [
131 2
                'salt' => $container->getParameter('cache_time_keeper.driver.shmop.salt'),
132 2
            ],
133
            'file' => [
134 2
                'path' => $container->getParameter('cache_time_keeper.driver.file.path'),
135 2
            ],
136
            'memcache' => [
137 2
                'prefix' => 'cache_time_keeper_',
138 2
                'persistent_id' => 'cache_time_keeper',
139 2
                'hosts' => [],
140 2
            ],
141 2
        ], $config['drivers']);
142
143 2
        return $config;
144
    }
145
}
146