Passed
Push — main ( 6d3c72...2f3aa0 )
by Daniel
04:34
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 20
ccs 0
cts 16
cp 0
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\DependencyInjection;
15
16
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
use Symfony\Component\HttpFoundation\Cookie;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    public function getConfigTreeBuilder(): TreeBuilder
27
    {
28
        $treeBuilder = new TreeBuilder('silverback_api_components');
29
        $rootNode = $treeBuilder->getRootNode();
30
        $rootNode
31
            ->children()
32
                ->scalarNode('website_name')->isRequired()->end()
33
                ->scalarNode('table_prefix')->defaultValue('_acb_')->end()
34
                ->scalarNode('metadata_key')->defaultValue('_metadata')->end()
35
            ->end();
36
37
        $this->addMercureNode($rootNode);
38
        $this->addRouteSecurityNode($rootNode);
39
        $this->addRoutableSecurityNode($rootNode);
40
        $this->addRefreshTokenNode($rootNode);
41
        $this->addPublishableNode($rootNode);
42
        $this->addEnabledComponentsNode($rootNode);
43
        $this->addUserNode($rootNode);
44
45
        return $treeBuilder;
46
    }
47
48
    private function addMercureNode(ArrayNodeDefinition $rootNode): void
49
    {
50
        $rootNode
51
            ->children()
52
                ->arrayNode('mercure')
53
                    ->addDefaultsIfNotSet()
54
                    ->children()
55
                        ->scalarNode('hub')->defaultNull()->end()
56
                        ->arrayNode('cookie')
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

56
                        ->/** @scrutinizer ignore-call */ arrayNode('cookie')
Loading history...
57
                            ->addDefaultsIfNotSet()
58
                            ->children()
59
                                 ->scalarNode('samesite')->defaultValue(Cookie::SAMESITE_STRICT)
60
                                    ->validate()
61
                                        ->ifNotInArray([Cookie::SAMESITE_STRICT, Cookie::SAMESITE_LAX, Cookie::SAMESITE_NONE])
62
                                        ->thenInvalid('Invalid Mercure cookie samesite value %s')
63
                                    ->end()
64
                                ->end()
65
                            ->end()
66
                        ->end()
67
                    ->end()
68
                ->end()
69
            ->end();
70
    }
71
72
    private function addRouteSecurityNode(ArrayNodeDefinition $rootNode): void
73
    {
74
        $rootNode
75
            ->children()
76
                ->arrayNode('route_security')
77
                    ->arrayPrototype()
78
                        ->children()
79
                            ->scalarNode('route')->end()
80
                            ->scalarNode('security')->end()
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

80
                            ->/** @scrutinizer ignore-call */ scalarNode('security')->end()
Loading history...
81
                        ->end()
82
                    ->end()
83
                ->end()
84
            ->end();
85
    }
86
87
    private function addRoutableSecurityNode(ArrayNodeDefinition $rootNode): void
88
    {
89
        $rootNode
90
            ->children()
91
                ->scalarNode('routable_security')->defaultNull()->end()
92
            ->end();
0 ignored issues
show
Bug introduced by
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

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

92
            ->/** @scrutinizer ignore-call */ end();
Loading history...
93
    }
94
95
    private function addRefreshTokenNode(ArrayNodeDefinition $rootNode): void
96
    {
97
        $rootNode
98
            ->children()
99
                ->arrayNode('refresh_token')
100
                    ->addDefaultsIfNotSet()
101
                    ->children()
102
                        ->scalarNode('handler_id')->cannotBeEmpty()->isRequired()->end()
103
                        ->arrayNode('options')
104
                            ->useAttributeAsKey('key')
105
                            ->prototype('variable')->end()
106
                        ->end()
107
                        ->scalarNode('cookie_name')->cannotBeEmpty()->isRequired()->end()
108
                        ->scalarNode('ttl')->cannotBeEmpty()->isRequired()->end()
109
                        ->scalarNode('database_user_provider')->cannotBeEmpty()->isRequired()->end()
110
                    ->end()
111
                ->end()
112
            ->end();
113
    }
114
115
    private function addPublishableNode(ArrayNodeDefinition $rootNode): void
116
    {
117
        $rootNode
118
            ->children()
119
                ->arrayNode('publishable')
120
                    ->addDefaultsIfNotSet()
121
                    ->children()
122
                        ->scalarNode('permission')->cannotBeEmpty()->isRequired()->end()
123
                    ->end()
124
                ->end()
125
            ->end();
126
    }
127
128
    private function addEnabledComponentsNode(ArrayNodeDefinition $rootNode): void
129
    {
130
        $rootNode
131
            ->children()
132
                ->arrayNode('enabled_components')
133
                    ->addDefaultsIfNotSet()
134
                    ->children()
135
                        ->booleanNode('form')->defaultValue(true)->end()
136
                        ->booleanNode('collection')->defaultValue(true)->end()
0 ignored issues
show
Bug introduced by
The method booleanNode() 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

136
                        ->/** @scrutinizer ignore-call */ booleanNode('collection')->defaultValue(true)->end()
Loading history...
137
                    ->end()
138
                ->end()
139
            ->end();
140
    }
141
142
    private function addUserNode(ArrayNodeDefinition $rootNode): void
143
    {
144
        $rootNode
145
            ->children()
146
                ->arrayNode('user')
147
                    ->addDefaultsIfNotSet()
148
                    ->children()
149
                        ->scalarNode('class_name')
150
                            ->isRequired()
151
                        ->end()
152
                        ->arrayNode('email_verification')
153
                            ->canBeDisabled()
154
                            ->addDefaultsIfNotSet()
155
                            ->children()
156
                                ->arrayNode('email')
157
                                    ->children()
158
                                        ->scalarNode('redirect_path_query')->end()
159
                                        ->scalarNode('default_redirect_path')->isRequired()->end()
160
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Please verify your email')->end()
161
                                    ->end()
162
                                ->end()
163
                                ->booleanNode('default_value')->isRequired()->end()
164
                                ->booleanNode('verify_on_change')->isRequired()->end()
165
                                ->booleanNode('verify_on_register')->isRequired()->end()
166
                                ->booleanNode('deny_unverified_login')->isRequired()->end()
167
                            ->end()
168
                        ->end()
169
                        ->arrayNode('new_email_confirmation')
170
                            ->addDefaultsIfNotSet()
171
                            ->children()
172
                                ->arrayNode('email')
173
                                    ->children()
174
                                        ->scalarNode('redirect_path_query')->end()
175
                                        ->scalarNode('default_redirect_path')->isRequired()->end()
176
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Please confirm your new email address')->end()
177
                                    ->end()
178
                                ->end()
179
                                ->integerNode('request_timeout_seconds')->defaultValue(86400)->end()
180
                            ->end()
181
                        ->end()
182
                        ->arrayNode('password_reset')
183
                            ->addDefaultsIfNotSet()
184
                            ->children()
185
                                ->arrayNode('email')
186
                                    ->children()
187
                                        ->scalarNode('redirect_path_query')->end()
188
                                        ->scalarNode('default_redirect_path')->isRequired()->end()
189
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Your password has been reset')->end()
190
                                    ->end()
191
                                ->end()
192
                                ->integerNode('repeat_ttl_seconds')->defaultValue(8600)->end()
193
                                ->integerNode('request_timeout_seconds')->defaultValue(3600)->end()
194
                            ->end()
195
                        ->end()
196
                        ->arrayNode('emails')
197
                            ->addDefaultsIfNotSet()
198
                            ->children()
199
                                ->arrayNode('welcome')
200
                                    ->canBeDisabled()
201
                                    ->addDefaultsIfNotSet()
202
                                    ->children()
203
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Welcome to {{ website_name }}')->end()
204
                                    ->end()
205
                                ->end()
206
                                ->arrayNode('user_enabled')
207
                                    ->canBeDisabled()
208
                                    ->addDefaultsIfNotSet()
209
                                    ->children()
210
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Your account has been enabled')->end()
211
                                    ->end()
212
                                ->end()
213
                                ->arrayNode('username_changed')
214
                                    ->canBeDisabled()
215
                                    ->addDefaultsIfNotSet()
216
                                    ->children()
217
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Your username has been updated')->end()
218
                                    ->end()
219
                                ->end()
220
                                ->arrayNode('password_changed')
221
                                    ->canBeDisabled()
222
                                    ->addDefaultsIfNotSet()
223
                                    ->children()
224
                                        ->scalarNode('subject')->cannotBeEmpty()->defaultValue('Your password has been changed')->end()
225
                                    ->end()
226
                                ->end()
227
                            ->end()
228
                        ->end()
229
                    ->end()
230
                ->end()
231
            ->end();
232
    }
233
}
234