Passed
Pull Request — master (#18)
by ANTHONIUS
03:14
created

AbstractSession::getName()   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.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
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\Session;
15
16
use Doyo\Bridge\CodeCoverage\ContainerFactory;
17
use Doyo\Bridge\CodeCoverage\Exception\SessionException;
18
use Doyo\Bridge\CodeCoverage\Processor;
19
use Doyo\Bridge\CodeCoverage\ProcessorInterface;
20
use Doyo\Bridge\CodeCoverage\TestCase;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use SebastianBergmann\CodeCoverage\CodeCoverage;
23
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
24
25
abstract class AbstractSession implements SessionInterface, \Serializable
26
{
27
    const CACHE_KEY = 'session';
28
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * @var TestCase|null
36
     */
37
    protected $testCase;
38
39
    /**
40
     * @var FilesystemAdapter|null
41
     */
42
    protected $adapter;
43
44
    /**
45
     * @var ProcessorInterface
46
     */
47
    protected $processor;
48
49
    /**
50
     * @var array
51
     */
52
    protected $exceptions = [];
53
54
    /**
55
     * @var array
56
     */
57
    protected $config = [];
58
59
    /**
60
     * @var ContainerInterface|null
61
     */
62
    protected $container;
63
64
    /**
65
     * @var ProcessorInterface|null
66
     */
67
    protected $currentProcessor;
68
69
    protected $cachedProperties = [
70
        'name',
71
        'processor',
72
        'exceptions',
73
        'config',
74
        'testCase',
75
    ];
76
77
    private $started = false;
78
79
    /**
80
     * AbstractSession constructor.
81
     *
82
     * @param string $name
83
     * @param array  $config
84
     */
85 16
    public function __construct(string $name)
86
    {
87 16
        $dir           = sys_get_temp_dir().'/doyo/code-coverage/sessions';
88 16
        $this->adapter = new FilesystemAdapter($name, 0, $dir);
89 16
        $this->refresh();
90
91 16
        $this->name = $name;
92
93 16
        register_shutdown_function([$this, 'shutdown']);
94
    }
95
96 9
    public function init(array $config)
97
    {
98 9
        $this->config = $config;
99 9
        $this->createContainer($config);
100 9
        $this->processor = $this->container->get('factory')->createProcessor(true);
101 9
        $this->save();
102
    }
103
104 3
    public function serialize()
105
    {
106 3
        $data = $this->toCache();
107
108 3
        return serialize($data);
109
    }
110
111 2
    public function unserialize($serialized)
112
    {
113 2
        $cache = unserialize($serialized);
114 2
        $this->fromCache($cache);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 4
    public function getName(): string
121
    {
122 4
        return $this->name;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 8
    public function getProcessor()
129
    {
130 8
        return $this->processor;
131
    }
132
133 16
    public function refresh()
134
    {
135 16
        $adapter = $this->adapter;
136
137 16
        $cached = $adapter->getItem(static::CACHE_KEY)->get();
0 ignored issues
show
Bug introduced by
The method getItem() does not exist on null. ( Ignorable by Annotation )

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

137
        $cached = $adapter->/** @scrutinizer ignore-call */ getItem(static::CACHE_KEY)->get();

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...
138
139 16
        if(!is_null($cached)){
140 12
            $this->fromCache($cached);
141
        }
142 16
        if(!empty($this->config)){
143 9
            $this->createContainer($this->config);
144
        }
145
    }
146
147 14
    private function createContainer($config)
148
    {
149 14
        $container       = (new ContainerFactory($config))->getContainer();
150 14
        $this->container = $container;
151
    }
152
153 11
    private function toCache()
154
    {
155 11
        $data = [];
156
157 11
        foreach ($this->cachedProperties as $property) {
158 11
            $data[$property] = $this->{$property};
159
        }
160
161 11
        return $data;
162
    }
163
164 12
    private function fromCache(array $cache)
165
    {
166 12
        foreach ($cache as $name => $value) {
167 12
            $this->{$name} = $value;
168
        }
169
    }
170
171 11
    public function save()
172
    {
173 11
        $adapter = $this->adapter;
174 11
        $item    = $adapter->getItem(static::CACHE_KEY);
175 11
        $data    = $this->toCache();
176
177 11
        $item->set($data);
178 11
        $adapter->save($item);
179
    }
180
181
    public function reset()
182
    {
183
        $this->testCase   = null;
184
        $this->exceptions = [];
185
186
        //$this->processor->clear();
187
    }
188
189 1
    public function hasExceptions()
190
    {
191 1
        return \count($this->exceptions) > 0;
192
    }
193
194 2
    public function getExceptions()
195
    {
196 2
        return $this->exceptions;
197
    }
198
199 4
    public function addException(\Exception $exception)
200
    {
201 4
        $message = $exception->getMessage();
202 4
        $id      = md5($message);
203
204 4
        if (!isset($this->exceptions[$id])) {
205 4
            $this->exceptions[$id] = $exception;
206
        }
207
    }
208
209 5
    public function setTestCase(TestCase $testCase)
210
    {
211 5
        $this->testCase = $testCase;
212
    }
213
214
    /**
215
     * @throws SessionException If TestCase is null
216
     */
217 5
    public function start()
218
    {
219 5
        if (null === $this->testCase) {
220 1
            throw new SessionException('Can not start coverage with null TestCase');
221
        }
222
223
        try {
224 4
            $container = $this->container;
225 4
            $testCase  = $this->testCase;
226 4
            $filter = $this->getProcessor()->getCodeCoverage()->filter();
227 1
            $class = $container->getParameter('coverage.driver.class');
0 ignored issues
show
Bug introduced by
The method getParameter() does not exist on null. ( Ignorable by Annotation )

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

227
            /** @scrutinizer ignore-call */ 
228
            $class = $container->getParameter('coverage.driver.class');

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...
228 1
            $coverage = new CodeCoverage(new $class, $filter);
229 1
            $processor = new Processor($coverage);
230 1
            $processor->start($testCase);
231
232 4
            $this->currentProcessor = $processor;
233 4
            $this->started = true;
234 3
        } catch (\Exception $exception) {
235 2
            $this->started = false;
236 2
            $message = sprintf(
237 2
                "Can not start coverage on session %s. Error message:\n%s",
238 2
                $this->getName(),
239 2
                $exception->getMessage()
240
            );
241 2
            $exception = new  SessionException($message);
242 2
            $this->addException($exception);
243
        }
244
    }
245
246 3
    public function stop()
247
    {
248
        try{
249 3
            $this->currentProcessor->stop();
0 ignored issues
show
Bug introduced by
The method stop() does not exist on null. ( Ignorable by Annotation )

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

249
            $this->currentProcessor->/** @scrutinizer ignore-call */ 
250
                                     stop();

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...
250 1
            $this->processor->merge($this->currentProcessor);
251 1
            $this->started = false;
252 2
        }catch (\Exception $exception){
253
            $message = sprintf(
254
                "Can not stop coverage on session <comment>%s</comment>. Error message:\n<error>%s</error>",
255
                $this->name,
256
                $exception->getMessage()
257
            );
258
            $e = new SessionException($message);
259
            $this->addException($e);
260
        }
261
    }
262
263 1
    public function shutdown()
264
    {
265 1
        if ($this->started) {
266 1
            $this->stop();
267
        }
268 1
        $this->save();
269
    }
270
}
271