Completed
Push — master ( 88f784...c08012 )
by Tobias
05:19
created

configureProviderPlugins()   F

Complexity

Conditions 14
Paths 483

Size

Total Lines 61

Duplication

Lines 18
Ratio 29.51 %

Code Coverage

Tests 24
CRAP Score 26.544

Importance

Changes 0
Metric Value
dl 18
loc 61
ccs 24
cts 40
cp 0.6
rs 2.818
c 0
b 0
f 0
cc 14
nc 483
nop 3
crap 26.544

How to fix   Long Method    Complexity   

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 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 Bazinga\GeocoderBundle\DataCollector\GeocoderDataCollector;
16
use Bazinga\GeocoderBundle\DependencyInjection\Compiler\FactoryValidatorPass;
17
use Bazinga\GeocoderBundle\Plugin\FakeIpPlugin;
18
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
19
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory;
20
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
21
use Geocoder\Plugin\Plugin\CachePlugin;
22
use Geocoder\Plugin\Plugin\LimitPlugin;
23
use Geocoder\Plugin\Plugin\LocalePlugin;
24
use Geocoder\Plugin\Plugin\LoggerPlugin;
25
use Geocoder\Plugin\PluginProvider;
26
use Geocoder\Provider\Provider;
27
use Symfony\Component\Config\Definition\Processor;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
31
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
32
use Symfony\Component\DependencyInjection\Reference;
33
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
34
use Symfony\Component\HttpKernel\Kernel;
35
36
/**
37
 * @author William Durand <[email protected]>.
38
 */
39
class BazingaGeocoderExtension extends Extension
40
{
41 31
    public function load(array $configs, ContainerBuilder $container)
42
    {
43 31
        $processor = new Processor();
44 31
        $configuration = $this->getConfiguration($configs, $container);
45 31
        $config = $processor->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 44 can be null; however, Symfony\Component\Config...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46
47 31
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
48 31
        $loader->load('services.yml');
49
50 31
        if (true === $config['profiling']['enabled']) {
51 3
            $loader->load('profiling.yml');
52
        }
53
54 31
        if ($config['fake_ip']['enabled']) {
55
            $definition = $container->getDefinition(FakeIpPlugin::class);
56
            $definition->replaceArgument(1, $config['fake_ip']['ip']);
57
        } else {
58 31
            $container->removeDefinition(FakeIpPlugin::class);
59
        }
60
61 31
        $this->loadProviders($container, $config);
62 31
    }
63
64 31
    private function loadProviders(ContainerBuilder $container, array $config)
65
    {
66 31
        foreach ($config['providers'] as $providerName => $providerConfig) {
67
            try {
68 29
                $factoryService = $container->getDefinition($providerConfig['factory']);
69 29
                $factoryClass = $factoryService->getClass() ?: $providerConfig['factory'];
70 29
                if (!$this->implementsProviderFactory($factoryClass)) {
71
                    throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory']));
72
                }
73
                // See if any option has a service reference
74 29
                $providerConfig['options'] = $this->findReferences($providerConfig['options']);
75 29
                $factoryClass::validate($providerConfig['options'], $providerName);
76
            } catch (ServiceNotFoundException $e) {
77
                // Assert: We are using a custom factory. If invalid config, it will be caught in FactoryValidatorPass
78
                $providerConfig['options'] = $this->findReferences($providerConfig['options']);
79
                FactoryValidatorPass::addFactoryServiceId($providerConfig['factory']);
80
            }
81
82 29
            $serviceId = 'bazinga_geocoder.provider.'.$providerName;
83 29
            $plugins = $this->configureProviderPlugins($container, $providerConfig, $serviceId);
84
85 29
            $def = $container->register($serviceId, PluginProvider::class)
86 29
                ->setFactory([PluginProviderFactory::class, 'createPluginProvider'])
87 29
                ->addArgument($plugins)
88 29
                ->addArgument(new Reference($providerConfig['factory']))
89 29
                ->addArgument($providerConfig['options']);
90
91 29
            $def->addTag('bazinga_geocoder.provider');
92 29
            foreach ($providerConfig['aliases'] as $alias) {
93
                $container->setAlias($alias, $serviceId);
94
            }
95
96 29
            if (Kernel::VERSION_ID > 40200) {
97 29
                $container->registerAliasForArgument($serviceId, Provider::class, "{$providerName}Geocoder");
98
            }
99
        }
100 31
    }
101
102
    /**
103
     * Configure plugins for a client.
104
     */
105 29
    public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array
106
    {
107 29
        $plugins = [];
108 29
        foreach ($config['plugins'] as $plugin) {
109 2
            if ($plugin['reference']['enabled']) {
110 2
                $plugins[] = $plugin['reference']['id'];
111
            }
112
        }
113
114 29
        if (isset($config['cache']) || isset($config['cache_lifetime']) || isset($config['cache_precision'])) {
115 2
            $cacheLifetime = isset($config['cache_lifetime']) ? (int) $config['cache_lifetime'] : null;
116
117 2
            if (null === $cacheServiceId = $config['cache']) {
118
                if (!$container->has('app.cache')) {
119
                    throw new \LogicException('You need to specify a service for cache.');
120
                }
121
                $cacheServiceId = 'app.cache';
122
            }
123 2
            $plugins[] = $providerServiceId.'.cache';
124 2
            $container->register($providerServiceId.'.cache', CachePlugin::class)
125 2
                ->setPublic(false)
126 2
                ->setArguments([new Reference($cacheServiceId), $cacheLifetime, $config['cache_precision']]);
127
        }
128
129 29 View Code Duplication
        if (isset($config['limit'])) {
0 ignored issues
show
Duplication introduced by
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...
130
            $plugins[] = $providerServiceId.'.limit';
131
            $container->register($providerServiceId.'.limit', LimitPlugin::class)
132
                ->setPublic(false)
133
                ->setArguments([(int) $config['limit']]);
134
        }
135
136 29 View Code Duplication
        if (isset($config['locale'])) {
0 ignored issues
show
Duplication introduced by
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...
137
            $plugins[] = $providerServiceId.'.locale';
138
            $container->register($providerServiceId.'.locale', LocalePlugin::class)
139
                ->setPublic(false)
140
                ->setArguments([$config['locale']]);
141
        }
142
143 29 View Code Duplication
        if (isset($config['logger'])) {
0 ignored issues
show
Duplication introduced by
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...
144
            $plugins[] = $providerServiceId.'.logger';
145
            $container->register($providerServiceId.'.logger', LoggerPlugin::class)
146
                ->setPublic(false)
147
                ->setArguments([new Reference($config['logger'])]);
148
        }
149
150 29
        if ($container->has(FakeIpPlugin::class)) {
151
            $plugins[] = FakeIpPlugin::class;
152
        }
153
154 29
        if ($container->has(GeocoderDataCollector::class)) {
155 1
            $plugins[] = $providerServiceId.'.profiler';
156 1
            $container->register($providerServiceId.'.profiler', ProfilingPlugin::class)
157 1
                ->setPublic(false)
158 1
                ->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))])
159 1
                ->addTag('bazinga_geocoder.profiling_plugin');
160
        }
161
162
        return array_map(function (string $id) {
163 4
            return new Reference($id);
164 29
        }, $plugins);
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 31
    public function getConfiguration(array $config, ContainerBuilder $container)
171
    {
172 31
        return new Configuration($container->getParameter('kernel.debug'));
173
    }
174
175 29
    private function findReferences(array $options): array
176
    {
177 29
        foreach ($options as $key => $value) {
178 21
            if (is_array($value)) {
179 1
                $options[$key] = $this->findReferences($value);
180 21
            } elseif ('_service' === substr((string) $key, -8) || 0 === strpos((string) $value, '@') || 'service' === $key) {
181
                $options[$key] = new Reference(ltrim($value, '@'));
182
            }
183
        }
184
185 29
        return $options;
186
    }
187
188
    /**
189
     * @param mixed $factoryClass
190
     */
191 29
    private function implementsProviderFactory($factoryClass): bool
192
    {
193 29
        if (false === $interfaces = class_implements($factoryClass)) {
194
            return false;
195
        }
196
197 29
        return in_array(ProviderFactoryInterface::class, $interfaces, true);
198
    }
199
}
200