Completed
Push — master ( 04839b...f87650 )
by Dmitry
03:31
created

Cache::wrap()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.0294

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
ccs 13
cts 14
cp 0.9286
rs 7.756
cc 9
eloc 14
nc 14
nop 2
crap 9.0294
1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
7
class Cache
8
{
9
    private $cache = [];
10
    private $converter;
11
12 44
    public function __construct(Filesystem $fs, Converter $converter)
13
    {
14 44
        $this->converter = $converter;
15 44
        $this->path = $fs->getPath('.cache');
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16 44
        if (!is_dir($this->path)) {
17 1
            mkdir($this->path);
18 1
            chmod($this->path, 0777);
19
        }
20 44
    }
21
22
23 2
    public function clear()
24
    {
25 2
        foreach (scandir($this->path) as $f) {
26 2
            if ($f != '.' && $f != '..') {
27 2
                unlink($this->path.'/'.$f);
28
            }
29
        }
30 2
    }
31
32 44
    public function exists($key)
33
    {
34 44
        if (array_key_exists($key, $this->cache)) {
35 4
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
36
        }
37 44
        $filename = $this->path . '/' . $key;
38 44
        if (file_exists($filename)) {
39 2
            if (!array_key_exists($key, $this->cache)) {
40 2
                $this->cache[$key] = include $filename;
41
            }
42 2
            $value = $this->cache[$key];
43 2
            return !array_key_exists('expire', $value) || $value['expire'] > Carbon::now()->getTimestamp();
44
        }
45 44
    }
46
47 4
    public function get($key)
48
    {
49 4
        if ($this->exists($key)) {
50 4
            return $this->converter->toObject($this->cache[$key]);
51
        }
52
    }
53
54 7
    public function set($key, $value)
55
    {
56 7
        $filename = $this->path . '/' . $key;
57 7
        $data = $this->converter->toArray($value);
58 7
        $string = '<?php return '.var_export($data, true).';';
59
60 7
        file_put_contents($filename, $string);
61 7
        chmod($filename, 0777);
62
63 7
        $this->cache[$key] = $data;
64 7
    }
65
66 44
    public function wrap($key, $callback)
67
    {
68 44
        if (!is_string($key)) {
69 44
            $key = md5(json_encode($key));
70
        }
71 44
        if ($this->exists($key)) {
72 4
            return $this->get($key);
73
        }
74 44
        $result = call_user_func($callback);
75 44
        $expire = null;
76 44
        if (is_array($result) && array_key_exists('expire', $result)) {
77
            $expire = $result['expire'];
78 44
        } elseif (is_object($result) && property_exists($result, 'expire')) {
79 4
            $expire = $result->expire;
80
        }
81 44
        if ($expire && $expire > Carbon::now()->getTimestamp()) {
82 3
            $this->set($key, $result);
83
        }
84 44
        return $result;
85
    }
86
}
87