Completed
Push — master ( 3a2ac4...6415d9 )
by Florent
01:57
created

SpomkyLabsJoseBundleExtension   B

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addServiceSource() 0 6 1
A load() 0 18 2
A getConfiguration() 0 4 1
A getAlias() 0 4 1
A initConfiguration() 0 8 3
A updateSources() 0 12 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;
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 string
35
     */
36
    private $alias;
37
38
    /**
39
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface[]
40
     */
41
    private $service_sources = [];
42
43
    /**
44
     * @param string $alias
45
     */
46
    public function __construct($alias)
47
    {
48
        $this->alias = $alias;
49
    }
50
51
    /**
52
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface $source
53
     */
54
    public function addServiceSource(SourceInterface $source)
55
    {
56
        $name = $source->getName();
57
        Assertion::false(in_array($name, $this->service_sources), sprintf('The source "%s" is already set.', $name));
58
        $this->service_sources[$name] = $source;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function load(array $configs, ContainerBuilder $container)
65
    {
66
        $processor = new Processor();
67
        $this->updateSources();
68
69
        $config = $processor->processConfiguration(
70
            $this->getConfiguration($configs, $container),
71
            $configs
72
        );
73
74
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
75
        $services = ['services', 'compression_methods', 'checkers', 'signature_algorithms', 'encryption_algorithms', 'checkers',];
76
        foreach ($services as $basename) {
77
            $loader->load(sprintf('%s.xml', $basename));
78
        }
79
80
        $this->initConfiguration($container, $config);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getConfiguration(array $configs, ContainerBuilder $container)
87
    {
88
        return new Configuration($this->getAlias(), $this->service_sources);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getAlias()
95
    {
96
        return $this->alias;
97
    }
98
99
    /**
100
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
101
     * @param array                                                   $config
102
     */
103
    private function initConfiguration(ContainerBuilder $container, array $config)
104
    {
105
        foreach ($this->service_sources as $service_source) {
106
            foreach ($config[$service_source->getName()] as $name => $data) {
107
                $service_source->createService($name, $data, $container);
108
            }
109
        }
110
    }
111
112
    /**
113
     * 
114
     */
115
    private function updateSources()
116
    {
117
        $this->addServiceSource(new JWTCreatorSource());
118
        $this->addServiceSource(new JWTLoaderSource());
119
        $this->addServiceSource(new SignerSource());
120
        $this->addServiceSource(new VerifierSource());
121
        $this->addServiceSource(new EncrypterSource());
122
        $this->addServiceSource(new DecrypterSource());
123
        $this->addServiceSource(new CheckerSource());
124
        $this->addServiceSource(new JWKSource());
125
        $this->addServiceSource(new JWKSetSource());
126
    }
127
}
128