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

ContainerFactory::createProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 9
cts 11
cp 0.8182
crap 2.024
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\DependencyInjection\CodeCoverageExtension;
19
use Doyo\Bridge\CodeCoverage\Driver\Dummy;
20
use Doyo\Bridge\CodeCoverage\Exception\SessionException;
21
use Doyo\Bridge\CodeCoverage\Session\LocalSession;
22
use Doyo\Bridge\CodeCoverage\Session\RemoteSession;
23
use Doyo\Bridge\CodeCoverage\Session\SessionInterface;
24
use Symfony\Component\Config\ConfigCache;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
28
29
class ContainerFactory
30
{
31
    /**
32
     * @var string
33
     */
34
    private $id;
35
36
    /**
37
     * @var string
38
     */
39
    private $class;
40
41
    /**
42
     * @var array
43
     */
44
    private $config;
45
46
    /**
47
     * @var ContainerInterface
48
     */
49
    private $container;
50
51 3
    public function __construct(array $config = [])
52
    {
53 3
        $id    = md5(serialize($config));
54 3
        $class = 'CodeCoverageContainer'.$id;
55
56 3
        $this->id     = $id;
57 3
        $this->class  = $class;
58 3
        $this->config = $config;
59
    }
60
61
    /**
62
     * @return ContainerInterface
63
     */
64 2
    public function getContainer(): ContainerInterface
65
    {
66 2
        if(is_null($this->container)){
67 2
            $this->doCreateContainer();
68
        }
69
70 2
        return $this->container;
71
    }
72
73
    /**
74
     * @param bool $useDummyDriver
75
     * @return ProcessorInterface
76
     */
77 1
    public function createProcessor(bool $useDummyDriver = false): ProcessorInterface
78
    {
79 1
        $container = $this->container;
80 1
        $driverClass = Dummy::class;
81 1
        if(!$useDummyDriver){
82 1
            $driverClass = $container->getParameter('coverage.driver.class');
83
        }
84
85 1
        $filter = $container->get('coverage.filter');
86 1
        $driver = new $driverClass;
87 1
        $processor = new Processor($driver, $filter);
88
89 1
        return $processor;
90
    }
91
92
    /**
93
     * @param   string $type
94
     * @param   string $name
95
     * @return  SessionInterface
96
     * @throws SessionException
97
     */
98
    public function createSession($type, $name): SessionInterface
99
    {
100
        $container = $this->container;
101
102
        $map = [
103
            'local' => LocalSession::class,
104
            'remote' => RemoteSession::class
105
        ];
106
107
        if(!isset($map[$type])){
108
            throw new SessionException('Unknown session type: '.$type, ' for: '.$name);
0 ignored issues
show
Bug introduced by
' for: ' . $name of type string is incompatible with the type integer expected by parameter $code of Doyo\Bridge\CodeCoverage...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            throw new SessionException('Unknown session type: '.$type, /** @scrutinizer ignore-type */ ' for: '.$name);
Loading history...
109
        }
110
111
        $codeCoverage = $this->createCodeCoverage();
112
        $patchXdebug = $container->getParameter('coverage.patch_xdebug');
113
        $filter = $container->get('coverage.filter');
114
        $processor = new Processor(new Dummy(), $filter);
115
        $class = $map[$type];
116
117
        /* @var SessionInterface $session */
118
        $session = new $class($name, $codeCoverage, $patchXdebug);
119
        $session->setProcessor($processor);
0 ignored issues
show
Bug introduced by
The method setProcessor() does not exist on Doyo\Bridge\CodeCoverage\Session\SessionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        $session->/** @scrutinizer ignore-call */ 
120
                  setProcessor($processor);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
121
        return $session;
122
    }
123
124
    public function createCodeCoverage()
125
    {
126
        $container = $this->container;
127
        $driverClass = $container->getParameter('coverage.driver.class');
128
        $driver = new $driverClass;
129
        $filter = $container->get('coverage.filter');
130
        $coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
131
132
        return $coverage;
133
    }
134
135 2
    private function doCreateContainer()
136
    {
137 2
        $id    = $this->id;
138 2
        $class = $this->class;
139 2
        $file  = sys_get_temp_dir().'/doyo/coverage/'.$id.'.php';
140
        //$config = ['config' => $this->config];
141 2
        $config = $this->config;
142 2
        $debug = isset($config['debug']) ? $config['debug']:false;
143
144 2
        $cachedContainer = new ConfigCache($file, $debug);
145 2
        if (!$cachedContainer->isFresh() || $debug) {
146
            //$this->dumpConfig();
147 2
            $builder = new ContainerBuilder();
148
149 2
            $builder->registerExtension(new CodeCoverageExtension());
150 2
            $builder->loadFromExtension('coverage', $config);
151
152 2
            $builder->addCompilerPass(new CoveragePass());
153 2
            $builder->addCompilerPass(new ReportPass());
154 2
            $builder->compile(true);
155
156 2
            $dumper = new PhpDumper($builder);
157 2
            $cachedContainer->write(
158 2
                $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

158
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
159 2
                    'class' => $class,
160
                ]),
161 2
                $builder->getResources()
162
            );
163
        }
164
165 2
        require_once $file;
166
167
        /* @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
168 2
        $container =  new $class();
169 2
        $container->set('factory', $this);
170
171 2
        $this->container = $container;
172
    }
173
}
174