Completed
Push — master ( ce82a2...2c4ca0 )
by Florent
02:09
created

SpomkyLabsJoseBundleExtension::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 15
Bugs 5 Features 1
Metric Value
c 15
b 5
f 1
dl 0
loc 15
rs 9.4286
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Processor;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
final class SpomkyLabsJoseBundleExtension extends Extension
21
{
22
    /**
23
     * @var string
24
     */
25
    private $alias;
26
27
    /**
28
     * @param string $alias
29
     */
30
    public function __construct($alias)
31
    {
32
        $this->alias = $alias;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function load(array $configs, ContainerBuilder $container)
39
    {
40
        $processor = new Processor();
41
        $configuration = new Configuration($this->getAlias());
42
43
        $config = $processor->processConfiguration($configuration, $configs);
44
45
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
        $services = $this->getXmlFileToLoad($config);
47
        foreach ($services as $basename) {
48
            $loader->load(sprintf('%s.xml', $basename));
49
        }
50
51
        $this->initConfiguration($container, $config);
52
    }
53
54
    /**
55
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
56
     * @param array                                                   $config
57
     */
58
    private function initConfiguration(ContainerBuilder $container, array $config)
59
    {
60
        if (true === $config['storage']['enabled']) {
61
            $container->setParameter($this->getAlias().'.jot.class', $config['storage']['class']);
62
            $container->setAlias($this->getAlias().'.jot.manager', $config['storage']['manager']);
63
        }
64
65
        $parameters = [
66
            'server_name',
67
            'compression_methods',
68
        ];
69
70
        foreach ($parameters as $parameter) {
71
            $container->setParameter($this->getAlias().'.'.$parameter, $config[$parameter]);
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getAlias()
79
    {
80
        return $this->alias;
81
    }
82
83
    /**
84
     * @param array $config
85
     *
86
     * @return string[]
87
     */
88
    private function getXmlFileToLoad(array $config)
89
    {
90
        $services = [
91
            'services',
92
            'compression_methods',
93
            'checkers',
94
            'payload_converters',
95
        ];
96
97
        if (true === $config['storage']['enabled']) {
98
            $services[] = 'jot';
99
        }
100
101
        return $services;
102
    }
103
}
104