Failed Conditions
Push — master ( 349866...67c1d1 )
by Florent
10:56 queued 06:08
created

ClientRegistrationSource::getNodeDefinition()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
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\ServerBundle\Component\Endpoint\ClientRegistration;
15
16
use OAuth2Framework\Component\ClientRegistrationEndpoint\ClientRegistrationEndpoint;
17
use OAuth2Framework\ServerBundle\Component\Component;
18
use OAuth2Framework\ServerBundle\Component\Endpoint\ClientRegistration\Compiler\ClientRegistrationEndpointRouteCompilerPass;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
23
24
class ClientRegistrationSource implements Component
25
{
26
    /**
27
     * @var Component[]
28
     */
29
    private $subComponents = [];
30
31
    /**
32
     * EndpointSource constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->subComponents = [
37
            new InitialAccessTokenSource(),
38
            new SoftwareStatementSource(),
39
        ];
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function name(): string
46
    {
47
        return 'client_registration';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function load(array $configs, ContainerBuilder $container)
54
    {
55
        if (!class_exists(ClientRegistrationEndpoint::class)) {
56
            return;
57
        }
58
        $config = $configs['endpoint']['client_registration'];
59
        $container->setParameter('oauth2_server.endpoint.client_registration.enabled', $config['enabled']);
60
        if (!$config['enabled']) {
61
            return;
62
        }
63
        $container->setParameter('oauth2_server.endpoint.client_registration.path', $config['path']);
64
        $container->setParameter('oauth2_server.endpoint.client_registration.host', $config['host']);
65
66
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/client_registration'));
67
        $loader->load('client_registration.php');
68
69
        foreach ($this->subComponents as $subComponent) {
70
            $subComponent->load($configs, $container);
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
78
    {
79
        if (!class_exists(ClientRegistrationEndpoint::class)) {
80
            return;
81
        }
82
        $childNode = $node->children()
83
            ->arrayNode($this->name())
84
                ->canBeEnabled();
85
86
        $childNode->children()
87
            ->scalarNode('path')
88
                ->defaultValue('/client/management')
89
                ->isRequired()
90
            ->end()
91
            ->scalarNode('host')
92
                ->info('If set, the route will be limited to that host')
93
                ->defaultValue('')
94
                ->treatFalseLike('')
95
                ->treatNullLike('')
96
            ->end()
97
        ->end();
98
99
        foreach ($this->subComponents as $subComponent) {
100
            $subComponent->getNodeDefinition($childNode, $node);
101
        }
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function prepend(ContainerBuilder $container, array $config): array
108
    {
109
        if (!class_exists(ClientRegistrationEndpoint::class)) {
110
            return [];
111
        }
112
        if (!$config['endpoint']['client_registration']['enabled']) {
113
            return [];
114
        }
115
        $updatedConfig = [];
116
        foreach ($this->subComponents as $subComponent) {
117
            $updatedConfig = array_merge(
118
                $updatedConfig,
119
                $subComponent->prepend($container, $config)
120
            );
121
        }
122
123
        return $updatedConfig;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function build(ContainerBuilder $container)
130
    {
131
        if (!class_exists(ClientRegistrationEndpoint::class)) {
132
            return;
133
        }
134
        $container->addCompilerPass(new ClientRegistrationEndpointRouteCompilerPass());
135
        foreach ($this->subComponents as $component) {
136
            $component->build($container);
137
        }
138
    }
139
}
140