Completed
Push — master ( c7a8ed...7c53a3 )
by Florent
08:28
created

SpomkyLabsJoseBundleExtension::initConfiguration()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 8
nop 2
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 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 \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[]
24
     */
25
    private $jwk_sources;
26
27
    /**
28
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[]
29
     */
30
    private $jwk_set_sources;
31
32
    /**
33
     * @var string
34
     */
35
    private $alias;
36
37
    /**
38
     * @param string $alias
39
     */
40
    public function __construct($alias)
41
    {
42
        $this->alias = $alias;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function load(array $configs, ContainerBuilder $container)
49
    {
50
        $processor = new Processor();
51
52
        $config = $processor->processConfiguration(
53
            $this->getConfiguration($configs, $container),
54
            $configs
55
        );
56
57
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
58
        $services = $this->getXmlFileToLoad();
59
        foreach ($services as $basename) {
60
            $loader->load(sprintf('%s.xml', $basename));
61
        }
62
63
        $this->initConfiguration($container, $config);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getConfiguration(array $configs, ContainerBuilder $container)
70
    {
71
        $jwk_sources = $this->createJWKSources();
72
        $jwk_set_sources = $this->createJWKSetSources();
73
        
74
        return new Configuration($this->getAlias(), $jwk_sources, $jwk_set_sources);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getAlias()
81
    {
82
        return $this->alias;
83
    }
84
85
    /**
86
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
87
     * @param array                                                   $config
88
     */
89
    private function initConfiguration(ContainerBuilder $container, array $config)
90
    {
91
        $parameters = [
92
            'compression_methods',
93
        ];
94
95
        foreach ($parameters as $parameter) {
96
            $container->setParameter($this->getAlias().'.'.$parameter, $config[$parameter]);
97
        }
98
99
        foreach ($config['keys'] as $name => $key) {
100
            $this->createJWK($name, $key, $container, $this->jwk_sources);
101
        }
102
103
        foreach ($config['key_sets'] as $name => $key_set) {
104
            $this->createJWKSet($name, $key_set, $container, $this->jwk_set_sources);
105
        }
106
    }
107
108
    /**
109
     * @param string                                                                       $name
110
     * @param array                                                                        $config
111
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder                      $container
112
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources
113
     */
114 View Code Duplication
    private function createJWKSet($name, array $config, ContainerBuilder $container, array $jwk_set_sources)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        foreach ($config as $key => $adapter) {
117
            if (array_key_exists($key, $jwk_set_sources)) {
118
                $service_id = sprintf('jose.key_set.%s', $name);
119
                $jwk_set_sources[$key]->create($container, $service_id, $adapter);
120
121
                return;
122
            }
123
        }
124
        throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name));
125
    }
126
127
    /**
128
     * @param string                                                                    $name
129
     * @param array                                                                     $config
130
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder                   $container
131
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
132
     */
133 View Code Duplication
    private function createJWK($name, array $config, ContainerBuilder $container, array $jwk_sources)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
134
    {
135
        foreach ($config as $key => $adapter) {
136
            if (array_key_exists($key, $jwk_sources)) {
137
                $service_id = sprintf('jose.key.%s', $name);
138
                $jwk_sources[$key]->create($container, $service_id, $adapter);
139
140
                return;
141
            }
142
        }
143
        throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name));
144
    }
145
146
    /**
147
     * @return string[]
148
     */
149
    private function getXmlFileToLoad()
150
    {
151
        $services = [
152
            'services',
153
            'compression_methods',
154
            'checkers',
155
        ];
156
157
        return $services;
158
    }
159
160 View Code Duplication
    private function createJWKSources()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
161
    {
162
        if (null !== $this->jwk_sources) {
163
            return $this->jwk_sources;
164
        }
165
166
        // load bundled adapter factories
167
        $tempContainer = new ContainerBuilder();
168
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
169
        $loader->load('jwk_sources.xml');
170
171
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
172
        $jwk_sources = [];
173
        foreach (array_keys($services) as $id) {
174
            $factory = $tempContainer->get($id);
175
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
176
        }
177
178
        return $this->jwk_sources = $jwk_sources;
179
    }
180
181 View Code Duplication
    private function createJWKSetSources()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
182
    {
183
        if (null !== $this->jwk_set_sources) {
184
            return $this->jwk_set_sources;
185
        }
186
187
        // load bundled adapter factories
188
        $tempContainer = new ContainerBuilder();
189
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
190
        $loader->load('jwk_set_sources.xml');
191
192
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_set_source');
193
        $jwk_set_sources = [];
194
        foreach (array_keys($services) as $id) {
195
            $factory = $tempContainer->get($id);
196
            $jwk_set_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
197
        }
198
199
        return $this->jwk_set_sources = $jwk_set_sources;
200
    }
201
}
202