Failed Conditions
Pull Request — master (#31)
by Florent
07:03 queued 03:28
created

OAuth2FrameworkServerExtension::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 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\Grant\GrantSource;
21
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\OpenIdConnect\OpenIdConnectSource;
22
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ResourceServerRepositorySource;
23
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\Scope\ScopeSource;
24
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ServerNameSource;
25
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\SourceInterface;
26
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\TokenEndpointAuthMethod\TokenEndpointAuthMethodSource;
27
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\TokenType\TokenTypeSource;
28
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\UserAccountRepositorySource;
29
use Symfony\Component\Config\FileLocator;
30
use Symfony\Component\DependencyInjection\ContainerBuilder;
31
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
32
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
33
34
final class OAuth2FrameworkServerExtension extends Extension implements PrependExtensionInterface
35
{
36
    /**
37
     * @var Source\SourceInterface[]
38
     */
39
    private $sourceMap;
40
41
    /**
42
     * @var string
43
     */
44
    private $alias;
45
46
    /**
47
     * OAuth2FrameworkServerExtension constructor.
48
     *
49
     * @param string $alias
50
     */
51
    public function __construct(string $alias)
52
    {
53
        $this->alias = $alias;
54
        $this->initSourceMap();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getAlias()
61
    {
62
        return $this->alias;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function load(array $configs, ContainerBuilder $container)
69
    {
70
        $configuration = new Configuration($this->alias, $this->sourceMap);
71
        $mergedConfig = $this->processConfiguration($configuration, $configs);
72
        $path = 'oauth2_server';
73
        $this->loadSources($path, $this->sourceMap, $mergedConfig, $container);
74
        $this->loadInternal($mergedConfig, $container);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function prepend(ContainerBuilder $container)
81
    {
82
        $bundleConfig = current($container->getExtensionConfig($this->getAlias()));
83
        $this->prependSources($bundleConfig, '', $this->sourceMap, $container);
84
    }
85
86
    /**
87
     * @param string            $path
88
     * @param SourceInterface[] $sources
89
     * @param array             $mergedConfig
90
     * @param ContainerBuilder  $container
91
     */
92
    private function loadSources(string $path, array $sources, array $mergedConfig, ContainerBuilder $container)
93
    {
94
        foreach ($sources as $k => $source) {
95
            if ($source instanceof SourceInterface) {
96
                $source->load($path, $container, $mergedConfig);
97
            } elseif (is_string($k) && is_array($source)) {
98
                $this->loadSources($path.'.'.$k, $source, $mergedConfig[$k], $container);
99
            }
100
        }
101
    }
102
103
    /**
104
     * @param array             $bundleConfig
105
     * @param string            $path
106
     * @param SourceInterface[] $sources
107
     * @param ContainerBuilder  $container
108
     */
109
    private function prependSources(array $bundleConfig, string $path, array $sources, ContainerBuilder $container)
110
    {
111
        foreach ($sources as $k => $source) {
112
            if ($source instanceof SourceInterface) {
113
                $source->prepend($bundleConfig, $path, $container);
114
            } elseif (is_string($k) && is_array($source)) {
115
                $this->prependSources($bundleConfig, $path.'['.$k.']', $source, $container);
116
            }
117
        }
118
    }
119
120
    /**
121
     * @param array            $mergedConfig
122
     * @param ContainerBuilder $container
123
     */
124
    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...
125
    {
126
        $loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
127
        $files = [
128
            'service',
129
            'access_token',
130
            'access_token_handler',
131
            'route_loader',
132
            'token_type_hint',
133
            'oauth2_response',
134
            'user_account_discovery',
135
        ];
136
        foreach ($files as $basename) {
137
            $loader->load(sprintf('%s.php', $basename));
138
        }
139
    }
140
141
    private function initSourceMap()
142
    {
143
        $this->sourceMap = [
144
            new ClientSource(),
145
            new ServerNameSource(),
146
            new AccessTokenRepositorySource(),
147
            new UserAccountRepositorySource(),
148
            new ResourceServerRepositorySource(),
149
            new TokenTypeSource(),
150
            new TokenEndpointAuthMethodSource(),
151
            new GrantSource(),
152
            new EndpointSource(),
153
            new ScopeSource(),
154
            new OpenIdConnectSource(),
155
        ];
156
    }
157
}
158