SuluActivityLogExtension::initElasticSearch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 6
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ActivityLogBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Extension\Extension;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * This is the class that loads and manages activity log bundle configuration.
23
 */
24
class SuluActivityLogExtension extends Extension
25
{
26
    const STORAGE_ELASTIC = 'elastic';
27
    const STORAGE_ARRAY = 'array';
28
    const STORAGE_CUSTOM = 'custom';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 1
        $configuration = new Configuration();
36 1
        $config = $this->processConfiguration($configuration, $configs);
37
38 1
        switch ($config['storage']) {
39 1
            case self::STORAGE_ELASTIC:
40
                $id = $this->initElasticSearch($config['storages']['elastic'], $container);
41
                break;
42
43 1
            case self::STORAGE_ARRAY:
44 1
                $id = $this->initArray($config['storages']['array'], $container);
45 1
                break;
46
47
            case self::STORAGE_CUSTOM:
48
                $id = $this->initCustom($config['storages']['custom'], $container);
49
                break;
50
        }
51
52 1
        $container->setAlias('sulu_activity_log.activity_log_storage', $id);
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
53
54 1
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
55 1
        $loader->load('services.xml');
56 1
    }
57
58
    /**
59
     * @param array $config
60
     * @param ContainerBuilder $container
61
     *
62
     * @return string
63
     */
64
    private function initElasticSearch(array $config, ContainerBuilder $container)
65
    {
66
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
67
        $loader->load('storages/elastic.xml');
68
69
        if (!$config['ongr_manager']) {
70
            $error = new InvalidConfigurationException();
71
            $error->setPath('sulu_activity_log.storages.elastic.ongr_manager');
72
73
            throw $error;
74
        }
75
76
        $container->getDefinition('sulu_activity_log.storage.elastic')->replaceArgument(
77
            0,
78
            new Reference('es.manager.' . $config['ongr_manager'])
79
        );
80
81
        return 'sulu_activity_log.storage.elastic';
82
    }
83
84
    /**
85
     * @param array $config
86
     * @param ContainerBuilder $container
87
     *
88
     * @return string
89
     */
90 1
    private function initArray(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    {
92 1
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
93 1
        $loader->load('storages/array.xml');
94
95 1
        return 'sulu_activity_log.storage.array';
96
    }
97
98
    /**
99
     * @param array $config
100
     * @param ContainerBuilder $container
101
     *
102
     * @return string
103
     */
104
    private function initCustom(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    {
106
        if (!$config['id']) {
107
            $error = new InvalidConfigurationException();
108
            $error->setPath('sulu_activity_log.storages.custom.id');
109
110
            throw $error;
111
        }
112
113
        return $config['id'];
114
    }
115
}
116