Completed
Push — master ( 2051f3...55baaf )
by Christian
04:02
created

XabbuhPandaExtension::load()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 3.0004

Importance

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