Completed
Push — develop ( 4662d2...c4df36 )
by A.
04:33
created

Configuration::setupLocaleConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
rs 9.125
cc 1
eloc 42
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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
22
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
23
use Symfony\Component\Config\Definition\ConfigurationInterface;
24
25
class Configuration implements ConfigurationInterface
26
{
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $rootNode = $treeBuilder->root('open_conext_profile');
31
32
        $rootNode
33
            ->children()
34
                ->scalarNode('engine_block_entity_id')
35
                    ->info('The EntityID of EngineBlock')
36
                    ->isRequired()
37
                    ->validate()
38
                        ->ifTrue(function ($entityId) {
39
                            return !is_string($entityId);
40
                        })
41
                        ->thenInvalid('EngineBlock EntityID should be a string')
42
                    ->end()
43
                ->end()
44
            ->end();
45
46
        $this->setupLocaleConfiguration($rootNode);
47
        $this->setupAttributeSupportConfiguration($rootNode);
48
49
        return $treeBuilder;
50
    }
51
52
    private function setupLocaleConfiguration(ArrayNodeDefinition $rootNode)
53
    {
54
        $rootNode
55
            ->children()
56
                ->arrayNode('locales')
57
                    ->info('The available application locales')
58
                    ->isRequired()
59
                    ->prototype('scalar')
60
                        ->validate()
61
                            ->ifTrue(function ($locale) {
62
                                return !is_string($locale);
63
                            })
64
                            ->thenInvalid('Available application locales should be strings')
65
                        ->end()
66
                    ->end()
67
                ->end()
68
                ->scalarNode('default_locale')
69
                    ->info('The default application locale')
70
                    ->isRequired()
71
                    ->validate()
72
                        ->ifTrue(function ($locale) {
73
                            return !is_string($locale);
74
                        })
75
                        ->thenInvalid('Default application locale should be a string')
76
                    ->end()
77
                ->end()
78
                ->scalarNode('locale_cookie_domain')
79
                    ->info('The domain for which the locale cookie is set')
80
                    ->isRequired()
81
                    ->validate()
82
                        ->ifTrue(function ($domain) {
83
                            return !is_string($domain);
84
                        })
85
                        ->thenInvalid('Locale cookie domain should be a string')
86
                    ->end()
87
                ->end()
88
                ->scalarNode('locale_cookie_key')
89
                    ->info('The key for which the locale cookie value is set')
90
                    ->isRequired()
91
                    ->validate()
92
                        ->ifTrue(function ($key) {
93
                            return !is_string($key);
94
                        })
95
                        ->thenInvalid('Locale cookie key should be a string')
96
                    ->end()
97
                ->end()
98
            ->end();
99
    }
100
101
    private function setupAttributeSupportConfiguration(ArrayNodeDefinition $rootNode)
102
    {
103
        $rootNode
104
            ->children()
105
                ->arrayNode('attribute_support')
106
                    ->isRequired()
107
                    ->children()
108
                        ->scalarNode('email_to')
109
                            ->info('Email address to which attributes are sent')
110
                            ->isRequired()
111
                            ->validate()
112
                                ->ifTrue(function ($email) {
113
                                    return !is_string($email);
114
                                })
115
                                ->thenInvalid('Email address to which attributes are sent should be a string')
116
                            ->end()
117
                            ->validate()
118
                                ->ifTrue(function ($email) {
119
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
120
                                })
121
                                ->thenInvalid('Email address to which attributes are sent should be valid')
122
                            ->end()
123
                        ->end()
124
                        ->scalarNode('email_from')
125
                            ->info('mail address from which attributes are sent')
126
                            ->isRequired()
127
                            ->validate()
128
                                ->ifTrue(function ($email) {
129
                                    return !is_string($email);
130
                                })
131
                                ->thenInvalid('Email address from which attributes are sent should be a string')
132
                            ->end()
133
                            ->validate()
134
                                ->ifTrue(function ($email) {
135
                                    return !filter_var($email, FILTER_VALIDATE_EMAIL);
136
                                })
137
                                ->thenInvalid('Email address from which attributes are sent should be valid')
138
                            ->end()
139
                        ->end()
140
                    ->end()
141
                ->end()
142
            ->end();
143
    }
144
}
145