Completed
Push — master ( a6e833...2ffd36 )
by Florent
02:08
created

JWKSetSource::addConfigurationSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 12
nc 2
nop 1
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\Source;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
19
final class JWKSetSource implements SourceInterface
20
{
21
    /**
22
     * @var null|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
23
     */
24
    private $jwkset_sources = null;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getName()
30
    {
31
        return 'key_sets';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function createService($name, array $config, ContainerBuilder $container)
38
    {
39
        foreach ($config as $key => $adapter) {
40
            if (array_key_exists($key, $this->getJWKSetSources())) {
41
                $service_id = sprintf('jose.key_set.%s', $name);
42
                $this->getJWKSetSources()[$key]->create($container, $service_id, $adapter);
43
44
                return;
45
            }
46
        }
47
        throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name));
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getNodeDefinition(ArrayNodeDefinition $node)
54
    {
55
        $sourceNodeBuilder = $node
56
            ->children()
57
                ->arrayNode('key_sets')
58
                    ->useAttributeAsKey('name')
59
                    ->prototype('array')
60
                        ->performNoDeepMerging()
61
                        ->children();
62
        foreach ($this->getJWKSetSources() as $name => $source) {
63
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
64
            $source->addConfiguration($sourceNode);
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function prepend(ContainerBuilder $container, array $config)
72
    {
73
    }
74
75
    /**
76
     * @return array|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
77
     */
78
    private function getJWKSetSources()
79
    {
80
        if (null !== $this->jwkset_sources) {
81
            return $this->jwkset_sources;
82
        }
83
84
        // load bundled adapter factories
85
        $tempContainer = new ContainerBuilder();
86
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config'));
87
        $loader->load('jwkset_sources.xml');
88
89
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_set_source');
90
        $jwkset_sources = [];
91
        foreach (array_keys($services) as $id) {
92
            $factory = $tempContainer->get($id);
93
            $jwkset_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
94
        }
95
96
        return $this->jwkset_sources = $jwkset_sources;
97
    }
98
}
99