SpomkyLabsJoseBundleExtension   C
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 19
dl 0
loc 121
rs 6.875
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 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 addDefaultSources() 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 Symfony\Component\Config\Definition\Processor;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
final class SpomkyLabsJoseBundleExtension extends Extension implements PrependExtensionInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $alias;
28
29
    /**
30
     * @var string
31
     */
32
    private $bundle_path;
33
34
    /**
35
     * @var \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface[]
36
     */
37
    private $service_sources = [];
38
39
    /**
40
     * SpomkyLabsJoseBundleExtension constructor.
41
     *
42
     * @param string $alias
43
     * @param string $bundle_path
44
     */
45
    public function __construct($alias, $bundle_path)
46
    {
47
        $this->alias = $alias;
48
        $this->bundle_path = $bundle_path;
49
        $this->addDefaultSources();
50
    }
51
52
    /**
53
     * @param \SpomkyLabs\JoseBundle\DependencyInjection\Source\SourceInterface $source
54
     */
55
    public function addServiceSource(Source\SourceInterface $source)
56
    {
57
        $name = $source->getName();
58
        Assertion::false(in_array($name, $this->service_sources), sprintf('The source "%s" is already set.', $name));
59
        $this->service_sources[$name] = $source;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function load(array $configs, ContainerBuilder $container)
66
    {
67
        $processor = new Processor();
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', 'jwkset_controller'];
76
        foreach ($services as $basename) {
77
            $loader->load(sprintf('%s.xml', $basename));
78
        }
79
80
        $this->initConfiguration($container, $config);
81
    }
82
83
    /**
84
     * @return \SpomkyLabs\JoseBundle\DependencyInjection\Configuration
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
    private function addDefaultSources()
113
    {
114
        $this->addServiceSource(new Source\JWTCreatorSource());
115
        $this->addServiceSource(new Source\JWTLoaderSource());
116
        $this->addServiceSource(new Source\SignerSource());
117
        $this->addServiceSource(new Source\VerifierSource());
118
        $this->addServiceSource(new Source\EncrypterSource());
119
        $this->addServiceSource(new Source\DecrypterSource());
120
        $this->addServiceSource(new Source\CheckerSource());
121
        $this->addServiceSource(new Source\JWKSource($this->bundle_path));
122
        $this->addServiceSource(new Source\JWKSetSource($this->bundle_path));
123
        $this->addServiceSource(new Source\EasyJWTCreatorSource());
124
        $this->addServiceSource(new Source\EasyJWTLoaderSource());
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function prepend(ContainerBuilder $container)
131
    {
132
        $configs = $container->getExtensionConfig($this->getAlias());
133
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
134
135
        foreach ($this->service_sources as $service_source) {
136
            $result = $service_source->prepend($container, $config);
137
            if (null !== $result) {
138
                $container->prependExtensionConfig($this->getAlias(), $result);
139
            }
140
        }
141
    }
142
}
143