setupAttributeAggregationAttributeConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
        $this->setupInformationRequestConfiguration($rootNode);
51
52
        return $treeBuilder;
53
    }
54
55
    private function setupLocaleConfiguration(ArrayNodeDefinition $rootNode)
56
    {
57
        $rootNode
58
            ->children()
59
                ->arrayNode('locales')
60
                    ->info('The available application locales')
61
                    ->isRequired()
62
                    ->prototype('scalar')
63
                        ->validate()
64
                            ->ifTrue(function ($locale) {
65
                                return !is_string($locale);
66
                            })
67
                            ->thenInvalid('Available application locales should be strings')
68
                        ->end()
69
                    ->end()
70
                ->end()
71
                ->scalarNode('default_locale')
72
                    ->info('The default application locale')
73
                    ->isRequired()
74
                    ->validate()
75
                        ->ifTrue(function ($locale) {
76
                            return !is_string($locale);
77
                        })
78
                        ->thenInvalid('Default application locale should be a string')
79
                    ->end()
80
                ->end()
81
                ->scalarNode('locale_cookie_domain')
82
                    ->info('The domain for which the locale cookie is set')
83
                    ->isRequired()
84
                    ->validate()
85
                        ->ifTrue(function ($domain) {
86
                            return !is_string($domain);
87
                        })
88
                        ->thenInvalid('Locale cookie domain should be a string')
89
                    ->end()
90
                ->end()
91
                ->scalarNode('locale_cookie_key')
92
                    ->info('The key for which the locale cookie value is set')
93
                    ->isRequired()
94
                    ->validate()
95
                        ->ifTrue(function ($key) {
96
                            return !is_string($key);
97
                        })
98
                        ->thenInvalid('Locale cookie key should be a string')
99
                    ->end()
100
                ->end()
101
                ->scalarNode('locale_cookie_expires_in')
102
                    ->info('The time interval after which the locale cookie expires; null gives a session cookie')
103
                    ->isRequired()
104
                    ->validate()
105
                        ->ifTrue(function ($expiresIn) {
106
                            if ($expiresIn === null) {
107
                                return false;
108
                            }
109
110
                            if (!is_string($expiresIn)) {
111
                                return true;
112
                            }
113
114
                            $now = new DateTimeImmutable();
115
                            $future = $now->modify($expiresIn);
116
117
                            return $now >= $future;
118
                        })
119
                        ->thenInvalid('Locale cookie expiration should be null or a positive DateTime modifier string, for example "+2 months"')
120
                    ->end()
121
                ->end()
122
                ->booleanNode('locale_cookie_secure')
123
                    ->isRequired()
124
                    ->info('Whether or not the locale cookie should be secure')
125
                ->end()
126
                ->booleanNode('locale_cookie_http_only')
127
                    ->isRequired()
128
                    ->info('Whether or not the locale cookie should be HTTP only')
129
                ->end()
130
            ->end();
131
    }
132
133
    private function setupAttributeSupportConfiguration(ArrayNodeDefinition $rootNode)
134
    {
135
        $rootNode
136
            ->children()
137
                ->arrayNode('attribute_support')
138
                    ->isRequired()
139
                    ->children()
140
                        ->scalarNode('email_to')
141
                            ->info('Email address to which attributes are sent')
142
                            ->isRequired()
143
                            ->validate()
144
                                ->ifTrue(function ($email) {
145
                                    return !is_string($email);
146
                                })
147
                                ->thenInvalid('Email address to which attributes are sent should be a string')
148
                            ->end()
149
                            ->validate()
150
                                ->ifTrue(function ($email) {
151
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
152
                                })
153
                                ->thenInvalid('Email address to which attributes are sent should be valid')
154
                            ->end()
155
                        ->end()
156
                        ->scalarNode('email_from')
157
                            ->info('mail address from which attributes are sent')
158
                            ->isRequired()
159
                            ->validate()
160
                                ->ifTrue(function ($email) {
161
                                    return !is_string($email);
162
                                })
163
                                ->thenInvalid('Email address from which attributes are sent should be a string')
164
                            ->end()
165
                            ->validate()
166
                                ->ifTrue(function ($email) {
167
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
168
                                })
169
                                ->thenInvalid('Email address from which attributes are sent should be valid')
170
                            ->end()
171
                        ->end()
172
                    ->end()
173
                ->end()
174
            ->end();
175
    }
176
177
    private function setupAttributeAggregationAttributeConfiguration(ArrayNodeDefinition $rootNode)
178
    {
179
180
        $protoType = $rootNode
181
            ->children()
182
                ->arrayNode('attribute_aggregation_supported_attributes')
183
                    ->isRequired()
184
                    ->info('A list of supported attributes by Attribute Aggregation')
185
                    ->requiresAtLeastOneElement()
186
                    ->useAttributeAsKey('type')
187
                    ->normalizeKeys(false)
188
                    ->prototype('array');
189
190
        $protoType
191
            ->children()
192
                ->scalarNode('logo_path')
193
                    ->info('The logo path of the AA attribute')
194
                    ->isRequired()
195
                    ->validate()
196
                        ->ifTrue(function ($logoPath) {
197
                            return !is_string($logoPath);
198
                        })
199
                        ->thenInvalid('The logo path of the AA attribute should be a string')
200
                    ->end()
201
                ->end()
202
                ->scalarNode('connect_url')
203
                    ->info('The connect url of the AA attribute')
204
                    ->isRequired()
205
                    ->validate()
206
                        ->ifTrue(function ($connectUrl) {
207
                            return !is_string($connectUrl);
208
                        })
209
                        ->thenInvalid('The connect url of the AA attribute should be a string')
210
                    ->end()
211
                ->end()
212
            ->end();
213
    }
214
215
    private function setupInformationRequestConfiguration(ArrayNodeDefinition $rootNode)
216
    {
217
        $rootNode
218
            ->children()
219
                ->arrayNode('information_request')
220
                    ->isRequired()
221
                    ->children()
222
                        ->scalarNode('email_to')
223
                            ->info('Email address to which the information request results are sent')
224
                            ->isRequired()
225
                            ->validate()
226
                                ->ifTrue(function ($email) {
227
                                    return !is_string($email);
228
                                })
229
                                ->thenInvalid('Email address to which information request results are sent should be a string')
230
                            ->end()
231
                            ->validate()
232
                                ->ifTrue(function ($email) {
233
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
234
                                })
235
                                ->thenInvalid('Email address to which information request results are sent should be valid')
236
                            ->end()
237
                        ->end()
238
                        ->scalarNode('email_from')
239
                            ->info('mail address from which information requests are sent')
240
                            ->isRequired()
241
                            ->validate()
242
                                ->ifTrue(function ($email) {
243
                                    return !is_string($email);
244
                                })
245
                                ->thenInvalid('Email address from which information requests are sent should be a string')
246
                            ->end()
247
                            ->validate()
248
                                ->ifTrue(function ($email) {
249
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
250
                                })
251
                                ->thenInvalid('Email address from which information requests are sent should be valid')
252
                            ->end()
253
                        ->end()
254
                    ->end()
255
                ->end()
256
            ->end();
257
    }
258
}
259