Completed
Push — master ( 9e93cb...04839b )
by Dmitry
03:09
created

Cache   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.45%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 18
c 4
b 3
f 0
lcom 1
cbo 3
dl 0
loc 72
ccs 42
cts 44
cp 0.9545
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 2
A set() 0 12 1
B wrap() 0 20 9
A exists() 0 13 4
A __construct() 0 10 2
1
<?php
2
3
namespace Basis;
4
5
use Carbon\Carbon;
6
7
class Cache
8
{
9
    private $converter;
10
11 44
    public function __construct(Filesystem $fs, Converter $converter)
12
    {
13 44
        $this->converter = $converter;
14 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...
15 44
        if (!is_dir($this->path)) {
16 4
            mkdir($this->path);
17 4
            @chown($this->path, 'www-data');
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...
18 4
            @chgrp($this->path, 'www-data');
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 44
    }
21
22
    private $cache = [];
23
24 44
    public function exists($key)
25
    {
26 44
        if (array_key_exists($key, $this->cache)) {
27 4
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
28
        }
29 44
        $filename = $this->path . '/' . $key;
30 44
        if (file_exists($filename)) {
31 2
            if (!array_key_exists($key, $this->cache)) {
32 2
                $this->cache[$key] = include $filename;
33
            }
34 2
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
35
        }
36 44
    }
37
38 4
    public function get($key)
39
    {
40 4
        if ($this->exists($key)) {
41 4
            return $this->converter->toObject($this->cache[$key]);
42
        }
43
    }
44
45 3
    public function set($key, $value)
46
    {
47 3
        $filename = $this->path . '/' . $key;
48 3
        $data = $this->converter->toArray($value);
49 3
        $string = '<?php return '.var_export($data, true).';';
50
51 3
        file_put_contents($filename, $string);
52 3
        @chown($filename, 'www-data');
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...
53 3
        @chgrp($filename, 'www-data');
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...
54
55 3
        $this->cache[$key] = $data;
56 3
    }
57
58 44
    public function wrap($key, $callback)
59
    {
60 44
        if (!is_string($key)) {
61 44
            $key = md5(json_encode($key));
62
        }
63 44
        if ($this->exists($key)) {
64 4
            return $this->get($key);
65
        }
66 44
        $result = call_user_func($callback);
67 44
        $expire = null;
68 44
        if (is_array($result) && array_key_exists('expire', $result)) {
69
            $expire = $result['expire'];
70 44
        } elseif (is_object($result) && property_exists($result, 'expire')) {
71 4
            $expire = $result->expire;
72
        }
73 44
        if ($expire && $expire > Carbon::now()->getTimestamp()) {
74 3
            $this->set($key, $result);
75
        }
76 44
        return $result;
77
    }
78
}
79