Completed
Pull Request — develop (#80)
by
unknown
01:52
created

Configuration::setupLocaleConfiguration()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 77
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 8.9342
c 0
b 0
f 0
cc 3
eloc 65
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\DependencyInjection;
20
21
use DateTimeImmutable;
22
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
26
class Configuration implements ConfigurationInterface
27
{
28
    public function getConfigTreeBuilder()
29
    {
30
        $treeBuilder = new TreeBuilder();
31
        $rootNode = $treeBuilder->root('open_conext_profile');
32
33
        $rootNode
34
            ->children()
35
                ->scalarNode('engine_block_entity_id')
36
                    ->info('The EntityID of EngineBlock')
37
                    ->isRequired()
38
                    ->validate()
39
                        ->ifTrue(function ($entityId) {
40
                            return !is_string($entityId);
41
                        })
42
                        ->thenInvalid('EngineBlock EntityID should be a string')
43
                    ->end()
44
                ->end()
45
            ->end();
46
47
        $this->setupLocaleConfiguration($rootNode);
48
        $this->setupAttributeSupportConfiguration($rootNode);
49
        $this->setupAttributeAggregationAttributeConfiguration($rootNode);
50
51
        return $treeBuilder;
52
    }
53
54
    private function setupLocaleConfiguration(ArrayNodeDefinition $rootNode)
55
    {
56
        $rootNode
57
            ->children()
58
                ->arrayNode('locales')
59
                    ->info('The available application locales')
60
                    ->isRequired()
61
                    ->prototype('scalar')
62
                        ->validate()
63
                            ->ifTrue(function ($locale) {
64
                                return !is_string($locale);
65
                            })
66
                            ->thenInvalid('Available application locales should be strings')
67
                        ->end()
68
                    ->end()
69
                ->end()
70
                ->scalarNode('default_locale')
71
                    ->info('The default application locale')
72
                    ->isRequired()
73
                    ->validate()
74
                        ->ifTrue(function ($locale) {
75
                            return !is_string($locale);
76
                        })
77
                        ->thenInvalid('Default application locale should be a string')
78
                    ->end()
79
                ->end()
80
                ->scalarNode('locale_cookie_domain')
81
                    ->info('The domain for which the locale cookie is set')
82
                    ->isRequired()
83
                    ->validate()
84
                        ->ifTrue(function ($domain) {
85
                            return !is_string($domain);
86
                        })
87
                        ->thenInvalid('Locale cookie domain should be a string')
88
                    ->end()
89
                ->end()
90
                ->scalarNode('locale_cookie_key')
91
                    ->info('The key for which the locale cookie value is set')
92
                    ->isRequired()
93
                    ->validate()
94
                        ->ifTrue(function ($key) {
95
                            return !is_string($key);
96
                        })
97
                        ->thenInvalid('Locale cookie key should be a string')
98
                    ->end()
99
                ->end()
100
                ->scalarNode('locale_cookie_expires_in')
101
                    ->info('The time interval after which the locale cookie expires; null gives a session cookie')
102
                    ->isRequired()
103
                    ->validate()
104
                        ->ifTrue(function ($expiresIn) {
105
                            if ($expiresIn === null) {
106
                                return false;
107
                            }
108
109
                            if (!is_string($expiresIn)) {
110
                                return true;
111
                            }
112
113
                            $now = new DateTimeImmutable();
114
                            $future = $now->modify($expiresIn);
115
116
                            return $now >= $future;
117
                        })
118
                        ->thenInvalid('Locale cookie expiration should be null or a positive DateTime modifier string, for example "+2 months"')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
119
                    ->end()
120
                ->end()
121
                ->booleanNode('locale_cookie_secure')
122
                    ->isRequired()
123
                    ->info('Whether or not the locale cookie should be secure')
124
                ->end()
125
                ->booleanNode('locale_cookie_http_only')
126
                    ->isRequired()
127
                    ->info('Whether or not the locale cookie should be HTTP only')
128
                ->end()
129
            ->end();
130
    }
131
132
    private function setupAttributeSupportConfiguration(ArrayNodeDefinition $rootNode)
133
    {
134
        $rootNode
135
            ->children()
136
                ->arrayNode('attribute_support')
137
                    ->isRequired()
138
                    ->children()
139
                        ->scalarNode('email_to')
140
                            ->info('Email address to which attributes are sent')
141
                            ->isRequired()
142
                            ->validate()
143
                                ->ifTrue(function ($email) {
144
                                    return !is_string($email);
145
                                })
146
                                ->thenInvalid('Email address to which attributes are sent should be a string')
147
                            ->end()
148
                            ->validate()
149
                                ->ifTrue(function ($email) {
150
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
151
                                })
152
                                ->thenInvalid('Email address to which attributes are sent should be valid')
153
                            ->end()
154
                        ->end()
155
                        ->scalarNode('email_from')
156
                            ->info('mail address from which attributes are sent')
157
                            ->isRequired()
158
                            ->validate()
159
                                ->ifTrue(function ($email) {
160
                                    return !is_string($email);
161
                                })
162
                                ->thenInvalid('Email address from which attributes are sent should be a string')
163
                            ->end()
164
                            ->validate()
165
                                ->ifTrue(function ($email) {
166
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
167
                                })
168
                                ->thenInvalid('Email address from which attributes are sent should be valid')
169
                            ->end()
170
                        ->end()
171
                    ->end()
172
                ->end()
173
            ->end();
174
    }
175
176
    private function setupAttributeAggregationAttributeConfiguration(ArrayNodeDefinition $rootNode)
177
    {
178
179
        $protoType = $rootNode
180
            ->children()
181
                ->arrayNode('attribute_aggregation_supported_attributes')
182
                    ->isRequired()
183
                    ->info('A list of supported attributes by Attribute Aggregation')
184
                    ->requiresAtLeastOneElement()
185
                    ->useAttributeAsKey('type')
186
                    ->normalizeKeys(false)
187
                    ->prototype('array');
188
189
        $protoType
190
            ->children()
191
                ->scalarNode('logo_path')
192
                    ->info('The logo path of the AA attribute')
193
                    ->isRequired()
194
                    ->validate()
195
                        ->ifTrue(function ($logoPath) {
196
                            return !is_string($logoPath);
197
                        })
198
                        ->thenInvalid('The logo path of the AA attribute should be a string')
199
                    ->end()
200
                ->end()
201
                ->scalarNode('connect_url')
202
                    ->info('The connect url of the AA attribute')
203
                    ->isRequired()
204
                    ->validate()
205
                        ->ifTrue(function ($connectUrl) {
206
                            return !is_string($connectUrl);
207
                        })
208
                        ->thenInvalid('The connect url of the AA attribute should be a string')
209
                    ->end()
210
                ->end()
211
                ->scalarNode('disconnect_url')
212
                    ->info('The disconnect url of the AA attribute')
213
                    ->isRequired()
214
                    ->validate()
215
                        ->ifTrue(function ($disconnectUrl) {
216
                            return !is_string($disconnectUrl);
217
                        })
218
                        ->thenInvalid('The disconnect url of the AA attribute should be a string')
219
                    ->end()
220
                ->end()
221
            ->end();
222
    }
223
}
224