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

AbstractSession::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

105
        $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...
106
107 11
        foreach($this->cachedProperties as $name){
108 11
            $this->{$name} = $cached[$name];
109
        }
110
111 11
        if(empty($this->config)){
112 11
            return;
113
        }
114
115 1
        $container = (new ContainerFactory($this->config))->getContainer();
116 1
        if(is_null($this->processor)){
117 1
            $this->processor = $container->get('factory')->createProcessor();
118
        }
119 1
        $this->container = $container;
120
    }
121
122 1
    public function save()
123
    {
124 1
        $adapter = $this->adapter;
125 1
        $item = $adapter->getItem(static::CACHE_KEY);
126
127 1
        $data = [];
128 1
        foreach($this->cachedProperties as $property){
129 1
            $data[$property] = $this->{$property};
130
        }
131
132 1
        $item->set($data);
133 1
        $adapter->save($item);
134
    }
135
136
    public function reset()
137
    {
138
        $this->testCase   = null;
139
        $this->exceptions = [];
140
141
        $this->processor->clear();
0 ignored issues
show
Bug introduced by
The method clear() 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

141
        $this->processor->/** @scrutinizer ignore-call */ 
142
                          clear();

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...
142
    }
143
144
    public function hasExceptions()
145
    {
146
        // TODO: Implement hasExceptions() method.
147
    }
148
149
    public function getExceptions()
150
    {
151
        // TODO: Implement getExceptions() method.
152
    }
153
154
    public function addException(\Exception $exception)
155
    {
156
        $message = $exception->getMessage();
157
        $id = md5($message);
158
159
        if(!isset($this->exceptions[$id])){
160
            $this->exceptions[$id] = $exception;
161
        }
162
    }
163
164
    public function setTestCase(TestCase $testCase)
165
    {
166
        // TODO: Implement setTestCase() method.
167
    }
168
169
    public function start()
170
    {
171
        try{
172
            $container = $this->container;
173
            $testCase = $this->testCase;
174
            $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

174
            $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...
175
            $processor->setCurrentTestCase($testCase);
176
            $this->currentProcessor = $processor;
177
        }catch (\Exception $exception){
178
            $this->addException($exception);
179
        }
180
    }
181
182
    public function stop()
183
    {
184
        $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

184
        $this->currentProcessor->/** @scrutinizer ignore-call */ 
185
                                 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...
185
        $this->processor->merge($this->currentProcessor);
186
    }
187
188
    public function shutdown()
189
    {
190
        if(!is_null($this->currentProcessor)){
191
            $this->stop();
192
        }
193
        $this->save();
194
    }
195
}
196