Passed
Pull Request — master (#5)
by ANTHONIUS
02:39
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.0019

Importance

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

77
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
78 1
                    'class' => $class,
79
                ]),
80 1
                $builder->getResources()
81
            );
82
        }
83
84 1
        require_once $file;
85
86 1
        return new $class();
87
    }
88
}
89