Completed
Push — master ( 755e9e...9e5cc9 )
by Florent
01:58
created

JWKSetSource::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\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 addConfigurationSection(ArrayNodeDefinition $node)
54
    {
55
        $sourceNodeBuilder = $node
56
            ->fixXmlConfig('source')
57
            ->children()
58
            ->arrayNode('key_sets')
59
            ->useAttributeAsKey('name')
60
            ->prototype('array')
61
            ->performNoDeepMerging()
62
            ->children();
63
        foreach ($this->getJWKSetSources() as $name => $source) {
64
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
65
            $source->addConfiguration($sourceNode);
66
        }
67
    }
68
69
    /**
70
     * @return array|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
71
     */
72
    private function getJWKSetSources()
73
    {
74
        if (null !== $this->jwkset_sources) {
75
            return $this->jwkset_sources;
76
        }
77
78
        // load bundled adapter factories
79
        $tempContainer = new ContainerBuilder();
80
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config'));
81
        $loader->load('jwkset_sources.xml');
82
83
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_set_source');
84
        $jwkset_sources = [];
85
        foreach (array_keys($services) as $id) {
86
            $factory = $tempContainer->get($id);
87
            $jwkset_sources[str_replace('-', '_', $factory->getKeySet())] = $factory;
88
        }
89
90
        return $this->jwkset_sources = $jwkset_sources;
91
    }
92
}
93