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

AlpixelCMSExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 15
c 6
b 0
f 0
lcom 1
cbo 7
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C load() 0 39 14
A prepend() 0 10 1
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