ConfigPass::normalizeIdForVariable()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 3
nop 1
1
<?php
2
3
namespace oliverde8\ComfyBundle\DependencyInjection\Compiler;
4
5
use oliverde8\ComfyBundle\Manager\ConfigManagerInterface;
6
use oliverde8\ComfyBundle\Model\ConfigInterface;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
/**
12
 * PluginPass to register all plugins to the plugin manager.
13
 *
14
 * @package eXpansion\Framework\Core\DependencyInjection\Compiler
15
 */
16
class ConfigPass implements CompilerPassInterface
17
{
18
    /**
19
     * Register all data providers to the Manager.
20
     *
21
     * @param ContainerBuilder $container
22
     */
23
    public function process(ContainerBuilder $container)
24
    {
25
        if (!$container->has(ConfigManagerInterface::class)) {
26
            return;
27
        }
28
29
        $definition = $container->getDefinition(ConfigManagerInterface::class);
30
31
        // Find all config's
32
        $configs = $container->findTaggedServiceIds('comfy.config');
33
        foreach ($configs as $id => $tags) {
34
            $definition->addMethodCall('registerConfig', [new Reference($id)]);
35
36
            $variableName = $this->normalizeIdForVariable($id);
37
            $container->registerAliasForArgument($id, ConfigInterface::class, $variableName);
38
        }
39
    }
40
41
    /**
42
     * Normalize the id to make a variable name out of it.
43
     *
44
     * @param string $id
45
     * @return string
46
     */
47
    protected function normalizeIdForVariable(string $id): string
48
    {
49
        $idParts = explode(".", $id);
50
51
        // Remove vendor name.
52
        array_shift($idParts);
53
54
        $variableName = '';
55
        foreach ($idParts as $part) {
56
            if (strtolower($part) !== 'comfy') {
57
                $part = str_replace("_", " ", $part);
58
                $part = ucwords($part);
59
                $part = str_replace(" ", "", $part);
60
61
                $variableName .= ucfirst($part);
62
            }
63
        }
64
65
        return lcfirst($variableName);
66
    }
67
}
68