Passed
Pull Request — master (#15)
by ANTHONIUS
03:02
created

ContainerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 63
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getContainer() 0 32 3
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
    public function __construct(array $config = [], bool $debug = false)
42
    {
43
        $id    = md5(serialize($config));
44
        $class = 'CodeCoverageContainer'.$id;
45
46
        $this->id     = $id;
47
        $this->class  = $class;
48
        $this->config = $config;
49
        $this->debug  = $debug;
50
    }
51
52
    /**
53
     * @return ContainerInterface
54
     */
55
    public function getContainer(): ContainerInterface
56
    {
57
        $id    = $this->id;
58
        $class = $this->class;
59
        $file  = sys_get_temp_dir().'/doyo/coverage/'.$id.'.php';
60
        //$config = ['config' => $this->config];
61
        $config = $this->config;
62
63
        $cachedContainer = new ConfigCache($file, $this->debug);
64
        if (!$cachedContainer->isFresh() || $this->debug) {
65
            //$this->dumpConfig();
66
            $builder = new ContainerBuilder();
67
68
            $builder->registerExtension(new CodeCoverageExtension());
69
            $builder->loadFromExtension('coverage', $config);
70
71
            $builder->addCompilerPass(new CoveragePass());
72
            $builder->addCompilerPass(new ReportPass());
73
            $builder->compile(true);
74
75
            $dumper = new PhpDumper($builder);
76
            $cachedContainer->write(
77
                $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
                    'class' => $class,
79
                ]),
80
                $builder->getResources()
81
            );
82
        }
83
84
        require_once $file;
85
86
        return new $class();
87
    }
88
}
89