Passed
Pull Request — develop (#5)
by ANTHONIUS
04:09
created

ReportPass::configureProcessorOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

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