Passed
Push — master ( 1e12ef...19fab1 )
by ANTHONIUS
11:36 queued 03:46
created

ContainerFactory::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
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 <https://itstoni.com>
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 Symfony\Component\Config\ConfigCache;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
24
use Symfony\Component\Yaml\Yaml;
25
26
class ContainerFactory
27
{
28
    /**
29
     * @var array
30
     */
31
    private $config;
32
33
    /**
34
     * @var ContainerInterface
35
     */
36
    private $container;
37
38 22
    public function __construct(array $config = [])
39
    {
40 22
        $this->config = $config;
41
    }
42
43
    /**
44
     * @return ContainerInterface
45
     */
46 21
    public function getContainer(): ContainerInterface
47
    {
48 21
        if (null === $this->container) {
49 21
            $this->doCreateContainer();
50
        }
51
52 21
        return $this->container;
53
    }
54
55
    /**
56
     * @param bool $useDummyDriver
57
     *
58
     * @return ProcessorInterface
59
     */
60 7
    public function createProcessor(bool $useDummyDriver = false): ProcessorInterface
61
    {
62 7
        $coverage = $this->createCodeCoverage($useDummyDriver);
63
64 7
        return new Processor($coverage);
65
    }
66
67 7
    public function createCodeCoverage(bool $useDummyDriver = false)
68
    {
69 7
        $container   = $this->container;
70 7
        $driverClass = $container->getParameter('coverage.driver.class');
71
72 7
        if ($useDummyDriver) {
73 4
            $driverClass = $container->getParameter('coverage.driver.dummy.class');
74
        }
75
76 7
        $driver = new $driverClass();
77 7
        $filter = $container->get('coverage.filter');
78
79 7
        return new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
80
    }
81
82
    public function createApplication($version = 'dev')
83
    {
84
        return new Application('code-coverage', $version);
85
    }
86
87 21
    private function doCreateContainer()
88
    {
89 21
        $config  = $this->config;
90 21
        $configs = [];
91
92 21
        if (isset($config['imports'])) {
93
            $configs = $this->normalizeConfig($config);
94
            unset($config['imports']);
95
        }
96
97 21
        $configs[] = $config;
98
99 21
        $debug = false;
100 21
        foreach ($configs as $config) {
101 21
            if (isset($config['debug'])) {
102
                $debug = $config['debug'];
103
            }
104
        }
105
106 21
        $id              = md5(serialize($configs));
107 21
        $file            = sys_get_temp_dir().'/doyo/coverage/container'.$id.'.php';
108 21
        $class           = 'CodeCoverageContainer'.$id;
109 21
        $cachedContainer = new ConfigCache($file, $debug);
110 21
        $debug           = true;
111 21
        if (!$cachedContainer->isFresh() || $debug) {
0 ignored issues
show
introduced by
The condition $debug is always true.
Loading history...
112
            //$this->dumpConfig();
113 21
            $builder = new ContainerBuilder();
114
115 21
            $builder->registerExtension(new CodeCoverageExtension());
116 21
            foreach ($configs as $config) {
117 21
                $builder->loadFromExtension('coverage', $config);
118
            }
119
120 21
            $builder->addCompilerPass(new CoveragePass());
121 21
            $builder->addCompilerPass(new ReportPass());
122 21
            $builder->compile(true);
123
124 21
            $dumper = new PhpDumper($builder);
125 21
            $cachedContainer->write(
126 21
                $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

126
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
127 21
                    'class' => $class,
128
                ]),
129 21
                $builder->getResources()
130
            );
131
        }
132
133 21
        require_once $file;
134
135
        /** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
136 21
        $container =  new $class();
137 21
        $container->set('factory', $this);
138
139 21
        $this->container = $container;
140
    }
141
142
    private function normalizeConfig($configuration)
143
    {
144
        $configs = [];
145
        foreach ($configuration['imports'] as $file) {
146
            $configs[] = $this->importFile($file);
147
        }
148
149
        return $configs;
150
    }
151
152
    private function importFile($file)
153
    {
154
        return Yaml::parseFile($file);
155
    }
156
}
157