Passed
Push — main ( bf434a...128b22 )
by Michael
02:11
created

CompilerPass::process()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 5
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Braunstetter\LocalizedRoutes\DependencyInjection\Compiler;
4
5
use Braunstetter\LocalizedRoutes\EventSubscriber\LocaleRewriteSubscriber;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
class CompilerPass implements CompilerPassInterface
10
{
11
12 2
    public function process(ContainerBuilder $container)
13
    {
14
15 2
        if (!$container->hasDefinition(LocaleRewriteSubscriber::class)) {
16 1
            return;
17
        }
18
19 1
        $frameworkConfig = $container->getExtensionConfig('framework');
20 1
        $subscriberDefinition = $container->getDefinition(LocaleRewriteSubscriber::class);
21 1
        $subscriberDefinition->replaceArgument(1, $this->getConfigValue('enabled_locales', $frameworkConfig));
22
23 1
        if ($defaultLocale = $this->getConfigValue('default_locale', $frameworkConfig)) {
24 1
            $container->setParameter('default_locale', $defaultLocale);
25
        }
26
27 1
        if ($enabledLocales = $this->getConfigValue('enabled_locales', $frameworkConfig)) {
28 1
            $container->setParameter('enabled_locales', $enabledLocales);
29 1
            $container->setParameter('enabled_locales_string', implode('|', $enabledLocales));
30
        }
31
    }
32
33 1
    private function getConfigValue(string $key, array $config)
34
    {
35 1
        $value = null;
36
37 1
        foreach ($config as $configItem) {
38 1
            if (array_key_exists($key, $configItem)) {
39 1
                $value = $configItem[$key];
40 1
                break;
41
            }
42
        }
43
44 1
        return $value;
45
    }
46
}