Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

Cache   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 80
ccs 39
cts 48
cp 0.8125
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
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
A __construct() 0 9 2
1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
7
class Cache
8
{
9
    private $cache = [];
10
    private $converter;
11
12
    public function __construct(Filesystem $fs, Converter $converter)
13
    {
14
        $this->converter = $converter;
15
        $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
        if (!is_dir($this->path)) {
17
            mkdir($this->path);
18
            @chmod($this->path, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
19
        }
20
    }
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 45
    public function exists($key)
33
    {
34 45
        if (array_key_exists($key, $this->cache)) {
35 5
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
36
        }
37 45
        $filename = $this->path . '/' . $key;
38 45
        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 45
    }
46
47 5
    public function get($key)
48
    {
49 5
        if ($this->exists($key)) {
50 5
            return $this->converter->toObject($this->cache[$key]);
51
        }
52
    }
53
54 8
    public function set($key, $value)
55
    {
56 8
        $filename = $this->path . '/' . $key;
57 8
        $data = $this->converter->toArray($value);
58 8
        $string = '<?php return '.var_export($data, true).';';
59
60 8
        file_put_contents($filename, $string);
61 8
        @chmod($filename, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
63 8
        $this->cache[$key] = $data;
64 8
    }
65
66 45
    public function wrap($key, $callback)
67
    {
68 45
        if (!is_string($key)) {
69 45
            $key = md5(json_encode($key));
70
        }
71 45
        if ($this->exists($key)) {
72 5
            return $this->get($key);
73
        }
74 45
        $result = call_user_func($callback);
75 45
        $expire = null;
76 45
        if (is_array($result) && array_key_exists('expire', $result)) {
77
            $expire = $result['expire'];
78 45
        } elseif (is_object($result) && property_exists($result, 'expire')) {
79 4
            $expire = $result->expire;
80
        }
81 45
        if ($expire && $expire > Carbon::now()->getTimestamp()) {
82 4
            $this->set($key, $result);
83
        }
84 45
        return $result;
85
    }
86
}
87