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

SpomkyLabsJoseBundleExtension   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 33
Bugs 12 Features 5
Metric Value
wmc 13
c 33
b 12
f 5
lcom 1
cbo 19
dl 0
loc 115
rs 6.875

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addServiceSource() 0 6 1
A load() 0 17 2
A getConfiguration() 0 4 1
A getAlias() 0 4 1
A initConfiguration() 0 8 3
A updateSources() 0 14 1
A prepend() 0 12 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\EasyJWTCreatorSource;
18
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EasyJWTLoaderSource;
19
use SpomkyLabs\JoseBundle\DependencyInjection\Source\EncrypterSource;
20
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSetSource;
21
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWKSource;
22
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTCreatorSource;
23
use SpomkyLabs\JoseBundle\DependencyInjection\Source\JWTLoaderSource;
24
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SignerSource;
25
use SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface;
26
use SpomkyLabs\JoseBundle\DependencyInjection\Source\VerifierSource;
27
use Symfony\Component\Config\Definition\Processor;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
31
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
32
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
33
34
final class SpomkyLabsJoseBundleExtension extends Extension implements PrependExtensionInterface
35
{
36
    /**
37
     * @var string
38
     */
39
    private $alias;
40
41
    /**
42
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface[]
43
     */
44
    private $service_sources = [];
45
46
    /**
47
     * @param string $alias
48
     */
49
    public function __construct($alias)
50
    {
51
        $this->alias = $alias;
52
        $this->updateSources();
53
    }
54
55
    /**
56
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface $source
57
     */
58
    public function addServiceSource(SourceInterface $source)
59
    {
60
        $name = $source->getName();
61
        Assertion::false(in_array($name, $this->service_sources), sprintf('The source "%s" is already set.', $name));
62
        $this->service_sources[$name] = $source;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function load(array $configs, ContainerBuilder $container)
69
    {
70
        $processor = new Processor();
71
72
        $config = $processor->processConfiguration(
73
            $this->getConfiguration($configs, $container),
74
            $configs
75
        );
76
77
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
78
        $services = ['services', 'compression_methods', 'checkers', 'signature_algorithms', 'encryption_algorithms', 'checkers'];
79
        foreach ($services as $basename) {
80
            $loader->load(sprintf('%s.xml', $basename));
81
        }
82
83
        $this->initConfiguration($container, $config);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getConfiguration(array $configs, ContainerBuilder $container)
90
    {
91
        return new Configuration($this->getAlias(), $this->service_sources);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getAlias()
98
    {
99
        return $this->alias;
100
    }
101
102
    /**
103
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
104
     * @param array                                                   $config
105
     */
106
    private function initConfiguration(ContainerBuilder $container, array $config)
107
    {
108
        foreach ($this->service_sources as $service_source) {
109
            foreach ($config[$service_source->getName()] as $name => $data) {
110
                $service_source->createService($name, $data, $container);
111
            }
112
        }
113
    }
114
115
    /**
116
     * 
117
     */
118
    private function updateSources()
119
    {
120
        $this->addServiceSource(new JWTCreatorSource());
121
        $this->addServiceSource(new JWTLoaderSource());
122
        $this->addServiceSource(new SignerSource());
123
        $this->addServiceSource(new VerifierSource());
124
        $this->addServiceSource(new EncrypterSource());
125
        $this->addServiceSource(new DecrypterSource());
126
        $this->addServiceSource(new CheckerSource());
127
        $this->addServiceSource(new JWKSource());
128
        $this->addServiceSource(new JWKSetSource());
129
        $this->addServiceSource(new EasyJWTCreatorSource());
130
        $this->addServiceSource(new EasyJWTLoaderSource());
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function prepend(ContainerBuilder $container)
137
    {
138
        $configs = $container->getExtensionConfig($this->getAlias());
139
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
140
141
        foreach ($this->service_sources as $service_source) {
142
            $result = $service_source->prepend($container, $config);
143
            if (null !== $result) {
144
                $container->prependExtensionConfig($this->getAlias(), $result);
145
            }
146
        }
147
    }
148
}
149