Completed
Pull Request — master (#2)
by nicolas
03:28
created

DekaleeCdn77Extension::prepend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Dekalee\Cdn77Bundle\DependencyInjection;
4
5
use Dekalee\Cdn77\Query\CreateResourceQuery;
6
use Dekalee\Cdn77\Query\ListResourcesQuery;
7
use Dekalee\Cdn77\Query\PurgeAllQuery;
8
use Dekalee\Cdn77\Query\PurgeFileQuery;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
use Symfony\Component\DependencyInjection\Loader;
14
15
/**
16
 * This is the class that loads and manages your bundle configuration
17
 *
18
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
19
 */
20
class DekaleeCdn77Extension extends Extension implements PrependExtensionInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function load(array $configs, ContainerBuilder $container)
26
    {
27
        $configuration = new Configuration();
28
        $config = $this->processConfiguration($configuration, $configs);
29
30
        $container->setParameter('dekalee_cdn77.api.login', $config['login']);
31
        $container->setParameter('dekalee_cdn77.api.password', $config['password']);
32
33
        foreach ([
34
            'list' => ListResourcesQuery::URL,
35
            'purge' => PurgeFileQuery::URL,
36
            'purge_all' => PurgeAllQuery::URL,
37
            'create' => CreateResourceQuery::URL,
38
        ] as $queryType => $url) {
39
            if (array_key_exists($queryType, $config['url'])) {
40
                $url = $config['url'][$queryType];
41
            }
42
            $container->setParameter('dekalee_cdn77.url.' . $queryType, $url);
43
        }
44
45
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
        $loader->load('query.yml');
47
48
        $bundles = $container->getParameter('kernel.bundles');
49
        if (!array_key_exists('GuzzleBundle', $bundles)) {
50
            $loader->load('client.yml');
51
        }
52
    }
53
54
    /**
55
     * Allow an extension to prepend the extension configurations.
56
     *
57
     * @param ContainerBuilder $container
58
     */
59
    public function prepend(ContainerBuilder $container)
60
    {
61
        $bundles = $container->getParameter('kernel.bundles');
62
63
        if (array_key_exists('GuzzleBundle', $bundles)) {
64
            $config = $container->getExtensionConfig('guzzle');
65
            $config[0]['clients']['cdn77'] = null;
66
            $processedConfiguration = $this->processConfiguration(new \EightPoints\Bundle\GuzzleBundle\DependencyInjection\Configuration('guzzle'), $config);
67
68
            $container->prependExtensionConfig('guzzle', $processedConfiguration);
69
        }
70
    }
71
}
72