Completed
Push — master ( b1583f...5418f9 )
by Christian
23s queued 14s
created

XabbuhPandaExtension::load()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 4.0005

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 29
cts 30
cp 0.9667
rs 9.2
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4.0005
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaBundle package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\PandaBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\Alias;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\ChildDefinition;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\DefinitionDecorator;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
use Xabbuh\PandaClient\Api\CloudInterface;
24
25
/**
26
 * XabbuhPandaExtension.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 */
30
class XabbuhPandaExtension extends Extension
31
{
32
    /**
33
     * {@inheritDoc}
34
     */
35 4
    public function load(array $configs, ContainerBuilder $container)
36
    {
37 4
        $configuration = new Configuration();
38 4
        $config = $this->processConfiguration($configuration, $configs);
39
40 4
        $container->setParameter(
41 4
            'xabbuh_panda.video_uploader.multiple_files',
42 4
            $config['video_uploader']['multiple_files']
43
        );
44 4
        $container->setParameter(
45 4
            'xabbuh_panda.video_uploader.cancel_button',
46 4
            $config['video_uploader']['cancel_button']
47
        );
48 4
        $container->setParameter(
49 4
            'xabbuh_panda.video_uploader.progress_bar',
50 4
            $config['video_uploader']['progress_bar']
51
        );
52
53 4
        $container->setParameter('xabbuh_panda.account.default', $config['default_account']);
54 4
        $container->setParameter('xabbuh_panda.cloud.default', $config['default_cloud']);
55
56
        // and load the service definitions
57 4
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
58 4
        $loader->load('account_manager.xml');
59 4
        $loader->load('cloud_manager.xml');
60 4
        $loader->load('commands.xml');
61 4
        $loader->load('controller.xml');
62 4
        $loader->load('transformers.xml');
63 4
        $loader->load('video_uploader_extension.xml');
64
65 4
        $knownAccounts = $this->loadAccounts($config['accounts'], $container);
66 4
        $knownClouds = $this->loadClouds($config['clouds'], $container, $knownAccounts, $config['default_account']);
67
68 4
        if (isset($knownClouds[$config['default_cloud']])) {
69 1
            $container->setAlias(CloudInterface::class, new Alias($knownClouds[$config['default_cloud']], false));
70
        }
71
72 4
        $baseHttpClientDefinition = $container->getDefinition('xabbuh_panda.http_client');
73
74 4
        foreach (array('client' => 0, 'request_factory' => 1, 'stream_factory' => 2) as $key => $argumentIndex) {
75 4
            if (null !== $config['httplug'][$key]) {
76
                $baseHttpClientDefinition->replaceArgument($argumentIndex, new Reference($config['httplug'][$key]));
77
            }
78
        }
79 4
    }
80
81 4
    private function loadAccounts(array $accounts, ContainerBuilder $container)
82
    {
83 4
        $accountManagerDefinition = $container->getDefinition('xabbuh_panda.account_manager');
84
85 4
        $knownAccounts = array();
86
87 4
        foreach ($accounts as $name => $accountConfig) {
88
            // register each account as a service
89 1
            $accountDefinition = new Definition(
90 1
                'Xabbuh\PandaClient\Api\Account',
91
                array(
92 1
                    $accountConfig['access_key'],
93 1
                    $accountConfig['secret_key'],
94 1
                    $accountConfig['api_host']
95
                )
96
            );
97 1
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_account';
98 1
            $container->setDefinition($id, $accountDefinition);
99
100
            // and pass it to the manager's registerAccount() method
101 1
            $accountManagerDefinition->addMethodCall(
102 1
                'registerAccount',
103 1
                array($name, new Reference($id))
104
            );
105 1
            $knownAccounts[$name] = $id;
106
        }
107
108 4
        return $knownAccounts;
109
    }
110
111 4
    private function loadClouds(array $clouds, ContainerBuilder $container, array $knownAccounts, $defaultAccount)
112
    {
113 4
        $cloudManagerDefinition = $container->getDefinition('xabbuh_panda.cloud_manager');
114
115 4
        $knownClouds = array();
116
117 4
        foreach ($clouds as $name => $cloudConfig) {
118 1
            $accountName = isset($cloudConfig['account']) ? $cloudConfig['account'] : $defaultAccount;
119
120 1
            if (!isset($knownAccounts[$accountName])) {
121
                throw new \InvalidArgumentException(sprintf('The account `%s` configured for the cloud `%s` is not one of the configured accounts.', $accountName, $name));
122
            }
123
124 1
            if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
125 1
                $httpClientDefinition = new ChildDefinition('xabbuh_panda.http_client');
126
            } else {
127
                $httpClientDefinition = new DefinitionDecorator('xabbuh_panda.http_client');
128
            }
129
130 1
            $httpClientDefinition->setPublic(false);
131
            // Get a reference to the account service directly, to avoid instantiating the AccountManager (and all other
132
            // accounts due to the AccountManager not supporting lazy-loading)
133 1
            $httpClientDefinition->addMethodCall('setAccount', array(new Reference($knownAccounts[$accountName])));
134 1
            $httpClientDefinition->addMethodCall('setCloudId', array($cloudConfig['id']));
135
136 1
            $httpClientId = 'xabbuh_panda.http_client.'.strtr($name, ' -', '_');
137
138 1
            $container->setDefinition($httpClientId, $httpClientDefinition);
139
140
            // register each cloud as a service
141 1
            $cloudDefinition = new Definition('Xabbuh\PandaClient\Api\Cloud');
142 1
            $cloudDefinition->addMethodCall('setHttpClient', array(new Reference($httpClientId)));
143 1
            $cloudDefinition->addMethodCall('setTransformers', array(new Reference('xabbuh_panda.transformer')));
144
145 1
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_cloud';
146 1
            $container->setDefinition($id, $cloudDefinition);
147
148
            // and pass it to the manager's registerAccount() method
149 1
            $cloudManagerDefinition->addMethodCall(
150 1
                'registerCloud',
151 1
                array($name, new Reference($id))
152
            );
153 1
            $knownClouds[$name] = $id;
154
        }
155
156 4
        return $knownClouds;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162 4
    public function getNamespace()
163
    {
164 4
        return 'http://xabbuh.de/schema/dic/xabbuh/panda';
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170
    public function getXsdValidationBasePath()
171
    {
172
        return __DIR__.'/../Resources/config/schema';
173
    }
174
}
175