AlpixelCMSExtension::load()   C
last analyzed

Complexity

Conditions 14
Paths 21

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 6.2666
c 0
b 0
f 0
cc 14
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
    public function prepend(ContainerBuilder $container)
61
    {
62
        $parser = new Parser();
63
64
        $configs = $container->getExtensionConfig($this->getAlias());
65
        $bundleConfig = $this->processConfiguration(new Configuration(), $configs);
66
67
        $config = $parser->parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));
68
69
        foreach ($config as $key => $configuration) {
0 ignored issues
show
Bug introduced by
The expression $config of type null|object<Symfony\Comp...|array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
70
            if ($key == 'sonata_admin' && $bundleConfig['white_label'] === true) {
71
                foreach ($configuration['dashboard']['blocks'] as $blockKey => $block) {
72
                    if ($block['type'] === 'alpixel_cms.sonata.admin.block.alpixel') {
73
                        unset($configuration['dashboard']['blocks'][$blockKey]);
74
                    }
75
                }
76
            }
77
            $container->prependExtensionConfig($key, $configuration);
78
        }
79
    }
80
}
81