Failed Conditions
Push — master ( d9377e...587e80 )
by Florent
02:05
created

JoseFrameworkExtension::load()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
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\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\SourceInterface;
20
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JKUSource;
21
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSetSource;
22
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSource;
23
use Jose\Bundle\Signature\DependencyInjection\Source\JWSBuilder;
24
use Jose\Bundle\Signature\DependencyInjection\Source\JWSLoader;
25
use Jose\Component\Core\Converter\JsonConverterInterface;
26
use Jose\Component\Core\Converter\StandardJsonConverter;
27
use Symfony\Component\Config\Definition\Processor;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
31
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
32
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
33
34
/**
35
 * Class JoseFrameworkExtension.
36
 */
37
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
38
{
39
    /**
40
     * @var string
41
     */
42
    private $alias;
43
44
    /**
45
     * @var SourceInterface[]
46
     */
47
    private $serviceSources = [];
48
49
    /**
50
     * JoseFrameworkExtension constructor.
51
     *
52
     * @param string $alias
53
     */
54
    public function __construct(string $alias)
55
    {
56
        $this->alias = $alias;
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
        $container->setAlias(JsonConverterInterface::class, $config['json_converter']);
80
        if (StandardJsonConverter::class === $config['json_converter']) {
81
            $loader->load('json_converter.yml');
82
        }
83
84
        foreach ($this->serviceSources as $serviceSource) {
85
            $serviceSource->createService($config[$serviceSource->name()], $container);
86
        }
87
    }
88
89
    /**
90
     * @param SourceInterface $source
91
     */
92
    public function addSource(SourceInterface $source)
93
    {
94
        $name = $source->name();
95
        if (in_array($name, $this->serviceSources)) {
96
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
97
        }
98
        $this->serviceSources[$name] = $source;
99
    }
100
101
    /**
102
     * @param array            $configs
103
     * @param ContainerBuilder $container
104
     *
105
     * @return Configuration
106
     */
107
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
108
    {
109
        return new Configuration($this->getAlias(), $this->serviceSources);
110
    }
111
112
    private function addDefaultSources()
113
    {
114
        if (class_exists(JKUSource::class)) {
115
            $this->addSource(new JKUSource());
116
            $this->addSource(new JWKSource());
117
            $this->addSource(new JWKSetSource());
118
        }
119
        if (class_exists(ClaimChecker::class)) {
120
            $this->addSource(new ClaimChecker());
121
        }
122
        if (class_exists(JWSBuilder::class)) {
123
            $this->addSource(new JWSBuilder());
124
        }
125
        if (class_exists(JWSLoader::class)) {
126
            $this->addSource(new JWSLoader());
127
        }
128
        if (class_exists(JWEBuilder::class)) {
129
            $this->addSource(new JWEBuilder());
130
        }
131
        if (class_exists(JWELoader::class)) {
132
            $this->addSource(new JWELoader());
133
        }
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function prepend(ContainerBuilder $container)
140
    {
141
        $configs = $container->getExtensionConfig($this->getAlias());
142
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
143
144
        foreach ($this->serviceSources as $serviceSource) {
145
            $result = $serviceSource->prepend($container, $config);
146
            if (null !== $result) {
147
                $container->prependExtensionConfig($this->getAlias(), $result);
148
            }
149
        }
150
    }
151
}
152