Failed Conditions
Push — master ( b5a0b4...819484 )
by Florent
08:00
created

ScopeSource::load()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 2
dl 0
loc 25
rs 9.2222
c 0
b 0
f 0
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\Scope;
15
16
use OAuth2Framework\Component\Scope\ScopeRepository;
17
use OAuth2Framework\ServerBundle\Component\Component;
18
use OAuth2Framework\ServerBundle\Component\Scope\Compiler\ScopeMetadataCompilerPass;
19
use OAuth2Framework\ServerBundle\Component\Scope\Compiler\ScopePolicyCompilerPass;
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 ScopeSource implements Component
26
{
27
    public function name(): string
28
    {
29
        return 'scope';
30
    }
31
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        if (!\interface_exists(ScopeRepository::class) || !$configs['scope']['enabled']) {
35
            return;
36
        }
37
38
        $container->setAlias(ScopeRepository::class, $configs['scope']['repository']);
39
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config/scope'));
40
        $loader->load('scope.php');
41
42
        if (!$configs['scope']['policy']['enabled']) {
43
            $container->setParameter('oauth2_server.scope.policy.by_default', 'none');
44
45
            return;
46
        }
47
48
        $container->setParameter('oauth2_server.scope.policy.by_default', $configs['scope']['policy']['by_default']);
49
        $loader->load('policy.php');
50
51
        if ($configs['scope']['policy']['default']['enabled']) {
52
            $container->setParameter('oauth2_server.scope.policy.default.scope', $configs['scope']['policy']['default']['scope']);
53
            $loader->load('policy_default.php');
54
        }
55
        if ($configs['scope']['policy']['error']['enabled']) {
56
            $loader->load('policy_error.php');
57
        }
58
    }
59
60
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
61
    {
62
        if (!\interface_exists(ScopeRepository::class)) {
63
            return;
64
        }
65
        $node->children()
66
            ->arrayNode($this->name())
67
            ->canBeEnabled()
68
            ->children()
69
            ->scalarNode('repository')
70
            ->info('Scope repository.')
71
            ->defaultNull()
72
            ->end()
73
            ->arrayNode('policy')
0 ignored issues
show
Bug introduced by
The method arrayNode() 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

73
            ->/** @scrutinizer ignore-call */ arrayNode('policy')
Loading history...
74
            ->canBeEnabled()
75
            ->children()
76
            ->scalarNode('by_default')
77
            ->info('Default scope policy.')
78
            ->defaultValue('none')
79
            ->end()
80
            ->arrayNode('error')
81
            ->canBeEnabled()
82
            ->info('When the error policy is used, requests without a scope are not allowed')
83
            ->end()
84
            ->arrayNode('default')
85
            ->canBeEnabled()
86
            ->children()
87
            ->scalarNode('scope')
88
            ->info('Scope added by default.')
89
            ->isRequired()
90
            ->end()
91
            ->end()
92
            ->end()
93
            ->end()
94
            ->end()
95
            ->end()
96
            ->end()
97
            ->end();
98
    }
99
100
    public function build(ContainerBuilder $container)
101
    {
102
        $container->addCompilerPass(new ScopePolicyCompilerPass());
103
        $container->addCompilerPass(new ScopeMetadataCompilerPass());
104
    }
105
106
    public function prepend(ContainerBuilder $container, array $config): array
107
    {
108
        //Nothing to do
109
        return [];
110
    }
111
}
112