Failed Conditions
Push — v7 ( 0b3eb8...e446d4 )
by Florent
02:45
created

JoseFrameworkExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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