Completed
Push — master ( e03c8b...0b5511 )
by Benjamin
02:26
created

AlpixelCMSExtension::load()   C

Complexity

Conditions 14
Paths 21

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 39
rs 5.0864
cc 14
eloc 22
nc 21
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\Extension\PrependExtensionInterface;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\Form\Exception\InvalidConfigurationException;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Symfony\Component\Yaml\Parser;
12
13
class AlpixelCMSExtension extends Extension implements PrependExtensionInterface
14
{
15
    private $_blockDefaultClass = 'Alpixel\Bundle\CMSBundle\Entity\Block';
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function load(array $configs, ContainerBuilder $container)
21
    {
22
        $configuration = new Configuration();
23
        $config = $this->processConfiguration($configuration, $configs);
24
25
        foreach ($config['content_types'] as $name => $contentType) {
26
            if (empty($contentType['title'])) {
27
                throw new InvalidConfigurationException('Content type ' . $name . ' shoud have a title');
28
            }
29
30
            if (empty($contentType['description'])) {
31
                @trigger_error('Content type ' . $name . ' shoud have a description', E_USER_WARNING);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
32
            }
33
34
            if (!isset($contentType['class']) || empty($contentType['class']) || !class_exists($contentType['class'])) {
35
                throw new InvalidConfigurationException('CMS ' . $contentType['class'] . ' can\'t be found');
36
            }
37
        }
38
        $container->setParameter('alpixel_cms.content_types', $config['content_types']);
39
40
        foreach ($config['blocks'] as $name => $contentType) {
41
            if (empty($contentType['title'])) {
42
                throw new InvalidConfigurationException('Block ' . $name . ' shoud have a title');
43
            }
44
45
            if ((!isset($contentType['class']) || empty($contentType['class'])) && class_exists($this->_blockDefaultClass)) {
46
                $config['blocks'][$name]['class'] = $this->_blockDefaultClass;
47
            }
48
49
            if (isset($contentType['class']) && !class_exists($contentType['class'])) {
50
                throw new InvalidConfigurationException('Block ' . $contentType['class'] . ' can\'t be found');
51
            }
52
        }
53
        $container->setParameter('alpixel_cms.blocks', $config['blocks']);
54
        $container->setParameter('alpixel_cms.exception_template', $config['exception_template']);
55
56
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
57
        $loader->load('services.yml');
58
    }
59
60
61
    public function prepend(ContainerBuilder $container)
62
    {
63
        $parser = new Parser();
64
        $config = $parser->parse(file_get_contents(__DIR__ . '/../Resources/config/config.yml'));
65
66
        $container->prependExtensionConfig('lunetics_locale', $config['lunetics_locale']);
67
        $container->prependExtensionConfig('jms_translation', $config['jms_translation']);
68
        $container->prependExtensionConfig('jms_i18n_routing', $config['jms_i18n_routing']);
69
        $container->prependExtensionConfig('twig', $config['twig']);
70
    }
71
}
72