Completed
Push — master ( c6f9e0...8b6623 )
by Florent
02:41
created

JWKSource   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 11
c 5
b 2
f 0
lcom 1
cbo 6
dl 0
loc 97
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A createService() 0 12 3
A getNodeDefinition() 0 14 2
A prepend() 0 3 1
A getJWKSources() 0 22 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\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
     * @var string
28
     */
29
    private $bundle_path = null;
30
31
    /**
32
     * JWKSetSource constructor.
33
     *
34
     * @param string $bundle_path
35
     */
36
    public function __construct($bundle_path)
37
    {
38
        $this->bundle_path = $bundle_path;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return 'keys';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function createService($name, array $config, ContainerBuilder $container)
53
    {
54
        foreach ($config as $key => $adapter) {
55
            if (array_key_exists($key, $this->getJWKSources())) {
56
                $service_id = sprintf('jose.key.%s', $name);
57
                $this->getJWKSources()[$key]->create($container, $service_id, $adapter);
58
59
                return;
60
            }
61
        }
62
        throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getNodeDefinition(ArrayNodeDefinition $node)
69
    {
70
        $sourceNodeBuilder = $node
71
            ->children()
72
                ->arrayNode('keys')
73
                    ->useAttributeAsKey('name')
74
                    ->prototype('array')
75
                        ->performNoDeepMerging()
76
                        ->children();
77
        foreach ($this->getJWKSources() as $name => $source) {
78
            $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset();
79
            $source->addConfiguration($sourceNode);
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function prepend(ContainerBuilder $container, array $config)
87
    {
88
    }
89
90
    /**
91
     * @return \SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource\JWKSourceInterface[]
92
     */
93
    private function getJWKSources()
94
    {
95
        if (null !== $this->jwk_sources) {
96
            return $this->jwk_sources;
97
        }
98
99
        // load bundled adapter factories
100
        $tempContainer = new ContainerBuilder();
101
        $loader = new XmlFileLoader($tempContainer, new FileLocator($this->bundle_path.'/Resources/config'));
102
        $loader->load('jwk_sources.xml');
103
104
        $services = $tempContainer->findTaggedServiceIds('jose.jwk_source');
105
        $jwk_sources = [];
106
        foreach (array_keys($services) as $id) {
107
            $factory = $tempContainer->get($id);
108
            $jwk_sources[str_replace('-', '_', $factory->getKey())] = $factory;
109
        }
110
111
        $this->jwk_sources = $jwk_sources;
112
113
        return $jwk_sources;
114
    }
115
}
116