Completed
Push — master ( a9dcf8...b1583f )
by Christian
04:56 queued 02:51
created

XabbuhPandaExtension::load()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 5.005

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 32
cts 34
cp 0.9412
rs 8.7361
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 5.005

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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('cloud_factory.xml');
61 4
        $loader->load('commands.xml');
62 4
        $loader->load('controller.xml');
63 4
        $loader->load('transformers.xml');
64 4
        $loader->load('video_uploader_extension.xml');
65
66 4
        $knownAccounts = $this->loadAccounts($config['accounts'], $container);
67 4
        $knownClouds = $this->loadClouds($config['clouds'], $container, $knownAccounts, $config['default_account']);
68
69 4
        if (isset($knownClouds[$config['default_cloud']])) {
70 1
            $container->setAlias(CloudInterface::class, new Alias($knownClouds[$config['default_cloud']], false));
71
        }
72
73 4
        if (method_exists(Definition::class, 'getDeprecation')) {
74 4
            $container->getDefinition('xabbuh_panda.cloud_factory')->setDeprecated('xabbuh/panda-bundle', '1.4', '');
0 ignored issues
show
Documentation introduced by
'xabbuh/panda-bundle' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to Definition::setDeprecated() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
        } else {
76
            $container->getDefinition('xabbuh_panda.cloud_factory')->setDeprecated(true);
77
        }
78
79 4
        $baseHttpClientDefinition = $container->getDefinition('xabbuh_panda.http_client');
80
81 4
        foreach (array('client' => 0, 'request_factory' => 1, 'stream_factory' => 2) as $key => $argumentIndex) {
82 4
            if (null !== $config['httplug'][$key]) {
83
                $baseHttpClientDefinition->replaceArgument($argumentIndex, new Reference($config['httplug'][$key]));
84
            }
85
        }
86 4
    }
87
88 4
    private function loadAccounts(array $accounts, ContainerBuilder $container)
89
    {
90 4
        $accountManagerDefinition = $container->getDefinition('xabbuh_panda.account_manager');
91
92 4
        $knownAccounts = array();
93
94 4
        foreach ($accounts as $name => $accountConfig) {
95
            // register each account as a service
96 1
            $accountDefinition = new Definition(
97 1
                'Xabbuh\PandaClient\Api\Account',
98
                array(
99 1
                    $accountConfig['access_key'],
100 1
                    $accountConfig['secret_key'],
101 1
                    $accountConfig['api_host']
102
                )
103
            );
104 1
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_account';
105 1
            $container->setDefinition($id, $accountDefinition);
106
107
            // and pass it to the manager's registerAccount() method
108 1
            $accountManagerDefinition->addMethodCall(
109 1
                'registerAccount',
110 1
                array($name, new Reference($id))
111
            );
112 1
            $knownAccounts[$name] = $id;
113
        }
114
115 4
        return $knownAccounts;
116
    }
117
118 4
    private function loadClouds(array $clouds, ContainerBuilder $container, array $knownAccounts, $defaultAccount)
119
    {
120 4
        $cloudManagerDefinition = $container->getDefinition('xabbuh_panda.cloud_manager');
121
122 4
        $knownClouds = array();
123
124 4
        foreach ($clouds as $name => $cloudConfig) {
125 1
            $accountName = isset($cloudConfig['account']) ? $cloudConfig['account'] : $defaultAccount;
126
127 1
            if (!isset($knownAccounts[$accountName])) {
128
                throw new \InvalidArgumentException(sprintf('The account `%s` configured for the cloud `%s` is not one of the configured accounts.', $accountName, $name));
129
            }
130
131 1
            if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
132 1
                $httpClientDefinition = new ChildDefinition('xabbuh_panda.http_client');
133
            } else {
134
                $httpClientDefinition = new DefinitionDecorator('xabbuh_panda.http_client');
135
            }
136
137 1
            $httpClientDefinition->setPublic(false);
138
            // Get a reference to the account service directly, to avoid instantiating the AccountManager (and all other
139
            // accounts due to the AccountManager not supporting lazy-loading)
140 1
            $httpClientDefinition->addMethodCall('setAccount', array(new Reference($knownAccounts[$accountName])));
141 1
            $httpClientDefinition->addMethodCall('setCloudId', array($cloudConfig['id']));
142
143 1
            $httpClientId = 'xabbuh_panda.http_client.'.strtr($name, ' -', '_');
144
145 1
            $container->setDefinition($httpClientId, $httpClientDefinition);
146
147
            // register each cloud as a service
148 1
            $cloudDefinition = new Definition('Xabbuh\PandaClient\Api\Cloud');
149 1
            $cloudDefinition->addMethodCall('setHttpClient', array(new Reference($httpClientId)));
150 1
            $cloudDefinition->addMethodCall('setTransformers', array(new Reference('xabbuh_panda.transformer')));
151
152 1
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_cloud';
153 1
            $container->setDefinition($id, $cloudDefinition);
154
155
            // and pass it to the manager's registerAccount() method
156 1
            $cloudManagerDefinition->addMethodCall(
157 1
                'registerCloud',
158 1
                array($name, new Reference($id))
159
            );
160 1
            $knownClouds[$name] = $id;
161
        }
162
163 4
        return $knownClouds;
164
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169 4
    public function getNamespace()
170
    {
171 4
        return 'http://xabbuh.de/schema/dic/xabbuh/panda';
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177
    public function getXsdValidationBasePath()
178
    {
179
        return __DIR__.'/../Resources/config/schema';
180
    }
181
}
182