Failed Conditions
Push — ng ( ede6c5...efffe8 )
by Florent
11:50
created

OAuth2FrameworkServerExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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 OAuth2Framework\Bundle\Server\DependencyInjection;
15
16
use Fluent\PhpConfigFileLoader;
17
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\AccessTokenRepositorySource;
18
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ClientSource;
19
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\Endpoint\EndpointSource;
20
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\FirewallSource;
21
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\Grant\GrantSource;
22
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\HttpSource;
23
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\KeySet;
24
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\OpenIdConnect\OpenIdConnectSource;
25
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ResourceServerRepositorySource;
26
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\Scope\ScopeSource;
27
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ServerNameSource;
28
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\SourceInterface;
29
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\TokenEndpointAuthMethod\TokenEndpointAuthMethodSource;
30
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\TokenType\TokenTypeSource;
31
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\UserAccountSource;
32
use Symfony\Component\Config\FileLocator;
33
use Symfony\Component\DependencyInjection\ContainerBuilder;
34
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
35
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
36
37
final class OAuth2FrameworkServerExtension extends Extension implements PrependExtensionInterface
38
{
39
    /**
40
     * @var Source\SourceInterface[]
41
     */
42
    private $sourceMap;
43
44
    /**
45
     * @var string
46
     */
47
    private $alias;
48
49
    /**
50
     * OAuth2FrameworkServerExtension constructor.
51
     *
52
     * @param string $alias
53
     */
54
    public function __construct(string $alias)
55
    {
56
        $this->alias = $alias;
57
        $this->initSourceMap();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAlias()
64
    {
65
        return $this->alias;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function load(array $configs, ContainerBuilder $container)
72
    {
73
        $configuration = new Configuration($this->alias, $this->sourceMap);
74
        $mergedConfig = $this->processConfiguration($configuration, $configs);
75
        $path = 'oauth2_server';
76
        $this->loadSources($path, $this->sourceMap, $mergedConfig, $container);
77
        $this->loadInternal($mergedConfig, $container);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function prepend(ContainerBuilder $container)
84
    {
85
        $bundleConfig = current($container->getExtensionConfig($this->getAlias()));
86
        $this->prependSources($bundleConfig, '', $this->sourceMap, $container);
87
    }
88
89
    /**
90
     * @param string            $path
91
     * @param SourceInterface[] $sources
92
     * @param array             $mergedConfig
93
     * @param ContainerBuilder  $container
94
     */
95
    private function loadSources(string $path, array $sources, array $mergedConfig, ContainerBuilder $container)
96
    {
97
        foreach ($sources as $k => $source) {
98
            if ($source instanceof SourceInterface) {
99
                $source->load($path, $container, $mergedConfig);
100
            } elseif (is_string($k) && is_array($source)) {
101
                $this->loadSources($path.'.'.$k, $source, $mergedConfig[$k], $container);
102
            }
103
        }
104
    }
105
106
    /**
107
     * @param array             $bundleConfig
108
     * @param string            $path
109
     * @param SourceInterface[] $sources
110
     * @param ContainerBuilder  $container
111
     */
112
    private function prependSources(array $bundleConfig, string $path, array $sources, ContainerBuilder $container)
113
    {
114
        foreach ($sources as $k => $source) {
115
            if ($source instanceof SourceInterface) {
116
                $source->prepend($bundleConfig, $path, $container);
117
            } elseif (is_string($k) && is_array($source)) {
118
                $this->prependSources($bundleConfig, $path.'['.$k.']', $source, $container);
119
            }
120
        }
121
    }
122
123
    /**
124
     * @param array            $mergedConfig
125
     * @param ContainerBuilder $container
126
     */
127
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $mergedConfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        $loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
130
        $files = [
131
            'access_token',
132
            'access_token_handler',
133
            'route_loader',
134
            'token_type_hint',
135
            'oauth2_response',
136
            'user_account_discovery',
137
            'content_parser_middleware',
138
        ];
139
        foreach ($files as $basename) {
140
            $loader->load(sprintf('%s.php', $basename));
141
        }
142
    }
143
144
    private function initSourceMap()
145
    {
146
        $this->sourceMap = [
147
            new ClientSource(),
148
            new FirewallSource(),
149
            new ServerNameSource(),
150
            new AccessTokenRepositorySource(),
151
            new UserAccountSource(),
152
            new ResourceServerRepositorySource(),
153
            new TokenTypeSource(),
154
            new TokenEndpointAuthMethodSource(),
155
            new GrantSource(),
156
            new EndpointSource(),
157
            new ScopeSource(),
158
            new OpenIdConnectSource(),
159
            new HttpSource(),
160
            new KeySet(),
161
        ];
162
    }
163
}
164