Passed
Pull Request — master (#15)
by ANTHONIUS
07:35
created

ContainerFactory::createCodeCoverage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage 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\Bridge\CodeCoverage;
15
16
use Doyo\Bridge\CodeCoverage\Compiler\CoveragePass;
17
use Doyo\Bridge\CodeCoverage\Compiler\ReportPass;
18
use Doyo\Bridge\CodeCoverage\Console\Application;
19
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension;
20
use Doyo\Bridge\CodeCoverage\Driver\Dummy;
21
use Doyo\Bridge\CodeCoverage\Exception\SessionException;
22
use Doyo\Bridge\CodeCoverage\Session\LocalSession;
23
use Doyo\Bridge\CodeCoverage\Session\RemoteSession;
24
use Doyo\Bridge\CodeCoverage\Session\SessionInterface;
25
use Symfony\Component\Config\ConfigCache;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
29
30
class ContainerFactory
31
{
32
    /**
33
     * @var string
34
     */
35
    private $id;
36
37
    /**
38
     * @var string
39
     */
40
    private $class;
41
42
    /**
43
     * @var array
44
     */
45
    private $config;
46
47
    /**
48
     * @var ContainerInterface
49
     */
50
    private $container;
51
52 21
    public function __construct(array $config = [])
53
    {
54 21
        $id    = md5(serialize($config));
55 21
        $class = 'CodeCoverageContainer'.$id;
56
57 21
        $this->id     = $id;
58 21
        $this->class  = $class;
59 21
        $this->config = $config;
60 21
    }
61
62
    /**
63
     * @return ContainerInterface
64
     */
65 20
    public function getContainer(): ContainerInterface
66
    {
67 20
        if(is_null($this->container)){
68 20
            $this->doCreateContainer();
69
        }
70
71 20
        return $this->container;
72
    }
73
74
    /**
75
     * @param bool $useDummyDriver
76
     * @return ProcessorInterface
77
     */
78 6
    public function createProcessor(bool $useDummyDriver = false): ProcessorInterface
79
    {
80 6
        $coverage = $this->createCodeCoverage($useDummyDriver);
81 6
        $processor = new Processor($coverage);
82
83 6
        return $processor;
84
    }
85
86 6
    public function createCodeCoverage(bool $useDummyDriver = false)
87
    {
88 6
        $container = $this->container;
89 6
        $driverClass = $container->getParameter('coverage.driver.class');
90
91 6
        if($useDummyDriver){
92 4
            $driverClass = $container->getParameter('coverage.driver.dummy.class');
93
        }
94
95 6
        $driver = new $driverClass;
96 6
        $filter = $container->get('coverage.filter');
97 6
        $coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
98
99 6
        return $coverage;
100
    }
101
102
    public function createApplication($version = 'dev')
103
    {
104
        return new Application('code-coverage', $version);
105
    }
106
107 20
    private function doCreateContainer()
108
    {
109 20
        $id    = $this->id;
110 20
        $class = $this->class;
111 20
        $file  = sys_get_temp_dir().'/doyo/coverage/'.$id.'.php';
112
        //$config = ['config' => $this->config];
113 20
        $config = $this->config;
114 20
        $debug = isset($config['debug']) ? $config['debug']:false;
115
116 20
        $cachedContainer = new ConfigCache($file, $debug);
117 20
        if (!$cachedContainer->isFresh() || $debug) {
118
            //$this->dumpConfig();
119 3
            $builder = new ContainerBuilder();
120
121 3
            $builder->registerExtension(new CodeCoverageExtension());
122 3
            $builder->loadFromExtension('coverage', $config);
123
124 3
            $builder->addCompilerPass(new CoveragePass());
125 3
            $builder->addCompilerPass(new ReportPass());
126 3
            $builder->compile(true);
127
128 3
            $dumper = new PhpDumper($builder);
129 3
            $cachedContainer->write(
130 3
                $dumper->dump([
0 ignored issues
show
Bug introduced by
It seems like $dumper->dump(array('class' => $class)) can also be of type array; however, parameter $content of Symfony\Component\Config...kerConfigCache::write() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
131 3
                    'class' => $class,
132
                ]),
133 3
                $builder->getResources()
134
            );
135
        }
136
137 20
        require_once $file;
138
139
        /* @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
140 20
        $container =  new $class();
141 20
        $container->set('factory', $this);
142
143 20
        $this->container = $container;
144 20
    }
145
}
146