Completed
Push — issue-20 ( f0936c...8a28ac )
by Christian
05:20
created

XabbuhPandaExtension::loadClouds()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 2
nop 2
crap 3
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\Definition;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
21
/**
22
 * XabbuhPandaExtension.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
class XabbuhPandaExtension extends Extension
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31 7
    public function load(array $configs, ContainerBuilder $container)
32
    {
33 7
        $configuration = new Configuration();
34 7
        $config = $this->processConfiguration($configuration, $configs);
35
36 7
        $container->setParameter(
37 7
            'xabbuh_panda.video_uploader.multiple_files',
38 7
            $config['video_uploader']['multiple_files']
39
        );
40 7
        $container->setParameter(
41 7
            'xabbuh_panda.video_uploader.cancel_button',
42 7
            $config['video_uploader']['cancel_button']
43
        );
44 7
        $container->setParameter(
45 7
            'xabbuh_panda.video_uploader.progress_bar',
46 7
            $config['video_uploader']['progress_bar']
47
        );
48
49 7
        $container->setParameter('xabbuh_panda.account.default', $config['default_account']);
50 7
        $container->setParameter('xabbuh_panda.cloud.default', $config['default_cloud']);
51
52
        // and load the service definitions
53 7
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
54 7
        $loader->load('account_manager.xml');
55 7
        $loader->load('cloud_manager.xml');
56 7
        $loader->load('cloud_factory.xml');
57 7
        $loader->load('controller.xml');
58 7
        $loader->load('transformers.xml');
59 7
        $loader->load('video_uploader_extension.xml');
60
61
        // Add the tag for the form type extension without using deprecated APIs
62 7
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
63 7
            $container->getDefinition('xabbuh_panda.video_uploader_extension')
64 7
                ->addTag('form.type_extension', array('extended_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType'));
65
        } else {
66
            $container->getDefinition('xabbuh_panda.video_uploader_extension')
67
                ->addTag('form.type_extension', array('alias' => 'file'));
68
        }
69
70 7
        $this->loadAccounts($config['accounts'], $container);
71 7
        $this->loadClouds($config['clouds'], $container);
72 7
    }
73
74 7
    private function loadAccounts(array $accounts, ContainerBuilder $container)
75
    {
76 7
        $accountManagerDefinition = $container->getDefinition('xabbuh_panda.account_manager');
77
78 7
        foreach ($accounts as $name => $accountConfig) {
79
            // register each account as a service
80 4
            $accountDefinition = new Definition(
81 4
                'Xabbuh\PandaClient\Api\Account',
82
                array(
83 4
                    $accountConfig['access_key'],
84 4
                    $accountConfig['secret_key'],
85 4
                    $accountConfig['api_host']
86
                )
87
            );
88 4
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_account';
89 4
            $container->setDefinition($id, $accountDefinition);
90
91
            // and pass it to the manager's registerAccount() method
92 4
            $accountManagerDefinition->addMethodCall(
93 4
                'registerAccount',
94 4
                array($name, new Reference($id))
95
            );
96
        }
97 7
    }
98
99 7
    private function loadClouds(array $clouds, ContainerBuilder $container)
100
    {
101 7
        $cloudManagerDefinition = $container->getDefinition('xabbuh_panda.cloud_manager');
102
103 7
        foreach ($clouds as $name => $cloudConfig) {
104
            // register each cloud as a service
105 5
            $cloudDefinition = new Definition(
106 5
                'Xabbuh\PandaClient\Api\Cloud',
107
                array(
108 5
                    $cloudConfig['id'],
109 5
                    isset($cloudConfig['account']) ? $cloudConfig['account'] : null,
110
                )
111
            );
112 5
            $cloudDefinition->setFactory(array(new Reference('xabbuh_panda.cloud_factory'), 'get'));
113
114 5
            $id = 'xabbuh_panda.'.strtr($name, ' -', '_').'_cloud';
115 5
            $container->setDefinition($id, $cloudDefinition);
116
117
            // and pass it to the manager's registerAccount() method
118 5
            $cloudManagerDefinition->addMethodCall(
119 5
                'registerCloud',
120 5
                array($name, new Reference($id))
121
            );
122
        }
123 7
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 7
    public function getNamespace()
129
    {
130 7
        return 'http://xabbuh.de/schema/dic/xabbuh/panda';
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136
    public function getXsdValidationBasePath()
137
    {
138
        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...
139
    }
140
}
141