Completed
Push — master ( f0a08a...a3469c )
by Dmitry
05:34
created

Cache::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Basis;
4
5
use Basis\Converter;
6
use Carbon\Carbon;
7
8
class Cache
9
{
10
    private $converter;
11
12 42
    public function __construct(Converter $converter)
13
    {
14 42
        $this->converter = $converter;
15 42
        if (!is_dir('.cache')) {
16 4
            mkdir('.cache');
17 4
            @chown('.cache', '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('.cache', '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 42
    }
21
22
    private $cache = [];
23
24 42
    public function exists($key)
25
    {
26 42
        $filename = '.cache/'.$key;
27 42
        if (file_exists($filename)) {
28 1
            if (!array_key_exists($key, $this->cache)) {
29
                $this->cache[$key] = include $filename;
30
            }
31 1
            return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp();
32
        }
33 42
    }
34
35 1
    public function get($key)
36
    {
37 1
        if ($this->exists($key)) {
38 1
            return $this->converter->toObject($this->cache[$key]);
39
        }
40
    }
41
42 1
    public function set($key, $value)
43
    {
44 1
        $filename = '.cache/'.$key;
45 1
        $data = $this->converter->toArray($value);
46 1
        $string = '<?php return '.var_export($data, true).';';
47
48 1
        file_put_contents($filename, $string);
49 1
        @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...
50 1
        @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...
51
52 1
        $this->cache[$key] = $data;
53 1
    }
54
55 42
    public function wrap($key, $callback)
56
    {
57 42
        $hash = md5(json_encode($key));
58 42
        if ($this->exists($hash)) {
59 1
            return $this->get($hash);
60
        }
61 42
        $result = call_user_func($callback);
62 42
        $expire = null;
63 42
        if (is_array($result) && array_key_exists('expire', $result)) {
64
            $expire = $result['expire'];
65 42
        } elseif (is_object($result) && property_exists($result, 'expire')) {
66 1
            $expire = $result->expire;
67
        }
68 42
        if ($expire && $expire > Carbon::now()->getTimestamp()) {
69 1
            $this->set($hash, $result);
70
        }
71 42
        return $result;
72
    }
73
}
74