Completed
Push — master ( 89c85d...bbd4f0 )
by Florent
12:02
created

SpomkyLabsJoseBundleExtension::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
42
        $config = $processor->processConfiguration(
43
            $this->getConfiguration($configs, $container),
44
            $configs
45
        );
46
47
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
48
        $services = $this->getXmlFileToLoad($config);
49
        foreach ($services as $basename) {
50
            $loader->load(sprintf('%s.xml', $basename));
51
        }
52
53
        $this->initConfiguration($container, $config);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getConfiguration(array $config, ContainerBuilder $container)
60
    {
61
        return new Configuration($this->getAlias());
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getAlias()
68
    {
69
        return $this->alias;
70
    }
71
72
    /**
73
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
74
     * @param array                                                   $config
75
     */
76
    private function initConfiguration(ContainerBuilder $container, array $config)
77
    {
78
        if (true === $config['storage']['enabled']) {
79
            $container->setParameter($this->getAlias().'.jot.class', $config['storage']['class']);
80
            $container->setAlias($this->getAlias().'.jot.manager', $config['storage']['manager']);
81
        }
82
83
        $parameters = [
84
            'server_name',
85
            'compression_methods',
86
        ];
87
88
        foreach ($parameters as $parameter) {
89
            $container->setParameter($this->getAlias().'.'.$parameter, $config[$parameter]);
90
        }
91
    }
92
93
    /**
94
     * @param array $config
95
     *
96
     * @return string[]
97
     */
98
    private function getXmlFileToLoad(array $config)
99
    {
100
        $services = [
101
            'services',
102
            'compression_methods',
103
            'checkers',
104
            'payload_converters',
105
        ];
106
107
        if (true === $config['storage']['enabled']) {
108
            $services[] = 'jot';
109
        }
110
111
        return $services;
112
    }
113
}
114