Completed
Push — develop ( e4e826...d92786 )
by ANTHONIUS
11s queued 10s
created

ReportPass::ensureDir()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
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\Reference;
10
11
class ReportPass implements CompilerPassInterface
12
{
13 1
    public function process(ContainerBuilder $container)
14
    {
15 1
        $services = $container->findTaggedServiceIds('doyo.coverage.report');
16
17
        // configure processors
18 1
        foreach($container->findTaggedServiceIds('doyo.coverage.report.processor') as $id => $tagArguments){
19 1
            $this->configureProcessor($container, $id, $tagArguments);
20
        }
21
        // configure report
22 1
        foreach($services as $id => $tagArguments){
23 1
            $this->configureReport($container, $id, $tagArguments);
24
        }
25
26
    }
27
28 1
    private function configureProcessor(ContainerBuilder $container, $id, array $tagArguments)
29
    {
30 1
        $definition = $container->getDefinition($id);
31 1
        $format = $tagArguments[0]['format'];
32 1
        $options = $container->getParameterBag()->get('doyo.coverage.report.'.$format);
0 ignored issues
show
Unused Code introduced by
The assignment to $options is dead and can be removed.
Loading history...
33 1
        $class = $container->getParameterBag()->get($id.'.class');
34
35 1
        $definition->setClass($class);
36
    }
37
38 1
    private function configureReport(ContainerBuilder $container, $id, array $tagArguments)
39
    {
40 1
        $definition = $container->getDefinition($id);
41 1
        $format = $tagArguments[0]['format'];
42 1
        $type = $tagArguments[0]['type'];
43 1
        $config = $container->getParameterBag()->get('doyo.coverage.report.'.$format);
44 1
        $class = $container->getParameterBag()->get('doyo.coverage.report.class');
45 1
        $basePath = $container->getParameterBag()->get('paths.base');
46 1
        $dispatcher = $container->getDefinition('event_dispatcher');
47 1
        $definition->setClass($class);
48
49 1
        if(isset($config['target'])){
50 1
            $target = $basePath.'/'.$config['target'];
51 1
            $definition->addMethodCall('setTarget', [$target]);
52 1
            $definition->addMethodCall('setProcessor',[new Reference($id.'.processor')]);
53 1
            $definition->addMethodCall('setName',[$format]);
54 1
            $dispatcher->addMethodCall('addSubscriber', [new Reference($id)]);
55 1
            $this->ensureDir($type, $target);
56
        }
57
    }
58
59 1
    private function ensureDir($type, $target)
60
    {
61 1
        $dir = $target;
62 1
        if($type === 'file'){
63 1
            $dir = dirname($target);
64
        }
65
66 1
        if(!is_dir($dir)){
67 1
            mkdir($dir,0775, true);
68
        }
69
    }
70
71
}
72