Completed
Push — master ( e28263...923cb7 )
by Tobias
43:39 queued 18:44
created

DependencyInjection/BazingaGeocoderExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of the BazingaGeocoderBundle package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
namespace Bazinga\Bundle\GeocoderBundle\DependencyInjection;
11
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Definition;
16
use Symfony\Component\DependencyInjection\Reference;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\Config\Definition\Processor;
19
20
/**
21
 * William Durand <[email protected]>.
22
 */
23
class BazingaGeocoderExtension extends Extension
24
{
25
    protected $container;
26
27
    public function load(array $configs, ContainerBuilder $container)
28
    {
29
        $this->container = $container;
30
31
        $processor = new Processor();
32
        $configuration = new Configuration();
33
        $config = $processor->processConfiguration($configuration, $configs);
34
35
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
36
        $loader->load('services.xml');
37
38
        if ($config['default_provider']) {
39
            $container->setParameter('bazinga_geocoder.default_provider', $config['default_provider']);
40
        }
41
42
        if (!empty($config['fake_ip']) && true === $config['fake_ip']['enabled']) {
43
            $definition = $container->getDefinition('bazinga_geocoder.event_listener.fake_request');
44
            $definition->replaceArgument(0, $config['fake_ip']['ip']);
45
46
            $tag = current($definition->getTag('kernel.event_listener'));
47
            $tag['priority'] = $config['fake_ip']['priority'];
48
            $tags = array('kernel.event_listener' => array($tag));
49
            $definition->setTags($tags);
50
        } else {
51
            $container->removeDefinition('bazinga_geocoder.event_listener.fake_request');
52
        }
53
54
        $container->setAlias('bazinga_geocoder.geocoder.adapter', $config['adapter']);
55
56
        if (isset($config['providers']['free_geo_ip'])) {
57
            $this->addProvider('free_geo_ip');
58
        }
59
60
        if (isset($config['providers']['host_ip'])) {
61
            $this->addProvider('host_ip');
62
        }
63
64
        if (isset($config['providers']['bing_maps'])) {
65
            $bingMapsParams = $config['providers']['bing_maps'];
66
67
            $this->addProvider('bing_maps', array(
68
                $bingMapsParams['api_key'],
69
                $bingMapsParams['locale'],
70
            ));
71
        }
72
73
        if (isset($config['providers']['ip_info_db'])) {
74
            $ipInfoDbParams = $config['providers']['ip_info_db'];
75
76
            $this->addProvider('ip_info_db', array(
77
                $ipInfoDbParams['api_key'],
78
            ));
79
        }
80
81 View Code Duplication
        if (isset($config['providers']['google_maps'])) {
82
            $googleMapsParams = $config['providers']['google_maps'];
83
84
            $this->addProvider('google_maps', array(
85
                $googleMapsParams['locale'],
86
                $googleMapsParams['region'],
87
                $googleMapsParams['use_ssl'],
88
                $googleMapsParams['api_key'],
89
            ));
90
        }
91
92 View Code Duplication
        if (isset($config['providers']['arcgis_online'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            $params = $config['providers']['arcgis_online'];
94
95
            $this->addProvider('arcgis_online', array(
96
                $params['source_country'],
97
                $params['use_ssl'],
98
            ));
99
        }
100
101 View Code Duplication
        if (isset($config['providers']['google_maps_business'])) {
102
            $googleMapsBusinessParams = $config['providers']['google_maps_business'];
103
104
            $this->addProvider('google_maps_business', array(
105
                $googleMapsBusinessParams['client_id'],
106
                $googleMapsBusinessParams['api_key'],
107
                $googleMapsBusinessParams['locale'],
108
                $googleMapsBusinessParams['region'],
109
                $googleMapsBusinessParams['use_ssl'],
110
            ));
111
        }
112
113
        if (isset($config['providers']['openstreetmap'])) {
114
            $openstreetMapsParams = $config['providers']['openstreetmap'];
115
116
            $this->addProvider('openstreetmap', array(
117
                $openstreetMapsParams['locale'],
118
            ));
119
        }
120
121
        if (isset($config['providers']['geoip'])) {
122
            $this->addProvider('geoip');
123
        }
124
125
        if (isset($config['providers']['mapquest'])) {
126
            $mapQuestParams = $config['providers']['mapquest'];
127
128
            $this->addProvider('mapquest', array(
129
                $mapQuestParams['api_key'],
130
            ));
131
        }
132
133
        if (isset($config['providers']['yandex'])) {
134
            $yandexParams = $config['providers']['yandex'];
135
136
            $this->addProvider('yandex', array(
137
                $yandexParams['locale'],
138
                $yandexParams['toponym'],
139
            ));
140
        }
141
142
        if (isset($config['providers']['geo_ips'])) {
143
            $geoIpsParams = $config['providers']['geo_ips'];
144
145
            $this->addProvider('geo_ips', array(
146
                $geoIpsParams['api_key'],
147
            ));
148
        }
149
150
        if (isset($config['providers']['geo_plugin'])) {
151
            $this->addProvider('geo_plugin');
152
        }
153
154
        if (isset($config['providers']['maxmind'])) {
155
            $maxmindParams = $config['providers']['maxmind'];
156
157
            $this->addProvider('maxmind', array(
158
                $maxmindParams['api_key'],
159
            ));
160
        }
161
162
        if (isset($config['providers']['maxmind_binary'])) {
163
            $maxmindBinaryParams = $config['providers']['maxmind_binary'];
164
165
            $provider = new Definition(
166
                '%bazinga_geocoder.geocoder.provider.maxmind_binary.class%',
167
                array(
168
                    $maxmindBinaryParams['binary_file'],
169
                    $maxmindBinaryParams['open_flag'],
170
                )
171
            );
172
173
            $provider
174
                ->setPublic(false)
175
                ->addTag('bazinga_geocoder.provider');
176
177
            $container->setDefinition('bazinga_geocoder.provider.maxmind_binary', $provider);
178
        }
179
180 View Code Duplication
        if (isset($config['providers']['opencage'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
            $openCageParams = $config['providers']['opencage'];
182
183
            $this->addProvider('opencage', array(
184
                $openCageParams['api_key'],
185
                $openCageParams['use_ssl'],
186
                $openCageParams['locale'],
187
            ));
188
        }
189
190
        if (isset($config['providers']['cache'])) {
191
            $params = $config['providers']['cache'];
192
            $cache = new Reference($params['adapter']);
193
            $fallback = new Reference('bazinga_geocoder.provider.'.$params['provider']);
194
195
            $provider = new Definition(
196
                '%bazinga_geocoder.geocoder.provider.cache.class%',
197
                array($cache, $fallback, $params['lifetime'])
198
            );
199
200
            if (isset($params['locale'])) {
201
                $provider->addArgument($params['locale']);
202
            }
203
204
            $provider
205
                ->setPublic(false)
206
                ->addTag('bazinga_geocoder.provider');
207
208
            $container->setDefinition('bazinga_geocoder.provider.cache', $provider);
209
        }
210
211
        if (isset($config['providers']['chain'])) {
212
            $chainProvider = new Definition(
213
                '%bazinga_geocoder.geocoder.provider.chain.class%'
214
            );
215
216
            $this->container->setDefinition('bazinga_geocoder.provider.chain', $chainProvider);
217
218
            $chainProvider
219
                ->setPublic(false)
220
                ->addTag('bazinga_geocoder.provider');
221
222
            if (isset($config['providers']['chain']['providers'])) {
223
                foreach ($config['providers']['chain']['providers'] as $name) {
224
                    if ($this->container->hasDefinition('bazinga_geocoder.provider.'.$name)) {
225
                        $chainProvider->addMethodCall('add', array(
226
                            $this->container->getDefinition('bazinga_geocoder.provider.'.$name),
227
                        ));
228
                    } else {
229
                        $chainProvider->addMethodCall('add', array(new Reference($name)));
230
                    }
231
                }
232
            }
233
        }
234
    }
235
236
    protected function addProvider($name, array $arguments = array())
237
    {
238
        $provider = new Definition(
239
            '%bazinga_geocoder.geocoder.provider.'.$name.'.class%',
240
            array_merge(
241
                array(new Reference('bazinga_geocoder.geocoder.adapter')),
242
                $arguments
243
            )
244
        );
245
246
        $provider
247
            ->setPublic(false)
248
            ->addTag('bazinga_geocoder.provider');
249
250
        $this->container->setDefinition('bazinga_geocoder.provider.'.$name, $provider);
251
    }
252
}
253