Passed
Push — master ( ad5a69...e2599e )
by
unknown
11:37
created

VMProApiExtension::loadInternal()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.0984

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 16
cts 19
cp 0.8421
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 8
nop 2
crap 5.0984
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
11
/**
12
 * Class VMProApiExtension.
13
 *
14
 * @author Ruben Knol <[email protected]>
15
 */
16
class VMProApiExtension extends ConfigurableExtension
17
{
18
    /**
19
     * Flatten multi-dimensional Symfony configuration array into a
20
     * one-dimensional parameters array.
21
     *
22
     * @param ContainerBuilder $container
23
     * @param array            $configs
24
     * @param string           $root
25
     *
26
     * @return array
27
     */
28 6
    private function flattenParametersFromConfig(ContainerBuilder $container, $configs, $root)
29
    {
30 6
        $parameters = [];
31
32 6
        foreach ($configs as $key => $value) {
33 6
            $parameterKey = sprintf('%s_%s', $root, $key);
34 6
            if (is_array($value)) {
35 6
                $parameters = array_merge(
36 6
                    $parameters,
37 6
                    $this->flattenParametersFromConfig($container, $value, $parameterKey)
38 6
                );
39
40 6
                continue;
41
            }
42
43 6
            $parameters[$parameterKey] = $value;
44 6
        }
45
46 6
        return $parameters;
47
    }
48
49
    /**
50
     * Load the appropriate service container configurations based on which
51
     * GuzzleHttp library is present in the project.
52
     *
53
     * @param array            $configs
54
     * @param ContainerBuilder $container
55
     */
56 6
    protected function loadInternal(array $configs, ContainerBuilder $container)
57
    {
58 6
        $parameters = $this->flattenParametersFromConfig($container, $configs, 'vm_pro_api');
59 6
        foreach ($parameters as $key => $value) {
60 6
            $container->setParameter($key, $value);
61 6
        }
62
63 6
        $loader = new YamlFileLoader(
64 6
            $container,
65 6
            new FileLocator(__DIR__.'/../Resources/config')
66 6
        );
67
68
        // Always load the services that will always be the same
69 6
        $loader->load('services/main.yml');
70
71
        // only load ratings service if the meta data fields are configured
72 6
        if (array_key_exists('vm_pro_api_rating_meta_data_fields_average', $parameters)
73 6
            && array_key_exists('vm_pro_api_rating_meta_data_fields_count', $parameters)) {
74
            $loader->load('services/ratings.yml');
75
        }
76
77
        // Dynamically load service configurations that are specific
78
        // to which Guzzle version is installed
79 6
        if (version_compare(ClientInterface::VERSION, '6.0', '>=')) {
80 6
            $loader->load('services/guzzle6.yml');
81 6
        } else {
82
            $loader->load('services/guzzle5.yml');
83
        }
84 6
    }
85
}
86