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

JWKSource::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 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 getNodeDefinition(ArrayNodeDefinition $node)
54
    {
55
        $sourceNodeBuilder = $node
56
            ->children()
57
                ->arrayNode('keys')
58
                    ->useAttributeAsKey('name')
59
                    ->prototype('array')
60
                        ->performNoDeepMerging()
61
                        ->children();
62
        foreach ($this->getJWKSources() 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
        // TODO: Implement prepend() method.
74
    }
75
76
    /**
77
     * @return \SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource\JWKSourceInterface[]
78
     */
79
    private function getJWKSources()
80
    {
81
        if (null !== $this->jwk_sources) {
82
            return $this->jwk_sources;
83
        }
84
85
        // load bundled adapter factories
86
        $tempContainer = new ContainerBuilder();
87
        $loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config'));
88
        $loader->load('jwk_sources.xml');
89
90
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
91
        $jwk_sources = [];
92
        foreach (array_keys($services) as $id) {
93
            $factory = $tempContainer->get($id);
94
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
95
        }
96
97
        $this->jwk_sources = $jwk_sources;
98
99
        return $jwk_sources;
100
    }
101
}
102