Completed
Push — develop ( 5dc85b...609c43 )
by A.
03:22
created

setupAttributeSupportConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
rs 8.8571
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
50
        return $treeBuilder;
51
    }
52
53
    private function setupLocaleConfiguration(ArrayNodeDefinition $rootNode)
54
    {
55
        $rootNode
56
            ->children()
57
                ->arrayNode('locales')
58
                    ->info('The available application locales')
59
                    ->isRequired()
60
                    ->prototype('scalar')
61
                        ->validate()
62
                            ->ifTrue(function ($locale) {
63
                                return !is_string($locale);
64
                            })
65
                            ->thenInvalid('Available application locales should be strings')
66
                        ->end()
67
                    ->end()
68
                ->end()
69
                ->scalarNode('default_locale')
70
                    ->info('The default application locale')
71
                    ->isRequired()
72
                    ->validate()
73
                        ->ifTrue(function ($locale) {
74
                            return !is_string($locale);
75
                        })
76
                        ->thenInvalid('Default application locale should be a string')
77
                    ->end()
78
                ->end()
79
                ->scalarNode('locale_cookie_domain')
80
                    ->info('The domain for which the locale cookie is set')
81
                    ->isRequired()
82
                    ->validate()
83
                        ->ifTrue(function ($domain) {
84
                            return !is_string($domain);
85
                        })
86
                        ->thenInvalid('Locale cookie domain should be a string')
87
                    ->end()
88
                ->end()
89
                ->scalarNode('locale_cookie_key')
90
                    ->info('The key for which the locale cookie value is set')
91
                    ->isRequired()
92
                    ->validate()
93
                        ->ifTrue(function ($key) {
94
                            return !is_string($key);
95
                        })
96
                        ->thenInvalid('Locale cookie key should be a string')
97
                    ->end()
98
                ->end()
99
                ->scalarNode('locale_cookie_expires_in')
100
                    ->info('The time interval after which the locale cookie expires; null gives a session cookie')
101
                    ->isRequired()
102
                    ->validate()
103
                        ->ifTrue(function ($expiresIn) {
104
                            if ($expiresIn === null) {
105
                                return false;
106
                            }
107
108
                            if (!is_string($expiresIn)) {
109
                                return true;
110
                            }
111
112
                            $now = new DateTimeImmutable();
113
                            $future = $now->modify($expiresIn);
114
115
                            return $now >= $future;
116
                        })
117
                        ->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...
118
                    ->end()
119
                ->end()
120
                ->booleanNode('locale_cookie_secure')
121
                    ->isRequired()
122
                    ->info('Whether or not the locale cookie should be secure')
123
                ->end()
124
                ->booleanNode('locale_cookie_http_only')
125
                    ->isRequired()
126
                    ->info('Whether or not the locale cookie should be HTTP only')
127
                ->end()
128
            ->end();
129
    }
130
131
    private function setupAttributeSupportConfiguration(ArrayNodeDefinition $rootNode)
132
    {
133
        $rootNode
134
            ->children()
135
                ->arrayNode('attribute_support')
136
                    ->isRequired()
137
                    ->children()
138
                        ->scalarNode('email_to')
139
                            ->info('Email address to which attributes are sent')
140
                            ->isRequired()
141
                            ->validate()
142
                                ->ifTrue(function ($email) {
143
                                    return !is_string($email);
144
                                })
145
                                ->thenInvalid('Email address to which attributes are sent should be a string')
146
                            ->end()
147
                            ->validate()
148
                                ->ifTrue(function ($email) {
149
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
150
                                })
151
                                ->thenInvalid('Email address to which attributes are sent should be valid')
152
                            ->end()
153
                        ->end()
154
                        ->scalarNode('email_from')
155
                            ->info('mail address from which attributes are sent')
156
                            ->isRequired()
157
                            ->validate()
158
                                ->ifTrue(function ($email) {
159
                                    return !is_string($email);
160
                                })
161
                                ->thenInvalid('Email address from which attributes are sent should be a string')
162
                            ->end()
163
                            ->validate()
164
                                ->ifTrue(function ($email) {
165
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
166
                                })
167
                                ->thenInvalid('Email address from which attributes are sent should be valid')
168
                            ->end()
169
                        ->end()
170
                    ->end()
171
                ->end()
172
            ->end();
173
    }
174
}
175