Passed
Pull Request — master (#18)
by ANTHONIUS
05:16
created

ContainerFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 126
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeConfig() 0 8 2
B doCreateContainer() 0 53 7
A createCodeCoverage() 0 13 2
A __construct() 0 4 1
A getContainer() 0 3 1
A importFile() 0 3 1
A createApplication() 0 3 1
A createProcessor() 0 5 1
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 37
    public function __construct(array $config = [])
39
    {
40 37
        $this->config = $config;
41 37
        $this->doCreateContainer();
42
    }
43
44
    /**
45
     * @return ContainerInterface
46
     */
47 33
    public function getContainer(): ContainerInterface
48
    {
49 33
        return $this->container;
50
    }
51
52
    /**
53
     * @param bool $useDummyDriver
54
     *
55
     * @return ProcessorInterface
56
     */
57 17
    public function createProcessor(bool $useDummyDriver = false): ProcessorInterface
58
    {
59 17
        $coverage = $this->createCodeCoverage($useDummyDriver);
60
61 17
        return new Processor($coverage);
62
    }
63
64 18
    public function createCodeCoverage(bool $useDummyDriver = false)
65
    {
66 18
        $container   = $this->container;
67 18
        $driverClass = $container->getParameter('coverage.driver.class');
68
69 18
        if ($useDummyDriver) {
70 11
            $driverClass = $container->getParameter('coverage.driver.dummy.class');
71
        }
72
73 18
        $driver = new $driverClass();
74 18
        $filter = $container->get('coverage.filter');
75
76 18
        return new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
77
    }
78
79 1
    public function createApplication($version = 'dev')
80
    {
81 1
        return new Application('code-coverage', $version);
82
    }
83
84 37
    private function doCreateContainer()
85
    {
86 37
        $config  = $this->config;
87 37
        $configs = [];
88
89 37
        if (isset($config['imports'])) {
90 6
            $configs = $this->normalizeConfig($config);
91 6
            unset($config['imports']);
92
        }
93
94 37
        $configs[] = $config;
95
96 37
        $debug = false;
97 37
        foreach ($configs as $config) {
98 37
            if (isset($config['debug'])) {
99 18
                $debug = $config['debug'];
100
            }
101
        }
102
103 37
        $id              = md5(serialize($configs));
104 37
        $file            = sys_get_temp_dir().'/doyo/coverage/container'.$id.'.php';
105 37
        $class           = 'CodeCoverageContainer'.$id;
106 37
        $cachedContainer = new ConfigCache($file, $debug);
107 37
        $debug           = true;
108 37
        if (!$cachedContainer->isFresh() || $debug) {
0 ignored issues
show
introduced by
The condition $debug is always true.
Loading history...
109
            //$this->dumpConfig();
110 37
            $builder = new ContainerBuilder();
111
112 37
            $builder->registerExtension(new CodeCoverageExtension());
113 37
            foreach ($configs as $config) {
114 37
                $builder->loadFromExtension('coverage', $config);
115
            }
116
117 37
            $builder->addCompilerPass(new CoveragePass());
118 37
            $builder->addCompilerPass(new ReportPass());
119 37
            $builder->compile(true);
120
121 37
            $dumper = new PhpDumper($builder);
122 37
            $cachedContainer->write(
123 37
                $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

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