Passed
Push — master ( 3cb906...aafecf )
by
unknown
04:36
created

VMProApiExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 74
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

2 Methods

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