Failed Conditions
Push — master ( 3bf08d...6e3f66 )
by Florent
03:16
created

OAuth2FrameworkServerExtension   C

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 19
dl 0
loc 126
rs 6.875
c 0
b 0
f 0

8 Methods

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