Failed Conditions
Push — v7 ( 8da077...12d27f )
by Florent
02:14
created

JoseFrameworkExtension::addDefaultSources()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 14
nc 64
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\JoseFramework\DependencyInjection;
15
16
use Jose\Bundle\Checker\DependencyInjection\Source\ClaimChecker;
17
use Jose\Bundle\Encryption\DependencyInjection\Source\JWEBuilder;
18
use Jose\Bundle\Encryption\DependencyInjection\Source\JWELoader;
19
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSetSource;
20
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSource;
21
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
22
use Jose\Bundle\Signature\DependencyInjection\Source\JWSBuilder;
23
use Jose\Bundle\Signature\DependencyInjection\Source\JWSLoader;
24
use Jose\Component\KeyManagement\JWKFactory;
25
use Symfony\Component\Config\Definition\Processor;
26
use Symfony\Component\Config\FileLocator;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
29
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
30
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
31
32
/**
33
 * Class JoseFrameworkExtension.
34
 */
35
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
36
{
37
    /**
38
     * @var string
39
     */
40
    private $alias;
41
42
    /**
43
     * @var string
44
     */
45
    private $bundlePath;
46
47
    /**
48
     * @var SourceInterface[]
49
     */
50
    private $serviceSources = [];
51
52
    /**
53
     * JoseFrameworkExtension constructor.
54
     *
55
     * @param string $alias
56
     * @param string $bundlePath
57
     */
58
    public function __construct(string $alias, string $bundlePath)
59
    {
60
        $this->alias = $alias;
61
        $this->bundlePath = $bundlePath;
62
        $this->addDefaultSources();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAlias(): string
69
    {
70
        return $this->alias;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function load(array $configs, ContainerBuilder $container)
77
    {
78
        $processor = new Processor();
79
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
80
81
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
82
        $loader->load('services.yml');
83
84
        if (class_exists(JWKFactory::class)) {
85
            $loader->load('jwk_factory.yml');
86
        }
87
        if (true === $config['use_default_json_converter']) {
88
            $loader->load('json_converter.yml');
89
        }
90
91
        foreach ($this->serviceSources as $serviceSource) {
92
            foreach ($config[$serviceSource->name()] as $name => $data) {
93
                $serviceSource->createService($name, $data, $container);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param SourceInterface $source
100
     */
101
    public function addSource(SourceInterface $source)
102
    {
103
        $name = $source->name();
104
        if (in_array($name, $this->serviceSources)) {
105
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
106
        }
107
        $this->serviceSources[$name] = $source;
108
    }
109
110
    /**
111
     * @param array            $configs
112
     * @param ContainerBuilder $container
113
     *
114
     * @return Configuration
115
     */
116
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
117
    {
118
        return new Configuration($this->getAlias(), $this->serviceSources);
119
    }
120
121
    private function addDefaultSources()
122
    {
123
        if (class_exists(JWKFactory::class)) {
124
            $this->addSource(new JWKSource($this->bundlePath));
125
            $this->addSource(new JWKSetSource($this->bundlePath));
126
        }
127
        if (class_exists(ClaimChecker::class)) {
128
            $this->addSource(new ClaimChecker());
129
        }
130
        if (class_exists(JWSBuilder::class)) {
131
            $this->addSource(new JWSBuilder());
132
        }
133
        if (class_exists(JWSLoader::class)) {
134
            $this->addSource(new JWSLoader());
135
        }
136
        if (class_exists(JWEBuilder::class)) {
137
            $this->addSource(new JWEBuilder());
138
        }
139
        if (class_exists(JWELoader::class)) {
140
            $this->addSource(new JWELoader());
141
        }
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function prepend(ContainerBuilder $container)
148
    {
149
        $configs = $container->getExtensionConfig($this->getAlias());
150
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
151
152
        foreach ($this->serviceSources as $serviceSource) {
153
            $result = $serviceSource->prepend($container, $config);
154
            if (null !== $result) {
155
                $container->prependExtensionConfig($this->getAlias(), $result);
156
            }
157
        }
158
    }
159
}
160