Passed
Pull Request — develop (#5)
by ANTHONIUS
04:10
created

Cache::__call()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 6
nop 2
dl 0
loc 26
ccs 0
cts 21
cp 0
crap 30
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension 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\Behat\Coverage\Bridge;
15
16
use Doyo\Behat\Coverage\Bridge\Aggregate;
0 ignored issues
show
Bug introduced by
The type Doyo\Behat\Coverage\Bridge\Aggregate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SebastianBergmann\CodeCoverage\CodeCoverage;
18
use SebastianBergmann\CodeCoverage\Filter;
19
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
20
21
/**
22
 * Class CoverageRepository.
23
 *
24
 * @method void      setCoverage(Aggregate $coverage)
25
 * @method Aggregate getCoverage()
26
 * @method bool      hasCoverage()
27
 * @method void      delCoverage()
28
 **
29
 * @method void   setFilter(Filter $filter)
30
 * @method bool   hasFilter()
31
 * @method Filter getFilter()
32
 **
33
 * @method void   setCoverageId(string $id)
34
 * @method bool   hasCoverageId()
35
 * @method string getCoverageId()
36
 **
37
 * @method void delete($id)
38
 * @method bool has($id)
39
 */
40
class Cache
41
{
42
    /**
43
     * @var FilesystemAdapter
44
     */
45
    private $cache;
46
47
    /**
48
     * @var CodeCoverage
49
     */
50
    private $coverage;
51
52
    public function __construct($name)
53
    {
54
        $dir         = sys_get_temp_dir().'/doyo/behat-coverage';
55
        $this->cache = new FilesystemAdapter($name, 0, $dir);
56
    }
57
58
    public function start()
59
    {
60
        $id      = $this->getCoverageId();
61
        $filter  = $this->getFilter();
62
        $factory = $this->getDriverFactory();
0 ignored issues
show
Bug introduced by
The method getDriverFactory() does not exist on Doyo\Behat\Coverage\Bridge\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-call */ 
63
        $factory = $this->getDriverFactory();
Loading history...
63
        $driver  = $factory->create();
64
65
        $coverage = new CodeCoverage($driver, $filter);
66
        $coverage->start($id);
67
        $this->coverage = $coverage;
68
        register_shutdown_function([$this, 'stop']);
69
    }
70
71
    public function stop()
72
    {
73
        $coverage = $this->coverage;
74
        $this->coverage->stop(true);
75
76
        if (!$this->hasCoverage()) {
77
            $this->setCoverage(new Aggregate());
78
        }
79
80
        $aggregat = $this->getCoverage();
81
82
        foreach ($coverage->getData() as $class => $counts) {
83
            $aggregat->update($class, $counts);
84
        }
85
86
        $this->setCoverage($aggregat);
87
    }
88
89
    public function set($id, $data)
90
    {
91
        $data  = serialize($data);
92
        $cache = $this->cache;
93
        $item  = $cache->getItem($id);
94
        $item->set($data);
95
        $cache->save($item);
96
    }
97
98
    public function get($id)
99
    {
100
        if (!$this->has($id)) {
101
            throw new \Exception('Cache item: '.$id.' is not set.');
102
        }
103
        $value = $this->cache->get($id, function ($item) {
104
            return $item;
105
        });
106
107
        return unserialize($value);
108
    }
109
110
    public function __call($name, $arguments)
111
    {
112
        $map         = ['has' => 'hasItem', 'del' => 'delete'];
113
        $cacheMethod = ['get', 'set', 'has', 'del'];
114
        $cache       = $this->cache;
115
116
        $lower = strtolower($name);
117
        $sub   = substr($lower, 0, 3);
118
119
        if (\in_array($sub, $cacheMethod, true) && \strlen($name) > 3) {
120
            $arg0 = substr($lower, 3);
121
            $name = substr($lower, 0, 3);
122
            array_unshift($arguments, $arg0);
123
        }
124
125
        $callable = [$this, $name];
126
127
        if (!method_exists($this, $name)) {
128
            $name = $map[$name] ?? $name;
129
            if (!method_exists($cache, $name)) {
130
                throw new \Exception('Method not exists: '.$name);
131
            }
132
            $callable = [$cache, $name];
133
        }
134
135
        return \call_user_func_array($callable, $arguments);
136
    }
137
}
138