Passed
Pull Request — develop (#5)
by ANTHONIUS
10:30
created

ReportPass::ensureDir()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
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
declare(strict_types=1);
13 1
14
namespace Doyo\Behat\Coverage\Compiler;
15 1
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18 1
use Symfony\Component\DependencyInjection\Definition;
19 1
use Symfony\Component\DependencyInjection\Reference;
20
21
class ReportPass implements CompilerPassInterface
22 1
{
23 1
    public function process(ContainerBuilder $container)
24
    {
25
        $services = $container->findTaggedServiceIds('doyo.coverage.report');
26
27
        // configure processors
28 1
        foreach ($container->findTaggedServiceIds('doyo.coverage.report.processor') as $id => $tagArguments) {
29
            $this->configureProcessor($container, $id, $tagArguments);
30 1
        }
31 1
        // configure report
32 1
        foreach ($services as $id => $tagArguments) {
33 1
            $this->configureReport($container, $id, $tagArguments);
34
        }
35 1
    }
36
37
    private function configureProcessor(ContainerBuilder $container, $id, array $tagArguments)
38 1
    {
39
        $definition = $container->getDefinition($id);
40 1
        $format     = $tagArguments[0]['format'];
41 1
        $options    = $container->getParameterBag()->get('doyo.coverage.report.'.$format);
42 1
        $class      = $container->getParameterBag()->get($id.'.class');
43 1
        $hasOptions = ['html', 'text', 'crap4j'];
44 1
45 1
        unset($options['target']);
46 1
        $definition->setClass($class);
47 1
        if (!empty($options) && \in_array($format, $hasOptions, true)) {
48
            $this->configureProcessorOptions($definition, $class, $options);
49 1
        }
50 1
    }
51 1
52 1
    private function configureProcessorOptions(Definition $definition, $class, $options)
53 1
    {
54 1
        $r           = new \ReflectionClass($class);
55 1
        $constructor = $r->getConstructor();
56
        $parameters  = [];
57
58
        foreach ($constructor->getParameters() as $reflectionParameter) {
59 1
            $paramName = $reflectionParameter->getName();
60
            if (!$reflectionParameter->isDefaultValueAvailable()) {
61 1
                return;
62 1
            }
63 1
            $value    = $reflectionParameter->getDefaultValue();
64
            $position = $reflectionParameter->getPosition();
65
            if (isset($options[$paramName])) {
66 1
                $value = $options[$paramName];
67 1
            }
68
            $parameters[$position] = $value;
69
        }
70
        $definition->setArguments($parameters);
71
    }
72
73
    private function configureReport(ContainerBuilder $container, $id, array $tagArguments)
74
    {
75
        $definition = $container->getDefinition($id);
76
        $format     = $tagArguments[0]['format'];
77
        $type       = $tagArguments[0]['type'];
78
        $config     = $container->getParameterBag()->get('doyo.coverage.report.'.$format);
79
        $class      = $container->getParameterBag()->get('doyo.coverage.report.class');
80
        $basePath   = $container->getParameterBag()->get('paths.base');
81
        $dispatcher = $container->getDefinition('doyo.coverage.dispatcher');
82
83
        $definition->setClass($class);
84
85
        if (isset($config['target'])) {
86
            $target = $basePath.'/'.$config['target'];
87
            $definition->addMethodCall('setTarget', [$target]);
88
            $definition->addMethodCall('setProcessor', [new Reference($id.'.processor')]);
89
            $definition->addMethodCall('setName', [$format]);
90
            $dispatcher->addMethodCall('addSubscriber', [new Reference($id)]);
91
            $this->ensureDir($type, $target);
92
        }
93
    }
94
95
    private function ensureDir($type, $target)
96
    {
97
        $dir = $target;
98
        if ('file' === $type) {
99
            $dir = \dirname($target);
100
        }
101
102
        if (!is_dir($dir)) {
103
            mkdir($dir, 0775, true);
104
        }
105
    }
106
}
107