Failed Conditions
Push — ng ( c00098...4b490a )
by Florent
06:57
created

GrantSource::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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\Bundle\Component\Grant;
15
16
use OAuth2Framework\Bundle\Component\Component;
17
use OAuth2Framework\Bundle\Component\ComponentWithCompilerPasses;
18
use OAuth2Framework\Bundle\Component\Grant\AuthorizationCode\AuthorizationCodeSource;
19
use OAuth2Framework\Bundle\Component\Grant\ClientCredentials\ClientCredentialsSource;
20
use OAuth2Framework\Bundle\Component\Grant\Implicit\ImplicitSource;
21
use OAuth2Framework\Bundle\Component\Grant\JwtBearer\JwtBearerSource;
22
use OAuth2Framework\Bundle\Component\Grant\None\NoneSource;
23
use OAuth2Framework\Bundle\Component\Grant\RefreshToken\RefreshTokenSource;
24
use OAuth2Framework\Bundle\Component\Grant\ResourceOwnerPasswordCredential\ResourceOwnerPasswordCredentialSource;
25
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
26
use OAuth2Framework\Component\TokenEndpoint\GrantType;
27
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
31
32
class GrantSource implements Component
33
{
34
    /**
35
     * @var Component[]
36
     */
37
    private $subComponents;
38
39
    public function __construct()
40
    {
41
        $this->subComponents = [
42
            new AuthorizationCodeSource(),
43
            new ClientCredentialsSource(),
44
            new ImplicitSource(),
45
            new RefreshTokenSource(),
46
            new ResourceOwnerPasswordCredentialSource(),
47
            new JwtBearerSource(),
48
            new NoneSource(),
49
        ];
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function name(): string
56
    {
57
        return 'grant';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function load(array $configs, ContainerBuilder $container)
64
    {
65
        $container->registerForAutoconfiguration(GrantType::class)->addTag('oauth2_server_grant_type');
66
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
67
68
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config/grant'));
69
        $loader->load('grant.php');
70
71
        foreach ($this->subComponents as $subComponent) {
72
            $subComponent->load($configs, $container);
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getNodeDefinition(NodeDefinition $node)
80
    {
81
        $childNode = $node->children()
82
            ->arrayNode($this->name())
83
                ->addDefaultsIfNotSet();
84
85
        foreach ($this->subComponents as $subComponent) {
86
            $subComponent->getNodeDefinition($childNode);
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function prepend(ContainerBuilder $container, array $config): array
94
    {
95
        $updatedConfig = [];
96
        foreach ($this->subComponents as $subComponent) {
97
            $updatedConfig = array_merge(
98
                $updatedConfig,
99
                $subComponent->prepend($container, $config)
100
            );
101
        }
102
103
        return $updatedConfig;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function build(ContainerBuilder $container)
110
    {
111
        foreach ($this->subComponents as $component) {
112
            $component->build($container);
113
        };
114
    }
115
}
116