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

ContainerFactory::createCodeCoverage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

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

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