Failed Conditions
Push — v7 ( e446d4...9b9adb )
by Florent
02:29
created

JoseFrameworkExtension::addSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Encryption\DependencyInjection\Source\JWEBuilder;
17
use Jose\Bundle\Encryption\DependencyInjection\Source\JWELoader;
18
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSetSource;
19
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSource;
20
use Jose\Bundle\JoseFramework\DependencyInjection\Source\JWSBuilder;
21
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
22
use Jose\Bundle\Signature\DependencyInjection\Source\JWSLoader;
23
use Symfony\Component\Config\Definition\Processor;
24
use Symfony\Component\Config\FileLocator;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
27
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
28
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
29
30
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
31
{
32
    /**
33
     * @var string
34
     */
35
    private $alias;
36
37
    /**
38
     * @var string
39
     */
40
    private $bundlePath;
41
42
    /**
43
     * @var SourceInterface[]
44
     */
45
    private $serviceSources = [];
46
47
    /**
48
     * JoseFrameworkExtension constructor.
49
     *
50
     * @param string $alias
51
     * @param string $bundlePath
52
     */
53
    public function __construct(string $alias, string $bundlePath)
54
    {
55
        $this->alias = $alias;
56
        $this->bundlePath = $bundlePath;
57
        $this->addDefaultSources();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAlias(): string
64
    {
65
        return $this->alias;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function load(array $configs, ContainerBuilder $container)
72
    {
73
        $processor = new Processor();
74
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
75
76
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
77
        $loader->load('services.yml');
78
79
        foreach ($this->serviceSources as $serviceSource) {
80
            foreach ($config[$serviceSource->name()] as $name => $data) {
81
                $serviceSource->createService($name, $data, $container);
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param SourceInterface $source
88
     */
89
    public function addSource(SourceInterface $source)
90
    {
91
        $name = $source->name();
92
        if (in_array($name, $this->serviceSources)) {
93
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
94
        }
95
        $this->serviceSources[$name] = $source;
96
    }
97
98
    /**
99
     * @param array            $configs
100
     * @param ContainerBuilder $container
101
     *
102
     * @return Configuration
103
     */
104
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
105
    {
106
        return new Configuration($this->getAlias(), $this->serviceSources);
107
    }
108
109
    private function addDefaultSources()
110
    {
111
        $this->addSource(new JWKSource($this->bundlePath));
112
        $this->addSource(new JWKSetSource($this->bundlePath));
113
        if (class_exists(JWSBuilder::class)) {
114
            $this->addSource(new JWSBuilder());
115
        }
116
        if (class_exists(JWSLoader::class)) {
117
            $this->addSource(new JWSLoader());
118
        }
119
        if (class_exists(JWEBuilder::class)) {
120
            $this->addSource(new JWEBuilder());
121
        }
122
        if (class_exists(JWELoader::class)) {
123
            $this->addSource(new JWELoader());
124
        }
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->serviceSources as $serviceSource) {
136
            $result = $serviceSource->prepend($container, $config);
137
            if (null !== $result) {
138
                $container->prependExtensionConfig($this->getAlias(), $result);
139
            }
140
        }
141
    }
142
}
143