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

MetadataEndpointSource::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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