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

JWKSource::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 JWKSource implements SourceInterface
20
{
21
    /**
22
     * @var null|\SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource\JWKSourceInterface[]
23
     */
24
    private $jwk_sources = null;
25
    
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getName()
30
    {
31
        return 'keys';
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->getJWKSources())) {
41
                $service_id = sprintf('jose.key.%s', $name);
42
                $this->getJWKSources()[$key]->create($container, $service_id, $adapter);
43
44
                return;
45
            }
46
        }
47
        throw new \LogicException(sprintf('The JWK 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('keys')
59
            ->useAttributeAsKey('name')
60
            ->prototype('array')
61
            ->performNoDeepMerging()
62
            ->children();
63
        foreach ($this->getJWKSources() as $name => $source) {
64
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
65
            $source->addConfiguration($sourceNode);
66
        }
67
    }
68
69
    /**
70
     * @return \SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource\JWKSourceInterface[]
71
     */
72
    private function getJWKSources()
73
    {
74
75
        if (null !== $this->jwk_sources) {
76
            return $this->jwk_sources;
77
        }
78
79
        // load bundled adapter factories
80
        $tempContainer = new ContainerBuilder();
81
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config'));
82
        $loader->load('jwk_sources.xml');
83
84
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
85
        $jwk_sources = [];
86
        foreach (array_keys($services) as $id) {
87
            $factory = $tempContainer->get($id);
88
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
89
        }
90
91
        $this->jwk_sources = $jwk_sources;
92
        
93
        return $jwk_sources;
94
    }
95
}
96