Completed
Push — master ( cfda18...16b364 )
by Tomas
05:21 queued 11s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 30
cts 31
cp 0.9677
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Bazinga\GeocoderBundle\DependencyInjection;
14
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * @author William Durand <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * Whether to use the debug mode.
26
     *
27
     * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
28
     *
29
     * @var bool
30
     */
31
    private $debug;
32
33
    /**
34
     * @param bool $debug
35
     */
36 33
    public function __construct($debug)
37
    {
38 33
        $this->debug = (bool) $debug;
39 33
    }
40
41
    /**
42
     * Proxy to get root node for Symfony < 4.2.
43
     *
44
     * @return ArrayNodeDefinition
45
     */
46 33
    protected function getRootNode(TreeBuilder $treeBuilder, string $name)
47
    {
48 33
        if (\method_exists($treeBuilder, 'getRootNode')) {
49 33
            return $treeBuilder->getRootNode();
50
        } else {
51
            return $treeBuilder->root($name);
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
    }
54
55
    /**
56
     * Generates the configuration tree builder.
57
     *
58
     * @return TreeBuilder The tree builder
59
     */
60 33
    public function getConfigTreeBuilder()
61
    {
62 33
        $treeBuilder = new TreeBuilder('bazinga_geocoder');
63
64 33
        $this->getRootNode($treeBuilder, 'bazinga_geocoder')
65 33
            ->children()
66 33
            ->append($this->getProvidersNode())
67 33
            ->arrayNode('profiling')
68 33
                ->addDefaultsIfNotSet()
69 33
                ->treatFalseLike(['enabled' => false])
70 33
                ->treatTrueLike(['enabled' => true])
71 33
                ->treatNullLike(['enabled' => $this->debug])
72 33
                ->info('Extend the debug profiler with information about requests.')
73 33
                ->children()
74 33
                    ->booleanNode('enabled')
75 33
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
76 33
                        ->defaultValue($this->debug)
77 33
                    ->end()
78 33
                ->end()
79 33
            ->end()
80 33
            ->arrayNode('fake_ip')
81 33
                ->beforeNormalization()
82 33
                ->ifString()
83
                    ->then(function ($value) {
84
                        return ['ip' => $value];
85 33
                    })
86 33
                ->end()
87 33
                ->canBeEnabled()
88 33
                ->children()
89 33
                    ->scalarNode('ip')->defaultNull()->end()
90 33
                    ->booleanNode('use_faker')->defaultFalse()->end()
91 33
                ->end()
92 33
            ->end();
93
94 33
        return $treeBuilder;
95
    }
96
97
    /**
98
     * @return ArrayNodeDefinition
99
     */
100 33
    private function getProvidersNode()
101
    {
102 33
        $treeBuilder = new TreeBuilder('providers');
103
104 33
        return $this->getRootNode($treeBuilder, 'providers')
105 33
            ->requiresAtLeastOneElement()
106 33
            ->useAttributeAsKey('name')
107 33
            ->arrayPrototype()
108 33
            ->fixXmlConfig('plugin')
109 33
                ->children()
110 33
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
111 33
                    ->variableNode('options')->defaultValue([])->end()
112 33
                    ->scalarNode('cache')->defaultNull()->end()
113 33
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
114 33
                    ->scalarNode('cache_precision')
115 33
                        ->defaultNull()
116 33
                        ->info('Precision of the coordinates to cache.')
117 33
                        ->end()
118 33
                    ->scalarNode('limit')->defaultNull()->end()
119 33
                    ->scalarNode('locale')->defaultNull()->end()
120 33
                    ->scalarNode('logger')->defaultNull()->end()
121 33
                    ->arrayNode('aliases')
122 33
                        ->scalarPrototype()->end()
123 33
                    ->end()
124 33
                    ->append($this->createClientPluginNode())
125 33
                ->end()
126 33
            ->end();
127
    }
128
129
    /**
130
     * Create plugin node of a client.
131
     *
132
     * @return ArrayNodeDefinition The plugin node
133
     */
134 33
    private function createClientPluginNode()
135
    {
136 33
        $builder = new TreeBuilder('plugins');
137 33
        $node = $this->getRootNode($builder, 'plugins');
138
139
        /** @var ArrayNodeDefinition $pluginList */
140
        $pluginList = $node
141 33
            ->info('A list of plugin service ids. The order is important.')
142 33
            ->arrayPrototype()
143
        ;
144
        $pluginList
145
            // support having just a service id in the list
146 33
            ->beforeNormalization()
147
                ->always(function ($plugin) {
148 2
                    if (is_string($plugin)) {
149
                        return [
150
                            'reference' => [
151 2
                                'enabled' => true,
152 2
                                'id' => $plugin,
153
                            ],
154
                        ];
155
                    }
156
157 1
                    return $plugin;
158 33
                })
159 33
            ->end()
160
        ;
161
162
        $pluginList
163 33
            ->children()
164 33
                ->arrayNode('reference')
165 33
                    ->canBeEnabled()
166 33
                    ->info('Reference to a plugin service')
167 33
                    ->children()
168 33
                        ->scalarNode('id')
169 33
                            ->info('Service id of a plugin')
170 33
                            ->isRequired()
171 33
                            ->cannotBeEmpty()
172 33
                        ->end()
173 33
                    ->end()
174 33
                ->end()
175 33
            ->end()
176 33
        ->end();
177
178 33
        return $node;
179
    }
180
}
181