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

SpomkyLabsJoseBundleExtension::createVerifier()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 1
nop 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;
13
14
use Assert\Assertion;
15
use SpomkyLabs\JoseBundle\DependencyInjection\Source\CheckerSource;
16
use SpomkyLabs\JoseBundle\DependencyInjection\Source\DecrypterSource;
17
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EncrypterSource;
18
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource;
19
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource;
20
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTCreatorSource;
21
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTLoaderSource;
22
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SignerSource;
23
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface;
24
use SpomkyLabs\JoseBundle\DependencyInjection\Source\VerifierSource;
25
use Symfony\Component\Config\Definition\Processor;
26
use Symfony\Component\Config\FileLocator;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
29
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
30
31
final class SpomkyLabsJoseBundleExtension extends Extension
32
{
33
    /**
34
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource\JWKSetSourceInterface[]
35
     */
36
    private $jwk_set_sources;
0 ignored issues
show
Unused Code introduced by
The property $jwk_set_sources is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    /**
39
     * @var string
40
     */
41
    private $alias;
42
43
    /**
44
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface[]
45
     */
46
    private $service_sources = [];
47
48
    /**
49
     * @param string $alias
50
     */
51
    public function __construct($alias)
52
    {
53
        $this->alias = $alias;
54
    }
55
56
    /**
57
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface $source
58
     */
59
    public function addServiceSource(SourceInterface $source)
60
    {
61
        $name = $source->getName();
62
        Assertion::false(in_array($name, $this->service_sources), sprintf('The source "%s" is already set.', $name));
63
        $this->service_sources[$name] = $source;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function load(array $configs, ContainerBuilder $container)
70
    {
71
        $processor = new Processor();
72
        $this->updateSources();
73
74
        $config = $processor->processConfiguration(
75
            $this->getConfiguration($configs, $container),
76
            $configs
77
        );
78
79
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
80
        $services = $this->getXmlFileToLoad();
81
        foreach ($services as $basename) {
82
            $loader->load(sprintf('%s.xml', $basename));
83
        }
84
85
        $this->initConfiguration($container, $config);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getConfiguration(array $configs, ContainerBuilder $container)
92
    {
93
        return new Configuration($this->getAlias(), $this->service_sources);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getAlias()
100
    {
101
        return $this->alias;
102
    }
103
104
    /**
105
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
106
     * @param array                                                   $config
107
     */
108
    private function initConfiguration(ContainerBuilder $container, array $config)
109
    {
110
        foreach ($this->service_sources as $service_source) {
111
            foreach ($config[$service_source->getName()] as $name => $data) {
112
                $service_source->createService($name, $data, $container);
113
            }
114
        }
115
    }
116
117
    /**
118
     * @return string[]
119
     */
120
    private function getXmlFileToLoad()
121
    {
122
        $services = [
123
            'services',
124
            'compression_methods',
125
            'checkers',
126
            'signature_algorithms',
127
            'encryption_algorithms',
128
            'checkers',
129
        ];
130
131
        return $services;
132
    }
133
134
    /**
135
     * 
136
     */
137
    private function updateSources()
138
    {
139
        $this->addServiceSource(new JWTCreatorSource());
140
        $this->addServiceSource(new JWTLoaderSource());
141
        $this->addServiceSource(new SignerSource());
142
        $this->addServiceSource(new VerifierSource());
143
        $this->addServiceSource(new EncrypterSource());
144
        $this->addServiceSource(new DecrypterSource());
145
        $this->addServiceSource(new CheckerSource());
146
        $this->addServiceSource(new JWKSource());
147
        $this->addServiceSource(new JWKSetSource());
148
    }
149
}
150