Completed
Push — master ( f41515...ea4213 )
by Benjamin
02:08
created

AlpixelCMSExtension::load()   C

Complexity

Conditions 15
Paths 16

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 5.0504
cc 15
eloc 23
nc 16
nop 2

How to fix   Complexity   

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
namespace Alpixel\Bundle\CMSBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader;
8
use Symfony\Component\Form\Exception\InvalidConfigurationException;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
11
class AlpixelCMSExtension extends Extension
12
{
13
    private $_blockDefaultClass = 'Alpixel\Bundle\CMSBundle\Entity\Block';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function load(array $configs, ContainerBuilder $container)
19
    {
20
        $configuration = new Configuration();
21
        $config = $this->processConfiguration($configuration, $configs);
22
23
        foreach ($config['content_types'] as $name => $contentType) {
24
            if (empty($contentType['title'])) {
25
                throw new InvalidConfigurationException('Content type '.$name.' shoud have a title');
26
            }
27
28
            if (empty($contentType['description'])) {
29
                throw new InvalidConfigurationException('Content type '.$name.' shoud have a description');
30
            }
31
32
            if (!isset($contentType['class']) || empty($contentType['class']) || !class_exists($contentType['class'])) {
33
                throw new InvalidConfigurationException('CMS '.$contentType['class'].' can\'t be found');
34
            }
35
36
            if (empty($contentType['controller'])) {
37
                throw new InvalidConfigurationException('CMS '.$contentType['controller'].' can\'t be found');
38
            }
39
        }
40
        $container->setParameter('cms.content_types', $config['content_types']);
41
42
        foreach ($config['blocks'] as $name => $contentType) {
43
            if (empty($contentType['title'])) {
44
                throw new InvalidConfigurationException('Block '.$name.' shoud have a title');
45
            }
46
47
            if ((!isset($contentType['class']) || empty($contentType['class'])) && class_exists($this->_blockDefaultClass)) {
48
                $config['blocks'][$name]['class'] = $this->_blockDefaultClass;
49
            }
50
51
            if (isset($contentType['class']) && !class_exists($contentType['class'])) {
52
                throw new InvalidConfigurationException('Block '.$contentType['class'].' can\'t be found');
53
            }
54
        }
55
        $container->setParameter('cms.blocks', $config['blocks']);
56
57
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
58
        $loader->load('services.yml');
59
    }
60
}
61