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

Cache   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.83%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 23
c 4
b 3
f 0
lcom 1
cbo 3
dl 0
loc 80
ccs 46
cts 48
cp 0.9583
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A clear() 0 8 4
B exists() 0 14 5
A get() 0 6 2
A set() 0 11 1
B wrap() 0 20 9
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