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

CoveragePass::compileCoverageOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle 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\Finder\Finder;
20
21
class CoveragePass implements CompilerPassInterface
22
{
23 1
    public function process(ContainerBuilder $container)
24
    {
25 1
        $this->compileCoverageOptions($container);
26 1
        $this->compileFilterOptions($container);
27
    }
28
29 1
    private function compileCoverageOptions(ContainerBuilder $container)
30
    {
31 1
        $options = $container->getParameterBag()->get('doyo.coverage.options');
32
33 1
        $definitions = $container->findTaggedServiceIds('doyo.code_coverage');
34
35
        /* @var \Symfony\Component\DependencyInjection\Definition $definition */
36 1
        foreach ($definitions as $id => $test) {
37 1
            $definition = $container->getDefinition($id);
38 1
            $this->addCoverageOption($definition, $options);
39
        }
40
    }
41
42 1
    private function addCoverageOption(Definition $definition, array $options)
43
    {
44 1
        foreach ($options as $name => $value) {
45 1
            $method = 'set'.ucfirst($name);
46 1
            $definition->addMethodCall($method, [$value]);
47
        }
48
    }
49
50 1
    private function compileFilterOptions(ContainerBuilder $container)
51
    {
52 1
        $config     = $container->getParameterBag()->get('doyo.coverage.config');
53 1
        $filter     = $config['filter'];
54 1
        $basePath   = $container->getParameterBag()->get('paths.base');
55
56 1
        $whitelist = $filter['whitelist'];
57 1
        $blackList = $filter['blacklist'];
58
59 1
        $files = [];
60 1
        foreach ($whitelist as $path) {
61 1
            $found = $this->findFiles($basePath, $path, $blackList);
62 1
            $files = array_merge($found, $files);
63
        }
64 1
        $container->setParameter('doyo.coverage.config.filter', $files);
65
    }
66
67 1
    private function findFiles($basePath, $path, $blacklist)
68
    {
69 1
        $lastPos = stripos($path, '/');
70 1
        $dir     = $path;
71 1
        $name    = null;
72 1
        if (false !== $lastPos) {
73 1
            $dir  = substr($path, 0, $lastPos);
74 1
            $name = substr($path, $lastPos + 1);
75
        }
76
77 1
        $in     = $basePath.DIRECTORY_SEPARATOR.$dir;
78 1
        $finder = Finder::create()
79 1
            ->in($in);
80 1
        if (!is_null($name)) {
81 1
            $finder->name($name);
82
        }
83
84 1
        foreach ($blacklist as $l) {
85 1
            $finder->notPath($l);
86
        }
87
88 1
        $files = [];
89 1
        foreach ($finder->files() as $file) {
90 1
            $files[] = $file->getRealPath();
91
        }
92
93 1
        return $files;
94
    }
95
}
96