AppExtension::applyConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
crap 2
1
<?php
2
namespace AppBundle\DependencyInjection;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
7
use Symfony\Component\DependencyInjection\Loader;
8
9
/**
10
 * App extension.
11
 * Sets aliases for storage services and logger
12
 *
13
 * @author Sergey Sadovoi <[email protected]>
14
 */
15
class AppExtension extends Extension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 1
    public function load(array $configs, ContainerBuilder $container)
21
    {
22 1
        $configuration = new Configuration();
23 1
        $config = $this->processConfiguration($configuration, $configs);
24
25 1
        $this->applyConfig('master', $container, $config['master']);
26 1
        $this->applyConfig('slave', $container, $config['slave']);
27
28 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
29 1
        $loader->load('services.yml');
30
31
        $this->setLogger($container);
32
    }
33
34
    /**
35
     * Sets the container parameters for selected type of the storage
36
     *
37
     * @param string           $type        Type of the storage
38
     * @param ContainerBuilder $container   App container
39
     * @param array            $config      Configuration values of the storage
40
     */
41 1
    protected function applyConfig($type, ContainerBuilder $container, $config)
42
    {
43
        // Set storage
44 1
        $storage = $config['storage'];
45 1
        $container->setAlias($type . '.storage', 'storage.' . $storage);
46
47
        // Set path
48 1
        $path = $config['path'];
49 1
        $container->setParameter($type . '.path', $path);
50
51
        // Set filters
52 1
        $filters = $config['filters'];
53 1
        $container->setParameter($type . '.filters', $filters);
54
55 1
        if ('slave' == $type) {
56 1
            $pathTpl = $config['path_tpl'];
57 1
            $container->setParameter($type . '.path_tpl', $pathTpl);
58 1
        }
59 1
    }
60
61
    /**
62
     * Sets alias for app logger
63
     *
64
     * @param ContainerBuilder $container   App container
65
     */
66
    protected function setLogger(ContainerBuilder $container)
67
    {
68
        $logger = $container->getParameter('app.logger');
69
        $container->setAlias('log.handler', $logger . '.log.handler');
70
    }
71
}
72