Failed Conditions
Push — v7 ( 477009...5356df )
by Florent
03:33
created

JoseFrameworkExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 120
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getAlias() 0 4 1
B load() 0 33 3
A addSource() 0 8 2
A getConfiguration() 0 4 1
A addDefaultSources() 0 5 1
A prepend() 0 12 3
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2017 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 Jose\Bundle\JoseFramework\DependencyInjection;
13
14
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSetSource;
15
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSource;
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
17
use Symfony\Component\Config\Definition\Processor;
18
use Symfony\Component\Config\FileLocator;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $alias;
30
31
    /**
32
     * @var string
33
     */
34
    private $bundlePath;
35
36
    /**
37
     * @var SourceInterface[]
38
     */
39
    private $serviceSources = [];
40
41
    /**
42
     * JoseFrameworkExtension constructor.
43
     *
44
     * @param string $alias
45
     * @param string $bundlePath
46
     */
47
    public function __construct(string $alias, string $bundlePath)
48
    {
49
        $this->alias = $alias;
50
        $this->bundlePath = $bundlePath;
51
        $this->addDefaultSources();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getAlias(): string
58
    {
59
        return $this->alias;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function load(array $configs, ContainerBuilder $container)
66
    {
67
        $processor = new Processor();
68
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
69
70
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
71
        $loader->load('services.yml');
72
73
        // A translator must always be registered (as support is included by
74
        // default in the Form and Validator component). If disabled, an identity
75
        // translator will be used and everything will still work as expected.
76
        /*if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
77
            if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
78
                throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
79
            }
80
81
            if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['form'])) {
82
                throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
83
            }
84
85
            if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['validation'])) {
86
                throw new LogicException('Validation support cannot be enabled as the Translation component is not installed.');
87
            }
88
89
            $loader->load('identity_translator.xml');
90
        }*/
91
92
        foreach ($this->serviceSources as $serviceSource) {
93
            foreach ($config[$serviceSource->name()] as $name => $data) {
94
                $serviceSource->createService($name, $data, $container);
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param SourceInterface $source
101
     */
102
    public function addSource(SourceInterface $source)
103
    {
104
        $name = $source->name();
105
        if (in_array($name, $this->serviceSources)) {
106
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
107
        }
108
        $this->serviceSources[$name] = $source;
109
    }
110
111
    /**
112
     * @param array $configs
113
     * @param ContainerBuilder $container
114
     *
115
     * @return Configuration
116
     */
117
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
118
    {
119
        return new Configuration($this->getAlias(), $this->serviceSources);
120
    }
121
122
    private function addDefaultSources()
123
    {
124
        $this->addSource(new JWKSource($this->bundlePath));
125
        $this->addSource(new JWKSetSource($this->bundlePath));
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function prepend(ContainerBuilder $container)
132
    {
133
        $configs = $container->getExtensionConfig($this->getAlias());
134
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
135
136
        foreach ($this->serviceSources as $serviceSource) {
137
            $result = $serviceSource->prepend($container, $config);
138
            if (null !== $result) {
139
                $container->prependExtensionConfig($this->getAlias(), $result);
140
            }
141
        }
142
    }
143
}
144