Completed
Pull Request — develop (#79)
by
unknown
02:13
created

setupInformationRequestConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 37
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->setupInformationRequestConfiguration($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 setupInformationRequestConfiguration(ArrayNodeDefinition $rootNode)
177
    {
178
        $rootNode
179
            ->children()
180
                ->arrayNode('information_request')
181
                    ->isRequired()
182
                    ->children()
183
                        ->scalarNode('email_to')
184
                            ->info('Email address to which the information request results are sent')
185
                            ->isRequired()
186
                            ->validate()
187
                                ->ifTrue(function ($email) {
188
                                    return !is_string($email);
189
                                })
190
                                ->thenInvalid('Email address to which information request results are sent should be a string')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 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...
191
                            ->end()
192
                            ->validate()
193
                                ->ifTrue(function ($email) {
194
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
195
                                })
196
                                ->thenInvalid('Email address to which information request results are sent should be valid')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 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...
197
                            ->end()
198
                        ->end()
199
                        ->scalarNode('email_from')
200
                            ->info('mail address from which information requests are sent')
201
                            ->isRequired()
202
                            ->validate()
203
                                ->ifTrue(function ($email) {
204
                                    return !is_string($email);
205
                                })
206
                                ->thenInvalid('Email address from which information requests are sent should be a string')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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...
207
                            ->end()
208
                            ->validate()
209
                                ->ifTrue(function ($email) {
210
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
211
                                })
212
                                ->thenInvalid('Email address from which information requests are sent should be valid')
213
                            ->end()
214
                        ->end()
215
                    ->end()
216
                ->end()
217
            ->end();
218
    }
219
}
220