Completed
Pull Request — 1.x (#55)
by Christian
04:34 queued 03:27
created

XabbuhPandaExtension::getXsdValidationBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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