Completed
Push — master ( 675097...51fab9 )
by duan
01:47
created

Cache::test()   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
2
3
namespace Yiranzai\File;
4
5
/**
6
 * Class Cache
7
 * @package Yiranzai\File
8
 */
9
class Cache
10
{
11
    /**
12
     *
13
     */
14
    public const DEFAULT_PATH = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;
15
    /**
16
     * @var string
17
     */
18
    protected $dataPath = self::DEFAULT_PATH;
19
    /**
20
     * @var Filesystem
21
     */
22
    protected $file;
23
    /**
24
     * @var array
25
     */
26
    protected $guarded = ['file'];
27
28
    /**
29
     * Hash constructor.
30
     * @param array $config
31
     */
32 3
    public function __construct($config = [])
33
    {
34 3
        if (!empty($config)) {
35 3
            foreach ($config as $key => $item) {
36 3
                if (in_array($key, $this->guarded, true)) {
37 3
                    continue;
38
                }
39 3
                $this->$key = $item;
40
            }
41
        }
42 3
        $this->file = new Filesystem();
43 3
    }
44
45
    /**
46
     * @param string $key
47
     * @param string $data
48
     * @return Cache
49
     */
50 3
    public function put(string $key, string $data): Cache
51
    {
52 3
        $this->ensureCacheDirectoryExists($path = $this->path($key));
53
54 3
        if ($this->file->exists($path = $this->path($key))) {
55
            if ($bucket = $this->getBucket($path)) {
56
                $bucket->putNode($key, $data);
57
            }
58
        } else {
59 3
            $bucket = new Bucket($key, $data);
60
        }
61 3
        $this->file->put($path, serialize($bucket));
62 3
        return $this;
63
    }
64
65
    /**
66
     * @param      $key
67
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
68
     * @return null
69
     */
70 3
    public function get($key, $default = null)
71
    {
72 3
        if (!$this->file->exists($path = $this->path($key))) {
73 3
            return $default;
74
        }
75 3
        if (!$bucket = $this->getBucket($path)) {
76
            return $default;
77
        }
78 3
        if ($node = $bucket->findNode($key)) {
79 3
            return $node->data;
80
        }
81
        return $default;
82
    }
83
84
    /**
85
     * @param string $path
86
     * @return Bucket|null
87
     */
88 3
    protected function getBucket(string $path): ?Bucket
89
    {
90 3
        $bucket = $this->file->get($path);
91 3
        if ($bucket) {
92 3
            $bucket = unserialize($bucket, array('allowed_classes' => [Node::class, Bucket::class]));
93
        }
94 3
        return $bucket;
95
    }
96
97
    /**
98
     * @param string $key
99
     * @return string
100
     */
101 3
    protected function path(string $key): string
102
    {
103 3
        $parts = array_slice(str_split($hash = hash('sha256', $key), 2), 0, 2);
104
105 3
        return $this->dataPath . DIRECTORY_SEPARATOR . implode(
106 3
            DIRECTORY_SEPARATOR,
107 2
            $parts
108 3
        ) . DIRECTORY_SEPARATOR . $hash;
109
    }
110
111
    /**
112
     * @param  string $path
113
     * @return void
114
     */
115 3
    protected function ensureCacheDirectoryExists($path): void
116
    {
117 3
        if (!$this->file->exists(dirname($path))) {
118 3
            $this->file->makeDirectory(dirname($path), 0777, true, true);
119
        }
120 3
    }
121
122
    /**
123
     * @param string $path
124
     * @return $this
125
     */
126 3
    public function dataPath(string $path): self
127
    {
128 3
        $this->dataPath = $path;
129 3
        return $this;
130
    }
131
}
132