Completed
Pull Request — master (#30)
by Christophe
03:39
created

XabbuhPandaExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 9

Test Coverage

Coverage 82.43%

Importance

Changes 0
Metric Value
wmc 12
cbo 9
dl 0
loc 136
ccs 61
cts 74
cp 0.8243
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B load() 0 49 4
B loadAccounts() 0 24 2
B loadClouds() 0 39 4
A getNamespace() 0 4 1
A getXsdValidationBasePath() 0 4 1
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\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ChildDefinition;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\DefinitionDecorator;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
use Xabbuh\PandaClient\Api\CloudInterface;
23
24
/**
25
 * XabbuhPandaExtension.
26
 *
27
 * @author Christian Flothmann <[email protected]>
28
 */
29
class XabbuhPandaExtension extends Extension
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34 3
    public function load(array $configs, ContainerBuilder $container)
35
    {
36 3
        $configuration = new Configuration();
37 3
        $config = $this->processConfiguration($configuration, $configs);
38
39 3
        $container->setParameter(
40 3
            'xabbuh_panda.video_uploader.multiple_files',
41 3
            $config['video_uploader']['multiple_files']
42
        );
43 3
        $container->setParameter(
44 3
            'xabbuh_panda.video_uploader.cancel_button',
45 3
            $config['video_uploader']['cancel_button']
46
        );
47 3
        $container->setParameter(
48 3
            'xabbuh_panda.video_uploader.progress_bar',
49 3
            $config['video_uploader']['progress_bar']
50
        );
51
52 3
        $container->setParameter('xabbuh_panda.account.default', $config['default_account']);
53 3
        $container->setParameter('xabbuh_panda.cloud.default', $config['default_cloud']);
54
55
        // and load the service definitions
56 3
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
57 3
        $loader->load('account_manager.xml');
58 3
        $loader->load('cloud_manager.xml');
59 3
        $loader->load('cloud_factory.xml');
60 3
        $loader->load('controller.xml');
61 3
        $loader->load('transformers.xml');
62 3
        $loader->load('video_uploader_extension.xml');
63
64 3
        $this->loadAccounts($config['accounts'], $container);
65 3
        $this->loadClouds($config['clouds'], $container);
66
67 3
        if (!empty($config['clouds'])) {
68 1
            $autowiredCloud = new Definition(CloudInterface::class);
69 1
            $autowiredCloud->setPublic(false);
70 1
            $autowiredCloud->setFactory(array(new Reference('xabbuh_panda.cloud_manager'), 'getCloud'));
71
72 1
            $container->setDefinition(CloudInterface::class, $autowiredCloud);
73
        }
74
75 3
        $baseHttpClientDefinition = $container->getDefinition('xabbuh_panda.http_client');
76
77 3
        foreach (array('client' => 0, 'request_factory' => 1, 'stream_factory' => 2) as $key => $argumentIndex) {
78 3
            if (null !== $config['httplug'][$key]) {
79 3
                $baseHttpClientDefinition->replaceArgument($argumentIndex, new Reference($config['httplug'][$key]));
80
            }
81
        }
82 3
    }
83
84 3
    private function loadAccounts(array $accounts, ContainerBuilder $container)
85
    {
86 3
        $accountManagerDefinition = $container->getDefinition('xabbuh_panda.account_manager');
87
88 3
        foreach ($accounts as $name => $accountConfig) {
89
            // register each account as a service
90
            $accountDefinition = new Definition(
91
                'Xabbuh\PandaClient\Api\Account',
92
                array(
93
                    $accountConfig['access_key'],
94
                    $accountConfig['secret_key'],
95
                    $accountConfig['api_host']
96
                )
97
            );
98
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_account';
99
            $container->setDefinition($id, $accountDefinition);
100
101
            // and pass it to the manager's registerAccount() method
102
            $accountManagerDefinition->addMethodCall(
103
                'registerAccount',
104
                array($name, new Reference($id))
105
            );
106
        }
107 3
    }
108
109 3
    private function loadClouds(array $clouds, ContainerBuilder $container)
110
    {
111 3
        $cloudManagerDefinition = $container->getDefinition('xabbuh_panda.cloud_manager');
112
113 3
        foreach ($clouds as $name => $cloudConfig) {
114 1
            $accountDefinition = new Definition('Xabbuh\PandaClient\Api\Account');
115 1
            $accountDefinition->setFactory(array(new Reference('xabbuh_panda.account_manager'), 'getAccount'));
116
117 1
            $accountDefinition->addArgument(isset($cloudConfig['account']) ? $cloudConfig['account'] : null);
118
119 1
            if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
120 1
                $httpClientDefinition = new ChildDefinition('xabbuh_panda.http_client');
121
            } else {
122
                $httpClientDefinition = new DefinitionDecorator('xabbuh_panda.http_client');
123
            }
124
125 1
            $httpClientDefinition->setPublic(false);
126 1
            $httpClientDefinition->addMethodCall('setAccount', array($accountDefinition));
127 1
            $httpClientDefinition->addMethodCall('setCloudId', array($cloudConfig['id']));
128
129 1
            $httpClientId = 'xabbuh_panda.http_client.'.strtr($name, ' -', '_');
130
131 1
            $container->setDefinition($httpClientId, $httpClientDefinition);
132
133
            // register each cloud as a service
134 1
            $cloudDefinition = new Definition('Xabbuh\PandaClient\Api\Cloud');
135 1
            $cloudDefinition->addMethodCall('setHttpClient', array(new Reference($httpClientId)));
136 1
            $cloudDefinition->addMethodCall('setTransformers', array(new Reference('xabbuh_panda.transformer')));
137
138 1
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_cloud';
139 1
            $container->setDefinition($id, $cloudDefinition);
140
141
            // and pass it to the manager's registerAccount() method
142 1
            $cloudManagerDefinition->addMethodCall(
143 1
                'registerCloud',
144 1
                array($name, new Reference($id))
145
            );
146
        }
147 3
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152 3
    public function getNamespace()
153
    {
154 3
        return 'http://xabbuh.de/schema/dic/xabbuh/panda';
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160
    public function getXsdValidationBasePath()
161
    {
162
        return __DIR__.'/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
163
    }
164
}
165