Cache   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 32
c 2
b 0
f 1
dl 0
loc 96
ccs 0
cts 46
cp 0
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A getDirectory() 0 10 2
A put() 0 4 1
A forget() 0 9 2
A get() 0 9 2
A rrmdir() 0 14 6
A ensureCacheDirectoryExists() 0 6 2
A path() 0 5 1
1
<?php namespace Todaymade\Daux;
2
3
use Symfony\Component\Console\Output\OutputInterface;
4
5
class Cache
6
{
7
    public static $printed = false;
8
9
    public static function getDirectory(): string
10
    {
11
        $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dauxio' . DIRECTORY_SEPARATOR;
12
13
        if (!Cache::$printed) {
14
            Cache::$printed = true;
15
            Daux::writeln("Using cache dir '$dir'", OutputInterface::VERBOSITY_VERBOSE);
16
        }
17
18
        return $dir;
19
    }
20
21
    /**
22
     * Store an item in the cache for a given number of minutes.
23
     */
24
    public static function put(string $key, string $value): void
25
    {
26
        Cache::ensureCacheDirectoryExists($path = Cache::path($key));
27
        file_put_contents($path, $value);
28
    }
29
30
    /**
31
     * Create the file cache directory if necessary.
32
     */
33
    protected static function ensureCacheDirectoryExists(string $path): void
34
    {
35
        $parent = dirname($path);
36
37
        if (!file_exists($parent)) {
38
            mkdir($parent, 0777, true);
39
        }
40
    }
41
42
    /**
43
     * Remove an item from the cache.
44
     */
45
    public static function forget(string $key): bool
46
    {
47
        $path = Cache::path($key);
48
49
        if (file_exists($path)) {
50
            return unlink($path);
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * Retrieve an item from the cache by key.
58
     *
59
     * @return mixed
60
     */
61
    public static function get(string $key): ?string
62
    {
63
        $path = Cache::path($key);
64
65
        if (file_exists($path)) {
66
            return file_get_contents($path);
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Get the full path for the given cache key.
74
     */
75
    protected static function path(string $key): string
76
    {
77
        $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
78
79
        return Cache::getDirectory() . '/' . implode('/', $parts) . '/' . $hash;
80
    }
81
82
    public static function clear(): void
83
    {
84
        Cache::rrmdir(Cache::getDirectory());
85
    }
86
87
    protected static function rrmdir(string $dir): void
88
    {
89
        if (is_dir($dir)) {
90
            $objects = scandir($dir);
91
            foreach ($objects as $object) {
92
                if ($object != '.' && $object != '..') {
93
                    if (is_dir($dir . '/' . $object)) {
94
                        Cache::rrmdir($dir . '/' . $object);
95
                    } else {
96
                        unlink($dir . '/' . $object);
97
                    }
98
                }
99
            }
100
            rmdir($dir);
101
        }
102
    }
103
}
104