Passed
Push — master ( 146be5...1e12ef )
by ANTHONIUS
06:12
created

AbstractSession::hasExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doyo\Bridge\CodeCoverage\Session;
4
5
use Doyo\Bridge\CodeCoverage\ContainerFactory;
6
use Doyo\Bridge\CodeCoverage\Exception\SessionException;
7
use Doyo\Bridge\CodeCoverage\ProcessorInterface;
8
use Doyo\Bridge\CodeCoverage\TestCase;
9
use Psr\Container\ContainerInterface;
10
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
11
12
abstract class AbstractSession implements SessionInterface, \Serializable
13
{
14
    const CACHE_KEY = 'session';
15
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * @var TestCase|null
23
     */
24
    protected $testCase;
25
26
    /**
27
     * @var FilesystemAdapter|null
28
     */
29
    protected $adapter;
30
31
    /**
32
     * @var ProcessorInterface
33
     */
34
    protected $processor;
35
36
    /**
37
     * @var array
38
     */
39
    protected $exceptions = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $config = [];
45
46
    /**
47
     * @var ContainerInterface|null
48
     */
49
    protected $container;
50
51
    /**
52
     * @var ProcessorInterface|null
53
     */
54
    protected $currentProcessor;
55
56
    protected $cachedProperties = [
57
        'name',
58
        'processor',
59
        'exceptions',
60
        'config',
61
        'testCase',
62
    ];
63
64
    /**
65
     * AbstractSession constructor.
66
     * @param string $name
67
     * @param array $config
68
     */
69 6
    public function __construct(string $name)
70
    {
71 6
        $dir = sys_get_temp_dir() . '/doyo/code-coverage/sessions';
72 6
        $this->adapter = new FilesystemAdapter($name, 0, $dir);
73 6
        $this->refresh();
74
75 6
        $this->name = $name;
76
77 6
        register_shutdown_function([$this,'shutdown']);
78
    }
79
80 4
    public function init(array $config)
81
    {
82 4
        $this->config = $config;
83 4
        $this->createContainer($config);
84 4
        $this->processor = $this->container->get('factory')->createProcessor(true);
85 4
        $this->save();
86
    }
87
88 2
    public function serialize()
89
    {
90 2
        $data = $this->toCache();
91
92 2
        return \serialize($data);
93
    }
94
95 2
    public function unserialize($serialized)
96
    {
97 2
        $cache = \unserialize($serialized);
98 2
        $this->fromCache($cache);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 2
    public function getName(): string
105
    {
106 2
        return $this->name;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 3
    public function getProcessor()
113
    {
114 3
        return $this->processor;
115
    }
116
117 6
    public function refresh()
118
    {
119 6
        $adapter = $this->adapter;
120
121 6
        $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

121
        $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...
122 6
        $this->fromCache($cached);
123
124 6
        $this->createContainer($this->config);
125
    }
126
127 6
    private function createContainer($config)
128
    {
129 6
        $container = (new ContainerFactory($config))->getContainer();
130 6
        $this->container = $container;
131
    }
132
133 4
    private function toCache()
134
    {
135 4
        $data = [];
136
137 4
        foreach($this->cachedProperties as $property){
138 4
            $data[$property] = $this->{$property};
139
        }
140
141 4
        return $data;
142
    }
143
144 6
    private function fromCache($cache)
145
    {
146 6
        if(is_null($cache)){
147 5
            return;
148
        }
149 3
        foreach ($cache as $name => $value){
150 3
            $this->{$name} = $value;
151
        }
152
    }
153
154 4
    public function save()
155
    {
156 4
        $adapter = $this->adapter;
157 4
        $item = $adapter->getItem(static::CACHE_KEY);
158 4
        $data = $this->toCache();
159
160 4
        $item->set($data);
161 4
        $adapter->save($item);
162
    }
163
164
    public function reset()
165
    {
166
        $this->testCase   = null;
167
        $this->exceptions = [];
168
169
        $this->processor->clear();
170
    }
171
172
    public function hasExceptions()
173
    {
174
        return count($this->exceptions) > 0;
175
    }
176
177 1
    public function getExceptions()
178
    {
179 1
        return $this->exceptions;
180
    }
181
182
    public function addException(\Exception $exception)
183
    {
184
        $message = $exception->getMessage();
185
        $id = md5($message);
186
187
        if(!isset($this->exceptions[$id])){
188
            $this->exceptions[$id] = $exception;
189
        }
190
    }
191
192
    public function setTestCase(TestCase $testCase)
193
    {
194
        $this->testCase = $testCase;
195
    }
196
197
    /**
198
     * @throws SessionException If TestCase is null
199
     */
200
    public function start()
201
    {
202
        if(is_null($this->testCase)){
203
            throw new SessionException('Can not start coverage without null TestCase');
204
        }
205
206
        try{
207
            $container = $this->container;
208
            $testCase = $this->testCase;
209
            $processor = $container->get('factory')->createProcessor();
0 ignored issues
show
Bug introduced by
The method get() 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

209
            $processor = $container->/** @scrutinizer ignore-call */ get('factory')->createProcessor();

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...
210
            $processor->setCurrentTestCase($testCase);
211
            $this->currentProcessor = $processor;
212
        }catch (\Exception $exception){
213
            $this->addException($exception);
214
        }
215
    }
216
217
    public function stop()
218
    {
219
        $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

219
        $this->currentProcessor->/** @scrutinizer ignore-call */ 
220
                                 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...
220
        $this->processor->merge($this->currentProcessor);
221
    }
222
223
    public function shutdown()
224
    {
225
        if(!is_null($this->currentProcessor)){
226
            $this->stop();
227
        }
228
        $this->save();
229
    }
230
}
231