Completed
Push — master ( 19fab1...ec2cfc )
by ANTHONIUS
23s queued 11s
created

ContainerFactory::createApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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