Completed
Push — master ( 054afb...cddf5a )
by Tomas
06:05 queued 11s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 33
cts 34
cp 0.9706
rs 9.296
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 35
    public function __construct($debug)
37
    {
38 35
        $this->debug = (bool) $debug;
39 35
    }
40
41
    /**
42
     * Proxy to get root node for Symfony < 4.2.
43
     *
44
     * @return ArrayNodeDefinition
45
     */
46 35
    protected function getRootNode(TreeBuilder $treeBuilder, string $name)
47
    {
48 35
        if (\method_exists($treeBuilder, 'getRootNode')) {
49 35
            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 35
    public function getConfigTreeBuilder()
61
    {
62 35
        $treeBuilder = new TreeBuilder('bazinga_geocoder');
63
64 35
        $this->getRootNode($treeBuilder, 'bazinga_geocoder')
65 35
            ->children()
66 35
            ->append($this->getProvidersNode())
67 35
            ->arrayNode('profiling')
68 35
                ->addDefaultsIfNotSet()
69 35
                ->treatFalseLike(['enabled' => false])
70 35
                ->treatTrueLike(['enabled' => true])
71 35
                ->treatNullLike(['enabled' => $this->debug])
72 35
                ->info('Extend the debug profiler with information about requests.')
73 35
                ->children()
74 35
                    ->booleanNode('enabled')
75 35
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
76 35
                        ->defaultValue($this->debug)
77 35
                    ->end()
78 35
                ->end()
79 35
            ->end()
80 35
            ->arrayNode('fake_ip')
81 35
                ->beforeNormalization()
82 35
                ->ifString()
83
                    ->then(function ($value) {
84
                        return ['ip' => $value];
85 35
                    })
86 35
                ->end()
87 35
                ->canBeEnabled()
88 35
                ->children()
89 35
                    ->scalarNode('local_ip')
90 35
                        ->defaultValue('127.0.0.1')
91 35
                    ->end()
92 35
                    ->scalarNode('ip')->defaultNull()->end()
93 35
                    ->booleanNode('use_faker')->defaultFalse()->end()
94 35
                ->end()
95 35
            ->end();
96
97 35
        return $treeBuilder;
98
    }
99
100
    /**
101
     * @return ArrayNodeDefinition
102
     */
103 35
    private function getProvidersNode()
104
    {
105 35
        $treeBuilder = new TreeBuilder('providers');
106
107 35
        return $this->getRootNode($treeBuilder, 'providers')
108 35
            ->requiresAtLeastOneElement()
109 35
            ->useAttributeAsKey('name')
110 35
            ->arrayPrototype()
111 35
            ->fixXmlConfig('plugin')
112 35
                ->children()
113 35
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
114 35
                    ->variableNode('options')->defaultValue([])->end()
115 35
                    ->scalarNode('cache')->defaultNull()->end()
116 35
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
117 35
                    ->scalarNode('cache_precision')
118 35
                        ->defaultNull()
119 35
                        ->info('Precision of the coordinates to cache.')
120 35
                        ->end()
121 35
                    ->scalarNode('limit')->defaultNull()->end()
122 35
                    ->scalarNode('locale')->defaultNull()->end()
123 35
                    ->scalarNode('logger')->defaultNull()->end()
124 35
                    ->arrayNode('aliases')
125 35
                        ->scalarPrototype()->end()
126 35
                    ->end()
127 35
                    ->append($this->createClientPluginNode())
128 35
                ->end()
129 35
            ->end();
130
    }
131
132
    /**
133
     * Create plugin node of a client.
134
     *
135
     * @return ArrayNodeDefinition The plugin node
136
     */
137 35
    private function createClientPluginNode()
138
    {
139 35
        $builder = new TreeBuilder('plugins');
140 35
        $node = $this->getRootNode($builder, 'plugins');
141
142
        /** @var ArrayNodeDefinition $pluginList */
143
        $pluginList = $node
144 35
            ->info('A list of plugin service ids. The order is important.')
145 35
            ->arrayPrototype()
146
        ;
147
        $pluginList
148
            // support having just a service id in the list
149 35
            ->beforeNormalization()
150
                ->always(function ($plugin) {
151 2
                    if (is_string($plugin)) {
152
                        return [
153
                            'reference' => [
154 2
                                'enabled' => true,
155 2
                                'id' => $plugin,
156
                            ],
157
                        ];
158
                    }
159
160 1
                    return $plugin;
161 35
                })
162 35
            ->end()
163
        ;
164
165
        $pluginList
166 35
            ->children()
167 35
                ->arrayNode('reference')
168 35
                    ->canBeEnabled()
169 35
                    ->info('Reference to a plugin service')
170 35
                    ->children()
171 35
                        ->scalarNode('id')
172 35
                            ->info('Service id of a plugin')
173 35
                            ->isRequired()
174 35
                            ->cannotBeEmpty()
175 35
                        ->end()
176 35
                    ->end()
177 35
                ->end()
178 35
            ->end()
179 35
        ->end();
180
181 35
        return $node;
182
    }
183
}
184