Failed Conditions
Push — master ( 7ab844...7c31d6 )
by Florent
02:19
created

JoseFrameworkExtension::addDefaultSources()   F

Complexity

Conditions 10
Paths 256

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 3.1304
c 0
b 0
f 0
cc 10
eloc 17
nc 256
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Http\HttplugBundle\HttplugBundle;
17
use Jose\Bundle\Checker\DependencyInjection\Source\ClaimChecker;
18
use Jose\Bundle\Encryption\DependencyInjection\Source\JWEBuilder;
19
use Jose\Bundle\Encryption\DependencyInjection\Source\JWELoader;
20
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
21
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JKUSource;
22
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSetSource;
23
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSource;
24
use Jose\Bundle\Signature\DependencyInjection\Source\JWSBuilder;
25
use Jose\Bundle\Signature\DependencyInjection\Source\JWSLoader;
26
use Jose\Component\Core\Converter\JsonConverterInterface;
27
use Jose\Component\Core\Converter\StandardJsonConverter;
28
use Symfony\Component\Config\Definition\Processor;
29
use Symfony\Component\Config\FileLocator;
30
use Symfony\Component\DependencyInjection\ContainerBuilder;
31
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
32
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
33
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
34
35
/**
36
 * Class JoseFrameworkExtension.
37
 */
38
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
39
{
40
    /**
41
     * @var string
42
     */
43
    private $alias;
44
45
    /**
46
     * @var SourceInterface[]
47
     */
48
    private $serviceSources = [];
49
50
    /**
51
     * JoseFrameworkExtension constructor.
52
     *
53
     * @param string $alias
54
     */
55
    public function __construct(string $alias)
56
    {
57
        $this->alias = $alias;
58
        $this->addDefaultSources();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getAlias(): string
65
    {
66
        return $this->alias;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function load(array $configs, ContainerBuilder $container)
73
    {
74
        $processor = new Processor();
75
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
76
77
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
78
        $loader->load('services.yml');
79
80
        $container->setAlias(JsonConverterInterface::class, $config['json_converter']);
81
        if (StandardJsonConverter::class === $config['json_converter']) {
82
            $loader->load('json_converter.yml');
83
        }
84
85
        foreach ($this->serviceSources as $serviceSource) {
86
            $serviceSource->load($config, $container);
87
        }
88
    }
89
90
    /**
91
     * @param SourceInterface $source
92
     */
93
    public function addSource(SourceInterface $source)
94
    {
95
        $name = $source->name();
96
        if (in_array($name, $this->serviceSources)) {
97
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
98
        }
99
        $this->serviceSources[$name] = $source;
100
    }
101
102
    /**
103
     * @param array            $configs
104
     * @param ContainerBuilder $container
105
     *
106
     * @return Configuration
107
     */
108
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
109
    {
110
        return new Configuration($this->getAlias(), $this->serviceSources);
111
    }
112
113
    private function addDefaultSources()
114
    {
115
        if (class_exists(JKUSource::class) && class_exists(HttplugBundle::class)) {
116
            $this->addSource(new JKUSource());
117
        }
118
        if (class_exists(JWKSource::class)) {
119
            $this->addSource(new JWKSource());
120
        }
121
        if (class_exists(JWKSetSource::class)) {
122
            $this->addSource(new JWKSetSource());
123
        }
124
        if (class_exists(ClaimChecker::class)) {
125
            $this->addSource(new ClaimChecker());
126
        }
127
        if (class_exists(JWSBuilder::class)) {
128
            $this->addSource(new JWSBuilder());
129
        }
130
        if (class_exists(JWSLoader::class)) {
131
            $this->addSource(new JWSLoader());
132
        }
133
        if (class_exists(JWEBuilder::class)) {
134
            $this->addSource(new JWEBuilder());
135
        }
136
        if (class_exists(JWELoader::class)) {
137
            $this->addSource(new JWELoader());
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function prepend(ContainerBuilder $container)
145
    {
146
        $configs = $container->getExtensionConfig($this->getAlias());
147
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
148
149
        foreach ($this->serviceSources as $serviceSource) {
150
            $result = $serviceSource->prepend($container, $config);
151
            if (null !== $result) {
152
                $container->prependExtensionConfig($this->getAlias(), $result);
153
            }
154
        }
155
    }
156
}
157