VMProApiExtension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 63
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A flattenParametersFromConfig() 0 20 3
A loadInternal() 0 29 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MovingImage\Bundle\VMProApiBundle\DependencyInjection;
6
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
11
use Symfony\Component\HttpKernel\Kernel;
12
13
class VMProApiExtension extends ConfigurableExtension
14
{
15
    /**
16
     * Flatten multi-dimensional Symfony configuration array into a
17
     * one-dimensional parameters array.
18
     */
19
    private function flattenParametersFromConfig(ContainerBuilder $container, array $configs, string $root): array
20
    {
21
        $parameters = [];
22
23
        foreach ($configs as $key => $value) {
24
            $parameterKey = sprintf('%s_%s', $root, $key);
25
            if (is_array($value)) {
26
                $parameters = array_merge(
27
                    $parameters,
28
                    $this->flattenParametersFromConfig($container, $value, $parameterKey)
29 6
                );
30
31 6
                continue;
32
            }
33 6
34 6
            $parameters[$parameterKey] = $value;
35 6
        }
36 6
37 6
        return $parameters;
38 6
    }
39 6
40
    /**
41 6
     * Load the appropriate service container configurations based on which
42
     * GuzzleHttp library is present in the project.
43
     *
44 6
     * @throws \Exception
45 6
     */
46
    protected function loadInternal(array $configs, ContainerBuilder $container): void
47 6
    {
48
        $parameters = $this->flattenParametersFromConfig($container, $configs, 'vm_pro_api');
49
        foreach ($parameters as $key => $value) {
50
            $container->setParameter($key, $value);
51
        }
52
53
        $loader = new YamlFileLoader(
54
            $container,
55
            new FileLocator(__DIR__.'/../Resources/config')
56
        );
57 6
58
        // Always load the services that will always be the same
59 6
        $loader->load('services/main.yml');
60 6
61 6
        // only load ratings service if the meta data fields are configured
62 6
        if (array_key_exists('vm_pro_api_rating_meta_data_fields_average', $parameters)
63
            && array_key_exists('vm_pro_api_rating_meta_data_fields_count', $parameters)) {
64 6
            $loader->load('services/ratings.yml');
65 6
        }
66 6
67 6
        // Dynamically load service configurations that are specific
68
        // to which Guzzle version is installed
69
        $loader->load('services/guzzle6.yml');
70 6
71
        if (version_compare(Kernel::VERSION, '3.3', '>=')) {
72
            $loader->load('services/symfony3.yml');
73 6
        }
74 6
    }
75
}
76