Passed
Push — master ( 6a7dae...ba8658 )
by Stéphane
03:03
created

Cache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Todaymade\Daux;
2
3
use Symfony\Component\Console\Output\OutputInterface;
4
use Todaymade\Daux\Daux;
5
6
class Cache
7
{
8
9
    static $printed = false;
10
11
    public static function getDirectory()
12
    {
13
        $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "dauxio" . DIRECTORY_SEPARATOR;
14
15
        if (!Cache::$printed) {
16
            Cache::$printed = true;
17
            Daux::writeln("Using cache dir '$dir'", OutputInterface::VERBOSITY_VERBOSE);
18
        }
19
20
        return $dir;
21
    }
22
23
    /**
24
     * Store an item in the cache for a given number of minutes.
25
     *
26
     * @param  string  $key
27
     * @param  mixed   $value
28
     * @return void
29
     */
30
    public static function put($key, $value)
31
    {
32
        Cache::ensureCacheDirectoryExists($path = Cache::path($key));
33
        file_put_contents($path, $value);
34
    }
35
36
    /**
37
     * Create the file cache directory if necessary.
38
     *
39
     * @param  string  $path
40
     * @return void
41
     */
42
    protected static function ensureCacheDirectoryExists($path)
43
    {
44
        $parent = dirname($path);
45
46
        if (!file_exists($parent)) {
47
            mkdir($parent, 0777, true);
48
        }
49
    }
50
51
    /**
52
     * Remove an item from the cache.
53
     *
54
     * @param  string  $key
55
     * @return bool
56
     */
57
    public static function forget($key)
58
    {
59
        $path = Cache::path($key);
60
61
        if (file_exists($path)) {
62
            return unlink($path);
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * Retrieve an item from the cache by key.
70
     *
71
     * @param  string|array  $key
72
     * @return mixed
73
     */
74
    public static function get($key)
75
    {
76
        $path = Cache::path($key);
0 ignored issues
show
Bug introduced by Stéphane Goetz
It seems like $key can also be of type array; however, parameter $key of Todaymade\Daux\Cache::path() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $path = Cache::path(/** @scrutinizer ignore-type */ $key);
Loading history...
77
78
        if (file_exists($path)) {
79
            return file_get_contents($path);
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * Get the full path for the given cache key.
87
     *
88
     * @param  string  $key
89
     * @return string
90
     */
91
    protected static function path($key)
92
    {
93
        $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
94
        return Cache::getDirectory() . '/' . implode('/', $parts) . '/' . $hash;
95
    }
96
97
    public static function clear()
98
    {
99
        Cache::rrmdir(Cache::getDirectory());
100
    }
101
102
    protected static function rrmdir($dir)
103
    {
104
        if (is_dir($dir)) {
105
            $objects = scandir($dir);
106
            foreach ($objects as $object) {
107
                if ($object != "." && $object != "..") {
108
                    if (is_dir($dir . "/" . $object)) {
109
                        Cache::rrmdir($dir . "/" . $object);
110
                    } else {
111
                        unlink($dir . "/" . $object);
112
                    }
113
114
                }
115
            }
116
            rmdir($dir);
117
        }
118
    }
119
}
120