Completed
Pull Request — master (#3)
by
unknown
30:30
created

SuluActivityLogExtension::initArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
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 1
    const STORAGE_ARRAY = 'array';
28
    const STORAGE_CUSTOM = 'custom';
29 1
30 1
    /**
31
     * {@inheritdoc}
32 1
     */
33
    public function load(array $configs, ContainerBuilder $container)
34 1
    {
35 1
        $configuration = new Configuration();
36 1
        $config = $this->processConfiguration($configuration, $configs);
37
38
        switch ($config['storage']) {
39
            case self::STORAGE_ELASTIC:
40
                $id = $this->initElasticSearch($config['storages']['elastic'], $container);
41
                break;
42
43
            case self::STORAGE_ARRAY:
44
                $id = $this->initArray($config['storages']['array'], $container);
45
                break;
46
47
            case self::STORAGE_CUSTOM:
48
                $id = $this->initCustom($config['storages']['custom']);
49
                break;
50
        }
51
52
        $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
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
55
        $loader->load('services.xml');
56
    }
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
    private function initArray($config, $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...
85
    {
86
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
87
        $loader->load('storages/array.xml');
88
89
        return 'sulu_activity_log.storage.array';
90
    }
91
92
    private function initCustom($config)
93
    {
94
        if (!$config['id']) {
95
            $error = new InvalidConfigurationException();
96
            $error->setPath('sulu_activity_log.storages.custom.id');
97
98
            throw $error;
99
        }
100
101
        return 'sulu_activity_log.storage.custom';
102
    }
103
}
104