Passed
Pull Request — master (#8)
by Matt
10:27
created

AlphaTwigBundle::deriveMappingNamespace()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Alpha\TwigBundle;
6
7
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
8
use Symfony\Component\HttpKernel\Bundle\Bundle;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11
class AlphaTwigBundle extends Bundle
12
{
13
    private const PARAM_TEMPLATE_MAPPING_DIR = 'alpha_twig.entity.template.mapping_dir';
14
    private const PARAM_TEMPLATE_MAPPING_NAMESPACE = 'alpha_twig.entity.template.class';
15
    private const DEFAULT_TEMPLATE_MAPPING_NAMESPACE = 'Alpha\TwigBundle\Entity';
16
17
    public function build(ContainerBuilder $container): void
18
    {
19
        $this->addRegisterMappingsPass($container);
20
    }
21
22
    private function addRegisterMappingsPass(ContainerBuilder $container): void
23
    {
24
        $mappings = [
25
            $this->deriveMappingDir($container) => $this->deriveMappingNamespace($container),
26
        ];
27
28
        $container->addCompilerPass(DoctrineOrmMappingsPass::createYamlMappingDriver($mappings));
29
    }
30
31
    private function deriveMappingDir(ContainerBuilder $container): string
32
    {
33
        if ($container->hasParameter(self::PARAM_TEMPLATE_MAPPING_DIR)) {
34
            return $container->getParameter(self::PARAM_TEMPLATE_MAPPING_DIR);
35
        }
36
37
        return realpath(__DIR__.'/Resources/config/doctrine');
38
    }
39
40
    private function deriveMappingNamespace(ContainerBuilder $container): string
41
    {
42
        if ($container->hasParameter(self::PARAM_TEMPLATE_MAPPING_NAMESPACE)) {
43
            $namespace = preg_replace(
44
                '#^(.*)\\\\[^\\\\]+$#',
45
                '$1',
46
                $container->getParameter(self::PARAM_TEMPLATE_MAPPING_NAMESPACE)
47
            );
48
49
            return $namespace ?: '\\';
50
        }
51
52
        return self::DEFAULT_TEMPLATE_MAPPING_NAMESPACE;
53
    }
54
}
55