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

CoveragePass::compileFilterOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 14
ccs 12
cts 12
cp 1
crap 3
rs 9.9
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
14
namespace Doyo\Behat\Coverage\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class CoveragePass implements CompilerPassInterface
22
{
23 2
    public function process(ContainerBuilder $container)
24
    {
25 2
        $this->compileFilterOptions($container);
26 2
        $this->compileDrivers($container);
27 2
        $this->compileCoverageOptions($container);
28
29 2
        $definition = $container->getDefinition('doyo.coverage.dispatcher');
30 2
        $tagged = $container->findTaggedServiceIds('doyo.dispatcher.subscriber');
31
32 2
        foreach($tagged as $id=>$arguments){
33 2
            $definition->addMethodCall('addSubscriber', [new Reference($id)]);
34
        }
35
    }
36
37 2
    private function compileDrivers(ContainerBuilder $container)
38
    {
39 2
        $drivers = $container->getParameterBag()->get('doyo.coverage.drivers');
40
41
        $map = [
42 2
            'cached' => $container->getParameterBag()->get('doyo.coverage.cached.class')
43
        ];
44
45 2
        foreach($drivers as $config){
46 2
            $namespace = $config['namespace'];
47 2
            $driver = $config['driver'];
48 2
            $class = $map[$driver];
49 2
            $id = 'doyo.coverage.cached.'.$namespace;
50
51 2
            $definition = new Definition($class);
52 2
            $definition->addTag('doyo.dispatcher.subscriber');
53 2
            $definition->addArgument($namespace);
54 2
            $definition->addMethodCall('setFilter',[new Reference('doyo.coverage.filter')]);
55 2
            $definition->addMethodCall('save');
56 2
            $definition->setPublic(true);
57
58 2
            $container->setDefinition($id, $definition);
59
        }
60
    }
61
62 2
    private function compileCoverageOptions(ContainerBuilder $container)
63
    {
64 2
        $options = $container->getParameterBag()->get('doyo.coverage.options');
65
66 2
        $definitions = $container->findTaggedServiceIds('doyo.code_coverage');
67
        /* @var \Symfony\Component\DependencyInjection\Definition $definition */
68 2
        foreach ($definitions as $id => $test) {
69 2
            $definition = $container->getDefinition($id);
70 2
            $this->addCoverageOption($definition, $options);
71
        }
72
    }
73
74 2
    private function addCoverageOption(Definition $definition, array $options)
75
    {
76 2
        foreach ($options as $name => $value) {
77 2
            $method = 'set'.ucfirst($name);
78 2
            $definition->addMethodCall($method, [$value]);
79
        }
80
    }
81
82 2
    private function compileFilterOptions(ContainerBuilder $container)
83
    {
84 2
        $config     = $container->getParameterBag()->get('doyo.coverage.config');
85 2
        $filter     = $config['filter'];
86 2
        $basePath   = $container->getParameterBag()->get('paths.base');
87 2
        $definition = $container->getDefinition('doyo.coverage.filter');
88
89 2
        foreach ($filter as $options) {
90 2
            $options['basePath'] = $basePath;
91 2
            $this->filterWhitelist($definition, $options, 'add');
92 2
            $exclude = $options['exclude'];
93 2
            foreach ($exclude as $item) {
94 2
                $item['basePath'] = $basePath;
95 2
                $this->filterWhitelist($definition, $item, 'remove');
96
            }
97
        }
98
    }
99
100 2
    private function filterWhitelist(Definition $definition, $options, $method)
101
    {
102 2
        $basePath  = $options['basePath'];
103 2
        $suffix    = $options['suffix'] ?: '.php';
104 2
        $prefix    = $options['prefix'] ?: '';
105 2
        $type      = $options['directory'] ? 'directory' : 'file';
106 2
        $directory = $basePath.\DIRECTORY_SEPARATOR.$options['directory'];
107 2
        $file      = $basePath.\DIRECTORY_SEPARATOR.$options['file'];
108
109 2
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
110 2
            $suffix    = $matches[1];
111 2
            $directory = str_replace($matches[0], '', $directory);
112
        }
113
114 2
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
115 2
        if ('directory' === $type) {
116 2
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
117
        }
118
119 2
        if ('file' === $type) {
120 2
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
121
        }
122
    }
123
}
124