Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 68
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 9.2447
c 0
b 0
f 0
cc 2
eloc 60
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Settings Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\SettingsBundle\DependencyInjection;
18
19
use SWP\Bundle\SettingsBundle\Model\SettingsInterface;
20
use SWP\Bundle\SettingsBundle\Model\SettingsRepository;
21
use SWP\Bundle\SettingsBundle\Model\Settings;
22
use SWP\Component\Storage\Factory\Factory;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
26
27
/**
28
 * This is the class that validates and merges configuration from your app/config files.
29
 *
30
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
31
 */
32
class Configuration implements ConfigurationInterface
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getConfigTreeBuilder()
38
    {
39
        $treeBuilder = new TreeBuilder();
40
        $treeBuilder->root('swp_settings')
41
            ->children()
42
                ->scalarNode('cache_service')->defaultNull()->info('A service implementing Psr\Cache\CacheItemPoolInterface')->end()
43
                ->integerNode('cache_lifetime')->defaultValue(3600)->end()
44
                ->arrayNode('persistence')
45
                    ->addDefaultsIfNotSet()
46
                    ->children()
47
                        ->arrayNode('orm')
48
                            ->addDefaultsIfNotSet()
49
                            ->canBeEnabled()
50
                            ->children()
51
                                ->arrayNode('classes')
52
                                ->addDefaultsIfNotSet()
53
                                ->children()
54
                                    ->arrayNode('settings')
55
                                        ->addDefaultsIfNotSet()
56
                                        ->children()
57
                                            ->scalarNode('model')->cannotBeEmpty()->defaultValue(Settings::class)->end()
58
                                            ->scalarNode('repository')->defaultValue(SettingsRepository::class)->end()
59
                                            ->scalarNode('factory')->defaultValue(Factory::class)->end()
60
                                            ->scalarNode('interface')->defaultValue(SettingsInterface::class)->end()
61
                                            ->scalarNode('object_manager_name')->defaultValue(null)->end()
62
                                        ->end()
63
                                    ->end()
64
                                ->end() // classes
65
                                ->end() // array node
66
                            ->end()
67
                        ->end()
68
                    ->end()
69
                ->end()
70
                ->arrayNode('settings')
71
                    ->useAttributeAsKey('settings')
72
                    ->prototype('array')
73
                        ->addDefaultsIfNotSet()
74
                        ->children()
75
                            ->scalarNode('scope')->defaultValue('global')->end()
76
                            ->scalarNode('value')
77
                                ->beforeNormalization()
78
                                ->ifArray()
79
                                ->then(function ($value) {
80
                                    return json_encode($value);
81
                                })
82
                                ->end()
83
                                ->defaultValue(null)
84
                            ->end()
85
                            ->scalarNode('type')
86
                                ->defaultValue('string')
87
                                ->validate()
88
                                    ->always(function ($v) {
89
                                        if (!in_array($v, ['string', 'array'])) {
90
                                            throw new InvalidTypeException();
91
                                        }
92
93
                                        return $v;
94
                                    })
95
                                ->end()
96
                            ->end()
97
                        ->end()
98
                    ->end()
99
                ->end()
100
            ->end()
101
        ;
102
103
        return $treeBuilder;
104
    }
105
}
106