Completed
Push — master ( 31cab7...e56a6a )
by BruceScrutinizer
02:19
created

SimpleFileCache::delete_directory()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NamespaceProtector\Cache;
6
7
use Webmozart\Assert\Assert;
8
use NamespaceProtector\Common\PathInterface;
9
10
final class SimpleFileCache implements \Psr\SimpleCache\CacheInterface
11
{
12
    /** @var PathInterface */
13
    private $path;
14
15
    public function __construct(PathInterface $cachePath)
16
    {
17
        $this->path = $cachePath;
18
        if (!\is_dir($this->path->get())) {
19
            \safe\mkdir($this->path->get());
20
        }
21
    }
22
23
    public function get($key, $default = null)
24
    {
25
        $fileCached = $this->createFileNameFromKey($key);
26
        if (!\file_exists($fileCached)) {
27
            return $default;
28
        }
29
30
        $jsonDecoder = new \PhpParser\JsonDecoder();
31
32
        $content = \Safe\file_get_contents($fileCached);
33
34
        return $jsonDecoder->decode($content);
35
    }
36
37
    public function set($key, $value, $ttl = null)
38
    {
39
        $fileCached = $this->createFileNameFromKey($key);
40
        \file_put_contents($fileCached, json_encode($value, JSON_PRETTY_PRINT));
41
42
        return true;
43
    }
44
45
    public function delete($key)
46
    {
47
        $fileCached = $this->createFileNameFromKey($key);
48
        \unlink($fileCached);
49
50
        return true;
51
    }
52
53
    public function clear()
54
    {
55
        $this->delete_directory($this->path->get());
56
        return true;
57
    }
58
59
    /**
60
     * @param array<string> $keys
61
     * @return array<mixed>
62
     */
63
    public function getMultiple($keys, $default = null)
64
    {
65
        throw new \RuntimeException('Non implemented yet');
66
    }
67
68
    /**
69
     * @param array<mixed> $values
70
     * @return bool
71
     */
72
    public function setMultiple($values, $ttl = null)
73
    {
74
        throw new \RuntimeException('Non implemented yet');
75
    }
76
77
    /**
78
     * @param array<string> $keys
79
     * @return bool
80
     */
81
    public function deleteMultiple($keys)
82
    {
83
        throw new \RuntimeException('Non implemented yet');
84
    }
85
86
    public function has($key)
87
    {
88
        $fileCached = $this->createFileNameFromKey($key);
89
        if (\file_exists($fileCached)) {
90
            return true;
91
        }
92
93
        return false;
94
    }
95
96
    private function createFileNameFromKey(string $key): string
97
    {
98
        return $this->path->get() . '/' . $key;
99
    }
100
101
    private function delete_directory(string $dirname): bool
102
    {
103
        $dir_handle = \Safe\opendir($dirname);
104
        Assert::notNull($dir_handle);
105
106
        while ($file = readdir($dir_handle)) {
107
            if ($file != "." && $file != "..") {
108
                if (!is_dir($dirname . \DIRECTORY_SEPARATOR . $file)) {
109
                    \Safe\unlink($dirname . \DIRECTORY_SEPARATOR . $file);
110
                } else {
111
                    $this->delete_directory($dirname . \DIRECTORY_SEPARATOR . $file);
112
                }
113
            }
114
        }
115
        closedir($dir_handle);
116
        \rmdir($dirname);
117
        return true;
118
    }
119
}
120