Completed
Push — master ( 47a956...6a7dae )
by Stéphane
02:49
created

Cache::getDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
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
    static $printed = false;
9
10
    protected static function getDirectory()
11
    {
12
        $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "dauxio" . DIRECTORY_SEPARATOR;
13
14
        if (!Cache::$printed) {
15
            Cache::$printed = true;
16
            Daux::writeln("Using cache dir '$dir'", OutputInterface::VERBOSITY_VERBOSE);
17
        }
18
19
        return $dir;
20
    }
21
22
    /**
23
     * Store an item in the cache for a given number of minutes.
24
     *
25
     * @param  string  $key
26
     * @param  mixed   $value
27
     * @return void
28
     */
29
    public static function put($key, $value)
30
    {
31
        Cache::ensureCacheDirectoryExists($path = Cache::path($key));
32
        file_put_contents($path, serialize($value));
33
    }
34
35
    /**
36
     * Create the file cache directory if necessary.
37
     *
38
     * @param  string  $path
39
     * @return void
40
     */
41
    protected static function ensureCacheDirectoryExists($path)
42
    {
43
        $parent = dirname($path);
44
45
        if (!file_exists($parent)) {
46
            mkdir($parent, 0777, true);
47
        }
48
    }
49
50
    /**
51
     * Remove an item from the cache.
52
     *
53
     * @param  string  $key
54
     * @return bool
55
     */
56
    public static function forget($key)
57
    {
58
        $path = Cache::path($key);
59
60
        if (file_exists($path)) {
61
            return unlink($path);
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * Retrieve an item from the cache by key.
69
     *
70
     * @param  string|array  $key
71
     * @return mixed
72
     */
73
    public static function get($key)
74
    {
75
        $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

75
        $path = Cache::path(/** @scrutinizer ignore-type */ $key);
Loading history...
76
77
        if (file_exists($path)) {
78
            return file_get_contents($path);
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * Get the full path for the given cache key.
86
     *
87
     * @param  string  $key
88
     * @return string
89
     */
90
    protected static function path($key)
91
    {
92
        $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
93
        return Cache::getDirectory().'/'.implode('/', $parts).'/'.$hash;
94
    }
95
}