ConfigurationHelper::registerContainerParameter()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 5
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Config;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\Yaml\Yaml;
19
use WBW\Library\Types\Helper\ArrayHelper;
20
21
/**
22
 * Configuration helper.
23
 *
24
 * @author webeweb <https://github.com/webeweb>
25
 * @package WBW\Bundle\CoreBundle\Config
26
 */
27
class ConfigurationHelper {
28
29
    /**
30
     * Get a root node.
31
     *
32
     * @param TreeBuilder $treeBuilder The tree builder.
33
     * @param string $nodeName The node name.
34
     * @return ArrayNodeDefinition|NodeDefinition Returns the root node.
35
     */
36
    public static function getRootNode(TreeBuilder $treeBuilder, string $nodeName): NodeDefinition {
0 ignored issues
show
Unused Code introduced by
The parameter $nodeName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public static function getRootNode(TreeBuilder $treeBuilder, /** @scrutinizer ignore-unused */ string $nodeName): NodeDefinition {

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

Loading history...
37
        return $treeBuilder->getRootNode();
38
    }
39
40
    /**
41
     * Load a YAML configuration.
42
     *
43
     * @param string $directory The directory.
44
     * @param string $filename The filename.
45
     * @return array Returns the YAML configuration.
46
     */
47
    public static function loadYamlConfig(string $directory, string $filename): array {
48
49
        $pathname = realpath("$directory/../Resources/config/$filename.yml");
50
        if (false === $pathname) {
51
            return [];
52
        }
53
54
        return Yaml::parse(file_get_contents($pathname));
55
    }
56
57
    /**
58
     * Create a tree builder.
59
     *
60
     * @param string $nodeName The node name.
61
     * @return TreeBuilder Returns the tree builder.
62
     */
63
    public static function newTreeBuilder(string $nodeName): TreeBuilder {
64
        return new TreeBuilder($nodeName);
65
    }
66
67
    /**
68
     * Register a container parameter.
69
     *
70
     * @param ContainerInterface $container The container.
71
     * @param array $config The configuration.
72
     * @param string $alias The alias.
73
     * @param string $key The key.
74
     * @param bool $tree Tree ?
75
     * @return void
76
     */
77
    public static function registerContainerParameter(ContainerInterface $container, array $config, string $alias, string $key, bool $tree = true): void {
78
79
        if (false === array_key_exists($key, $config)) {
80
            return;
81
        }
82
83
        $item = $config[$key];
84
        $name = "$alias.$key";
85
86
        if (true === $tree || false === is_array($item) || false === ArrayHelper::isObject($item)) {
87
            $container->setParameter($name, $item);
88
            return;
89
        }
90
91
        foreach ($item as $k => $v) {
92
            static::registerContainerParameter($container, $item, $name, $k, $tree);
93
        }
94
    }
95
96
    /**
97
     * Register the container parameters.
98
     *
99
     * @param ContainerInterface $container The container.
100
     * @param array $config The configuration.
101
     * @return void
102
     */
103
    public static function registerContainerParameters(ContainerInterface $container, array $config): void {
104
        foreach ($config as $k => $v) {
105
            $container->setParameter($k, $v);
106
        }
107
    }
108
}
109