AddPathsCompilerPass::process()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\PhpTemplatesBundle\DependencyInjection\Compiler;
6
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
11
12
final class AddPathsCompilerPass implements CompilerPassInterface
13
{
14 4
    public function process(ContainerBuilder $container): void
15
    {
16 4
        if (!$container->has('setono_php_templates.engine.default')) {
17 1
            return;
18
        }
19
20 3
        $engine = $container->getDefinition('setono_php_templates.engine.default');
21
22 3
        if ($container->hasParameter('kernel.bundles')) {
23 3
            self::addBundlePaths($container->getParameter('kernel.bundles'), $engine);
24
        }
25
26 3
        if ($container->hasParameter('kernel.project_dir')) {
27 2
            self::addProjectPath($container->getParameter('kernel.project_dir'), $engine);
28
        }
29 3
    }
30
31 3
    private static function addBundlePaths(array $bundles, Definition $engine): void
32
    {
33 3
        foreach ($bundles as $bundleName => $bundleClass) {
34 3
            $bundle = new $bundleClass();
35 3
            if (!$bundle instanceof BundleInterface) {
36 1
                continue;
37
            }
38
39 2
            $phpTemplatesPath = $bundle->getPath() . '/Resources/views/php';
40
41 2
            if (!is_dir($phpTemplatesPath) || !is_readable($phpTemplatesPath)) {
42 1
                continue;
43
            }
44
45 2
            $engine->addMethodCall('addPath', [$phpTemplatesPath]);
46
        }
47 3
    }
48
49 2
    private static function addProjectPath(string $projectDir, Definition $engine): void
50
    {
51 2
        $phpTemplatesPath = $projectDir . '/templates/php';
52
53 2
        if (!is_dir($phpTemplatesPath) || !is_readable($phpTemplatesPath)) {
54 1
            return;
55
        }
56
57 1
        $engine->addMethodCall('addPath', [$phpTemplatesPath]);
58 1
    }
59
}
60