getNodeDefinition()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 30
rs 9.536
c 0
b 0
f 0
cc 4
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\TokenIntrospection;
15
16
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenIntrospectionEndpoint;
17
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHint;
18
use OAuth2Framework\ServerBundle\Component\Component;
19
use OAuth2Framework\ServerBundle\Component\Endpoint\TokenIntrospection\Compiler\TokenIntrospectionRouteCompilerPass;
20
use OAuth2Framework\ServerBundle\Component\Endpoint\TokenIntrospection\Compiler\TokenTypeHintCompilerPass;
21
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25
26
class TokenIntrospectionEndpointSource implements Component
27
{
28
    public function name(): string
29
    {
30
        return 'token_introspection';
31
    }
32
33
    public function load(array $configs, ContainerBuilder $container): void
34
    {
35
        if (!class_exists(TokenIntrospectionEndpoint::class)) {
36
            return;
37
        }
38
        $config = $configs['endpoint']['token_introspection'];
39
        $container->setParameter('oauth2_server.endpoint.token_introspection.enabled', $config['enabled']);
40
        if (!$config['enabled']) {
41
            return;
42
        }
43
        $container->registerForAutoconfiguration(TokenTypeHint::class)->addTag('oauth2_server_introspection_type_hint');
44
        $container->setParameter('oauth2_server.endpoint.token_introspection.path', $config['path']);
45
        $container->setParameter('oauth2_server.endpoint.token_introspection.host', $config['host']);
46
47
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/token_introspection'));
48
        $loader->load('introspection.php');
49
    }
50
51
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode): void
52
    {
53
        if (!class_exists(TokenIntrospectionEndpoint::class)) {
54
            return;
55
        }
56
        $rootNode->validate()
57
            ->ifTrue(function ($config) {
58
                return true === $config['endpoint'][$this->name()]['enabled'] && (!isset($config['resource_server']['repository']) || null === $config['resource_server']['repository']);
59
            })
60
            ->thenInvalid('The resource server repository must be set when the introspection endpoint is enabled')
61
            ->end()
62
        ;
63
64
        $node->children()
65
            ->arrayNode($this->name())
66
            ->canBeEnabled()
67
            ->children()
68
            ->scalarNode('path')
69
            ->info('The token introspection endpoint path')
70
            ->defaultValue('/token/introspection')
71
            ->end()
72
            ->scalarNode('host')
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            ->/** @scrutinizer ignore-call */ scalarNode('host')
Loading history...
73
            ->info('If set, the route will be limited to that host')
74
            ->defaultValue('')
75
            ->treatNullLike('')
76
            ->treatFalseLike('')
77
            ->end()
78
            ->end()
79
            ->end()
80
            ->end()
81
        ;
82
    }
83
84
    public function build(ContainerBuilder $container): void
85
    {
86
        if (!class_exists(TokenIntrospectionEndpoint::class)) {
87
            return;
88
        }
89
        $container->addCompilerPass(new TokenTypeHintCompilerPass());
90
        $container->addCompilerPass(new TokenIntrospectionRouteCompilerPass());
91
    }
92
93
    public function prepend(ContainerBuilder $container, array $config): array
94
    {
95
        return [];
96
    }
97
}
98