Completed
Push — ezp26297-rest_embedding_http_c... ( 123323 )
by
unknown
80:28 queued 54:58
created

EzPublishRestExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 18.37 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 9
loc 49
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 17 1
A prepend() 9 9 2
A enableControllerCache() 0 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the eZ Publish Kernel package.
4
 *
5
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
6
 * @license For full copyright and license information view LICENSE file distributed with this source code.
7
 */
8
namespace eZ\Bundle\EzPublishRestBundle\DependencyInjection;
9
10
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ConfigurationProcessor;
11
use Symfony\Component\Config\FileLocator;
12
use Symfony\Component\Config\Resource\FileResource;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
15
use Symfony\Component\DependencyInjection\Loader;
16
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * This is the class that loads and manages your bundle configuration.
21
 *
22
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
23
 */
24
class EzPublishRestExtension extends Extension implements PrependExtensionInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function load(array $configs, ContainerBuilder $container)
30
    {
31
        $configuration = $this->getConfiguration($configs, $container);
32
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
34
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
35
        $loader->load('services.yml');
36
        $loader->load('value_object_visitors.yml');
37
        $loader->load('input_parsers.yml');
38
        $loader->load('security.yml');
39
        $loader->load('default_settings.yml');
40
41
        $processor = new ConfigurationProcessor($container, 'ezsettings');
42
        $processor->mapConfigArray('rest_root_resources', $config);
43
44
        $this->enableControllerCache($container);
45
    }
46
47 View Code Duplication
    public function prepend(ContainerBuilder $container)
48
    {
49
        if ($container->hasExtension('nelmio_cors')) {
50
            $file = __DIR__ . '/../Resources/config/nelmio_cors.yml';
51
            $config = Yaml::parse(file_get_contents($file));
52
            $container->prependExtensionConfig('nelmio_cors', $config);
53
            $container->addResource(new FileResource($file));
54
        }
55
    }
56
57
    private function enableControllerCache(ContainerBuilder $container)
58
    {
59
        foreach (['content', 'location', 'content_type', 'url_alias', 'binary_content', 'user'] as $controllerId) {
60
            if (!$container->hasDefinition("ezpublish_rest.controller.cached_$controllerId")) {
61
                continue;
62
            }
63
            if (!$container->hasAlias("ezpublish_rest.controller.$controllerId")) {
64
                return;
65
            }
66
            $container->setAlias(
67
                "ezpublish_rest.controller.$controllerId",
68
                "ezpublish_rest.controller.cached_$controllerId"
69
            );
70
        }
71
    }
72
}
73