Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 8.7418
c 0
b 0
f 0
cc 1
nc 1
nop 0

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\EngineBlockApiClientBundle\DependencyInjection;
20
21
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
22
use Symfony\Component\Config\Definition\ConfigurationInterface;
23
24
class Configuration implements ConfigurationInterface
25
{
26
    public function getConfigTreeBuilder()
27
    {
28
        $treeBuilder = new TreeBuilder();
29
        $rootNode = $treeBuilder->root('open_conext_engine_block_api_client');
30
31
        $rootNode
32
            ->children()
33
                ->arrayNode('http_client')
34
                    ->isRequired()
35
                    ->children()
36
                        ->scalarNode('base_url')
37
                            ->info('The base URL of the EngineBlock API host')
38
                            ->isRequired()
39
                            ->cannotBeEmpty()
40
                            ->validate()
41
                                ->ifTrue(function ($baseUrl) {
42
                                    return !is_string($baseUrl);
43
                                })
44
                                ->thenInvalid('The EngineBlock API base URL should be a string')
45
                            ->end()
46
                            ->validate()
47
                                ->ifTrue(function ($baseUrl) {
48
                                    return !filter_var($baseUrl, FILTER_VALIDATE_URL);
49
                                })
50
                                ->thenInvalid('The EngineBlock API base URL should be a valid URL')
51
                            ->end()
52
                            ->validate()
53
                                ->ifTrue(function ($baseUrl) {
54
                                    $path = parse_url($baseUrl, PHP_URL_PATH);
55
56
                                    return $path[strlen($path)-1] !== '/';
57
                                })
58
                                ->thenInvalid('The EngineBlock API base URL must end in a forward slash')
59
                            ->end()
60
                        ->end()
61
                        ->booleanNode('verify_ssl')
62
                            ->defaultTrue()
63
                        ->end()
64
                        ->scalarNode('username')
65
                            ->info('The username of the API user')
66
                            ->isRequired()
67
                            ->cannotBeEmpty()
68
                            ->validate()
69
                                ->ifTrue(function ($username) {
70
                                    return !is_string($username);
71
                                })
72
                                ->thenInvalid('The EngineBlock API username should be a string')
73
                            ->end()
74
                        ->end()
75
                        ->scalarNode('password')
76
                            ->info('The password of the API user')
77
                            ->isRequired()
78
                            ->cannotBeEmpty()
79
                            ->validate()
80
                                ->ifTrue(function ($password) {
81
                                    return !is_string($password);
82
                                })
83
                                ->thenInvalid('The EngineBlock API password should be a string')
84
                            ->end()
85
                        ->end()
86
                    ->end()
87
                ->end()
88
            ->end();
89
90
        return $treeBuilder;
91
    }
92
}
93