Completed
Push — master ( b2cd26...6b1baa )
by ANTHONIUS
13s queued 11s
created

ContainerFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Doyo\Bridge\CodeCoverage;
5
6
use Doyo\Bridge\CodeCoverage\Compiler\ReportPass;
7
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension;
8
use Doyo\Bridge\CodeCoverage\Compiler\CoveragePass;
9
use Symfony\Component\Config\ConfigCache;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
13
14
class ContainerFactory
15
{
16
    private $builder;
0 ignored issues
show
introduced by
The private property $builder is not used, and could be removed.
Loading history...
17
18
    private $configCache;
0 ignored issues
show
introduced by
The private property $configCache is not used, and could be removed.
Loading history...
19
20
    private $id;
21
22
    private $class;
23
24
    private $config;
25
26
    /**
27
     * @var bool
28
     */
29
    private $debug;
30
31 2
    public function __construct(array $config = [], bool $debug = false)
32
    {
33 2
        $id = md5(serialize($config));
34 2
        $class = 'CodeCoverageContainer'.$id;
35
36 2
        $this->id = $id;
37 2
        $this->class = $class;
38 2
        $this->config = $config;
39 2
        $this->debug = $debug;
40
    }
41
42
    /**
43
     * @return ContainerInterface
44
     */
45 1
    public function getContainer(): ContainerInterface
46
    {
47 1
        $id = $this->id;
48 1
        $class = $this->class;
49 1
        $file = sys_get_temp_dir().'/doyo/coverage/'.$id.'.php';
50
        //$config = ['config' => $this->config];
51 1
        $config = $this->config;
52
53 1
        $cachedContainer = new ConfigCache($file, $this->debug);
54 1
        if(!$cachedContainer->isFresh() || $this->debug){
55
            //$this->dumpConfig();
56 1
            $builder = new ContainerBuilder();
57
58 1
            $builder->registerExtension(new CodeCoverageExtension());
59 1
            $builder->loadFromExtension('coverage',$config);
60
61 1
            $builder->addCompilerPass(new CoveragePass());
62 1
            $builder->addCompilerPass(new ReportPass());
63 1
            $builder->compile();
64
65 1
            $dumper = new PhpDumper($builder);
66 1
            $cachedContainer->write(
67 1
                $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

67
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
68 1
                    'class' => $class
69
                ]),
70 1
                $builder->getResources()
71
            );
72
        }
73
74 1
        require_once $file;
75 1
        return new $class();
76
    }
77
}
78