Completed
Push — master ( 01f5ec...920f25 )
by
unknown
12s
created

VMProApiExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 70
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A flattenParametersFromConfig() 0 20 3
B loadInternal() 0 29 5
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 3
            $loader->load('services/guzzle6.yml');
81 3
        } else {
82 3
            $loader->load('services/guzzle5.yml');
83
        }
84 6
    }
85
}
86