Completed
Push — master ( de1564...663938 )
by Florent
10:19
created

SpomkyLabsJoseBundleExtension::updateSources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 4 Features 0
Metric Value
c 7
b 4
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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 Assert\Assertion;
15
use SpomkyLabs\JoseBundle\DependencyInjection\Source\CheckerSource;
16
use SpomkyLabs\JoseBundle\DependencyInjection\Source\DecrypterSource;
17
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EasyJWTCreatorSource;
18
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EasyJWTLoaderSource;
19
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EncrypterSource;
20
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource;
21
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource;
22
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTCreatorSource;
23
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTLoaderSource;
24
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SignerSource;
25
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface;
26
use SpomkyLabs\JoseBundle\DependencyInjection\Source\VerifierSource;
27
use Symfony\Component\Config\Definition\Processor;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
31
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
32
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
33
34
final class SpomkyLabsJoseBundleExtension extends Extension implements PrependExtensionInterface
35
{
36
    /**
37
     * @var string
38
     */
39
    private $alias;
40
41
    /**
42
     * @var string
43
     */
44
    private $bundle_path;
45
46
    /**
47
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface[]
48
     */
49
    private $service_sources = [];
50
51
    /**
52
     * SpomkyLabsJoseBundleExtension constructor.
53
     *
54
     * @param string $alias
55
     * @param string $bundle_path
56
     */
57
    public function __construct($alias, $bundle_path)
58
    {
59
        $this->alias = $alias;
60
        $this->bundle_path = $bundle_path;
61
        $this->addDefaultSources();
62
    }
63
64
    /**
65
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface $source
66
     */
67
    public function addServiceSource(SourceInterface $source)
68
    {
69
        $name = $source->getName();
70
        Assertion::false(in_array($name, $this->service_sources), sprintf('The source "%s" is already set.', $name));
71
        $this->service_sources[$name] = $source;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function load(array $configs, ContainerBuilder $container)
78
    {
79
        $processor = new Processor();
80
81
        $config = $processor->processConfiguration(
82
            $this->getConfiguration($configs, $container),
83
            $configs
84
        );
85
86
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
87
        $services = ['services', 'compression_methods', 'checkers', 'signature_algorithms', 'encryption_algorithms', 'checkers'];
88
        foreach ($services as $basename) {
89
            $loader->load(sprintf('%s.xml', $basename));
90
        }
91
92
        $this->initConfiguration($container, $config);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getConfiguration(array $configs, ContainerBuilder $container)
99
    {
100
        return new Configuration($this->getAlias(), $this->service_sources);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getAlias()
107
    {
108
        return $this->alias;
109
    }
110
111
    /**
112
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
113
     * @param array                                                   $config
114
     */
115
    private function initConfiguration(ContainerBuilder $container, array $config)
116
    {
117
        foreach ($this->service_sources as $service_source) {
118
            foreach ($config[$service_source->getName()] as $name => $data) {
119
                $service_source->createService($name, $data, $container);
120
            }
121
        }
122
    }
123
124
    /**
125
     *
126
     */
127
    private function addDefaultSources()
128
    {
129
        $this->addServiceSource(new JWTCreatorSource());
130
        $this->addServiceSource(new JWTLoaderSource());
131
        $this->addServiceSource(new SignerSource());
132
        $this->addServiceSource(new VerifierSource());
133
        $this->addServiceSource(new EncrypterSource());
134
        $this->addServiceSource(new DecrypterSource());
135
        $this->addServiceSource(new CheckerSource());
136
        $this->addServiceSource(new JWKSource($this->bundle_path));
137
        $this->addServiceSource(new JWKSetSource($this->bundle_path));
138
        $this->addServiceSource(new EasyJWTCreatorSource());
139
        $this->addServiceSource(new EasyJWTLoaderSource());
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function prepend(ContainerBuilder $container)
146
    {
147
        $configs = $container->getExtensionConfig($this->getAlias());
148
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
149
150
        foreach ($this->service_sources as $service_source) {
151
            $result = $service_source->prepend($container, $config);
152
            if (null !== $result) {
153
                $container->prependExtensionConfig($this->getAlias(), $result);
154
            }
155
        }
156
    }
157
}
158