Completed
Push — develop ( 4c84f9...64a224 )
by Florent
05:18
created

SpomkyLabsJoseBundleExtension::createDecrypter()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 2
eloc 11
nc 1
nop 3
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\Definition;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
final class SpomkyLabsJoseBundleExtension extends Extension
23
{
24
    /**
25
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[]
26
     */
27
    private $jwk_sources;
28
29
    /**
30
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[]
31
     */
32
    private $jwk_set_sources;
33
34
    /**
35
     * @var string
36
     */
37
    private $alias;
38
39
    /**
40
     * @param string $alias
41
     */
42
    public function __construct($alias)
43
    {
44
        $this->alias = $alias;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function load(array $configs, ContainerBuilder $container)
51
    {
52
        $processor = new Processor();
53
54
        $config = $processor->processConfiguration(
55
            $this->getConfiguration($configs, $container),
56
            $configs
57
        );
58
59
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
60
        $services = $this->getXmlFileToLoad();
61
        foreach ($services as $basename) {
62
            $loader->load(sprintf('%s.xml', $basename));
63
        }
64
65
        $this->initConfiguration($container, $config);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getConfiguration(array $configs, ContainerBuilder $container)
72
    {
73
        $jwk_sources = $this->createJWKSources();
74
        $jwk_set_sources = $this->createJWKSetSources();
75
76
        return new Configuration($this->getAlias(), $jwk_sources, $jwk_set_sources);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getAlias()
83
    {
84
        return $this->alias;
85
    }
86
87
    /**
88
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
89
     * @param array                                                   $config
90
     */
91
    private function initConfiguration(ContainerBuilder $container, array $config)
92
    {
93
        foreach ($config['keys'] as $name => $key) {
94
            $this->createJWK($name, $key, $container, $this->jwk_sources);
95
        }
96
97
        foreach ($config['key_sets'] as $name => $key_set) {
98
            $this->createJWKSet($name, $key_set, $container, $this->jwk_set_sources);
99
        }
100
101
        foreach ($config['encrypters'] as $name => $encrypter) {
102
            $this->createEncrypter($name, $encrypter, $container);
103
        }
104
105
        foreach ($config['decrypters'] as $name => $decrypter) {
106
            $this->createDecrypter($name, $decrypter, $container);
107
        }
108
109
        foreach ($config['signers'] as $name => $signer) {
110
            $this->createSigner($name, $signer, $container);
111
        }
112
113
        foreach ($config['verifiers'] as $name => $verifier) {
114
            $this->createVerifier($name, $verifier, $container);
115
        }
116
    }
117
118
    /**
119
     * @param string                                                                       $name
120
     * @param array                                                                        $config
121
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder                      $container
122
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources
123
     */
124 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...
125
    {
126
        foreach ($config as $key => $adapter) {
127
            if (array_key_exists($key, $jwk_set_sources)) {
128
                $service_id = sprintf('jose.key_set.%s', $name);
129
                $jwk_set_sources[$key]->create($container, $service_id, $adapter);
130
131
                return;
132
            }
133
        }
134
        throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name));
135
    }
136
137
    /**
138
     * @param string                                                                    $name
139
     * @param array                                                                     $config
140
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder                   $container
141
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources
142
     */
143 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...
144
    {
145
        foreach ($config as $key => $adapter) {
146
            if (array_key_exists($key, $jwk_sources)) {
147
                $service_id = sprintf('jose.key.%s', $name);
148
                $jwk_sources[$key]->create($container, $service_id, $adapter);
149
150
                return;
151
            }
152
        }
153
        throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name));
154
    }
155
156
    /**
157
     * @param string                                                  $name
158
     * @param array                                                   $config
159
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
160
     */
161 View Code Duplication
    private function createEncrypter($name, array $config, ContainerBuilder $container)
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...
162
    {
163
        $service_id = sprintf('jose.encrypter.%s', $name);
164
        $definition = new Definition('Jose\Encrypter');
165
        $definition->setFactory([
166
            new Reference('jose.factory.service'),
167
            'createEncrypter',
168
        ]);
169
        $definition->setArguments([
170
            $config['algorithms'],
171
            $config['compression_methods'],
172
            null === $config['logger'] ? null : new Reference($config['logger']),
173
        ]);
174
175
        $container->setDefinition($service_id, $definition);
176
        
177
        if (true === $config['create_decrypter']) {
178
            $this->createDecrypter($name, $config, $container);
179
        }
180
    }
181
182
    /**
183
     * @param string                                                  $name
184
     * @param array                                                   $config
185
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
186
     */
187 View Code Duplication
    private function createDecrypter($name, array $config, ContainerBuilder $container)
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...
188
    {
189
        $service_id = sprintf('jose.decrypter.%s', $name);
190
        $definition = new Definition('Jose\Decrypter');
191
        $definition->setFactory([
192
            new Reference('jose.factory.service'),
193
            'createDecrypter',
194
        ]);
195
        $definition->setArguments([
196
            $config['algorithms'],
197
            $config['compression_methods'],
198
            null === $config['logger'] ? null : new Reference($config['logger']),
199
        ]);
200
        
201
        $container->setDefinition($service_id, $definition);
202
    }
203
204
    /**
205
     * @param string                                                  $name
206
     * @param array                                                   $config
207
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
208
     */
209 View Code Duplication
    private function createSigner($name, array $config, ContainerBuilder $container)
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...
210
    {
211
        $service_id = sprintf('jose.signer.%s', $name);
212
        $definition = new Definition('Jose\Signer');
213
        $definition->setFactory([
214
            new Reference('jose.factory.service'),
215
            'createSigner',
216
        ]);
217
        $definition->setArguments([
218
            $config['algorithms'],
219
            null === $config['logger'] ? null : new Reference($config['logger']),
220
        ]);
221
222
        $container->setDefinition($service_id, $definition);
223
        
224
        if (true === $config['create_verifier']) {
225
            $this->createVerifier($name, $config, $container);
226
        }
227
    }
228
229
    /**
230
     * @param string                                                  $name
231
     * @param array                                                   $config
232
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
233
     */
234 View Code Duplication
    private function createVerifier($name, array $config, ContainerBuilder $container)
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...
235
    {
236
        $service_id = sprintf('jose.verifier.%s', $name);
237
        $definition = new Definition('Jose\Verifier');
238
        $definition->setFactory([
239
            new Reference('jose.factory.service'),
240
            'createVerifier',
241
        ]);
242
        $definition->setArguments([
243
            $config['algorithms'],
244
            null === $config['logger'] ? null : new Reference($config['logger']),
245
        ]);
246
        
247
        $container->setDefinition($service_id, $definition);
248
    }
249
250
    /**
251
     * @return string[]
252
     */
253
    private function getXmlFileToLoad()
254
    {
255
        $services = [
256
            'services',
257
            'compression_methods',
258
            'checkers',
259
            'signature_algorithms',
260
            'encryption_algorithms',
261
            'checkers',
262
        ];
263
264
        return $services;
265
    }
266
267 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...
268
    {
269
        if (null !== $this->jwk_sources) {
270
            return $this->jwk_sources;
271
        }
272
273
        // load bundled adapter factories
274
        $tempContainer = new ContainerBuilder();
275
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
276
        $loader->load('jwk_sources.xml');
277
278
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
279
        $jwk_sources = [];
280
        foreach (array_keys($services) as $id) {
281
            $factory = $tempContainer->get($id);
282
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
283
        }
284
285
        return $this->jwk_sources = $jwk_sources;
286
    }
287
288 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...
289
    {
290
        if (null !== $this->jwk_set_sources) {
291
            return $this->jwk_set_sources;
292
        }
293
294
        // load bundled adapter factories
295
        $tempContainer = new ContainerBuilder();
296
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
297
        $loader->load('jwk_set_sources.xml');
298
299
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_set_source');
300
        $jwk_set_sources = [];
301
        foreach (array_keys($services) as $id) {
302
            $factory = $tempContainer->get($id);
303
            $jwk_set_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
304
        }
305
306
        return $this->jwk_set_sources = $jwk_set_sources;
307
    }
308
}
309