1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cache; |
4
|
|
|
|
5
|
|
|
use React\Cache\CacheInterface; |
6
|
|
|
use React\Filesystem\FilesystemInterface as ReactFilesystem; |
7
|
|
|
use React\Filesystem\Node\FileInterface; |
8
|
|
|
use React\Filesystem\Node\NodeInterface; |
9
|
|
|
use function React\Promise\all; |
10
|
|
|
use React\Promise\Promise; |
11
|
|
|
use React\Promise\PromiseInterface; |
12
|
|
|
use function React\Promise\reject; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
use Throwable; |
15
|
|
|
|
16
|
|
|
final class Filesystem implements CacheInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ReactFilesystem |
20
|
|
|
*/ |
21
|
|
|
private $filesystem; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* filesystem constructor. |
30
|
|
|
* @param ReactFilesystem $filesystem |
31
|
|
|
* @param string $path |
32
|
|
|
*/ |
33
|
11 |
|
public function __construct(ReactFilesystem $filesystem, string $path) |
34
|
|
|
{ |
35
|
11 |
|
$this->filesystem = $filesystem; |
36
|
11 |
|
$this->path = $path; |
37
|
11 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $key |
41
|
|
|
* @param null|mixed $default |
42
|
|
|
* @return PromiseInterface |
43
|
|
|
*/ |
44
|
5 |
|
public function get($key, $default = null): PromiseInterface |
45
|
|
|
{ |
46
|
|
|
return $this->has($key)->then(function (bool $has) use ($key, $default) { |
47
|
5 |
|
if ($has === true) { |
48
|
4 |
|
return $this->getFile($key)->getContents(); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return resolve($default); |
52
|
5 |
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @param null|mixed $ttl |
59
|
|
|
*/ |
60
|
5 |
|
public function set($key, $value, $ttl = null): PromiseInterface |
61
|
|
|
{ |
62
|
5 |
|
$file = $this->getFile($key); |
63
|
5 |
|
if (\strpos($key, \DIRECTORY_SEPARATOR) === false) { |
64
|
1 |
|
return $this->putContents($file, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$path = \explode(\DIRECTORY_SEPARATOR, $key); |
68
|
4 |
|
\array_pop($path); |
69
|
4 |
|
$path = \implode(\DIRECTORY_SEPARATOR, $path); |
70
|
|
|
|
71
|
4 |
|
$dir = $this->filesystem->dir($this->path . $path); |
72
|
|
|
|
73
|
|
|
return $dir->createRecursive()->then(null, function (Throwable $error) { |
74
|
2 |
|
if ($error->getMessage() === 'mkdir(): File exists') { |
75
|
2 |
|
return resolve(true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return reject($error); |
79
|
|
|
})->then(function () use ($file, $value): PromiseInterface { |
80
|
4 |
|
return $this->putContents($file, $value); |
81
|
4 |
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $key |
86
|
|
|
*/ |
87
|
4 |
|
public function delete($key): PromiseInterface |
88
|
|
|
{ |
89
|
|
|
return $this->has($key)->then(function () use ($key): PromiseInterface { |
90
|
4 |
|
return $this->getFile($key)->remove(); |
91
|
|
|
})->then(function () { |
92
|
1 |
|
return resolve(true); |
93
|
|
|
}, function () { |
94
|
3 |
|
return resolve(false); |
95
|
4 |
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
public function getMultiple(array $keys, $default = null): PromiseInterface |
99
|
|
|
{ |
100
|
2 |
|
$promises = []; |
101
|
2 |
|
foreach ($keys as $key) { |
102
|
2 |
|
$promises[$key] = $this->get($key, $default); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return all($promises); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function setMultiple(array $values, $ttl = null): PromiseInterface |
109
|
|
|
{ |
110
|
2 |
|
$promises = []; |
111
|
2 |
|
foreach ($values as $key => $value) { |
112
|
2 |
|
$promises[$key] = $this->set($key, $value, $ttl); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
return all($promises)->then(function ($results) { |
|
|
|
|
116
|
2 |
|
foreach ($results as $result) { |
117
|
2 |
|
if ($result === false) { |
118
|
|
|
return resolve(false); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
return resolve(true); |
123
|
2 |
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function deleteMultiple(array $keys): PromiseInterface |
127
|
|
|
{ |
128
|
|
|
$promises = []; |
129
|
|
|
foreach ($keys as $key) { |
130
|
|
|
$promises[$key] = $this->delete($key); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
return all($promises)->then(function ($results) { |
|
|
|
|
134
|
|
|
foreach ($results as $result) { |
135
|
|
|
if ($result === false) { |
136
|
|
|
return resolve(false); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return resolve(true); |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function clear(): PromiseInterface |
145
|
|
|
{ |
146
|
|
|
return (new Promise(function ($resolve, $reject): void { |
147
|
1 |
|
$stream = $this->filesystem->dir($this->path)->lsRecursiveStreaming(); |
|
|
|
|
148
|
|
|
$stream->on('data', function (NodeInterface $node) use ($reject): void { |
149
|
1 |
|
if ($node instanceof FileInterface === false) { |
150
|
1 |
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
$node->remove()->then(null, $reject); |
154
|
1 |
|
}); |
155
|
1 |
|
$stream->on('error', $reject); |
156
|
1 |
|
$stream->on('close', $resolve); |
157
|
|
|
}))->then(function () { |
158
|
1 |
|
return resolve(true); |
159
|
1 |
|
}); |
160
|
|
|
} |
161
|
|
|
|
162
|
9 |
|
public function has($key): PromiseInterface |
163
|
|
|
{ |
164
|
|
|
return $this->getFile($key)->exists()->then(function () { |
165
|
5 |
|
return resolve(true); |
166
|
|
|
}, function () { |
167
|
7 |
|
return resolve(false); |
168
|
9 |
|
}); |
169
|
|
|
} |
170
|
|
|
|
171
|
5 |
|
private function putContents(FileInterface $file, $value): PromiseInterface |
172
|
|
|
{ |
173
|
|
|
return $file->putContents($value)->then(function () { |
174
|
5 |
|
return resolve(true); |
175
|
|
|
}, function () { |
176
|
|
|
return resolve(false); |
177
|
5 |
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $key |
182
|
|
|
* @return FileInterface |
183
|
|
|
*/ |
184
|
11 |
|
private function getFile($key): FileInterface |
185
|
|
|
{ |
186
|
11 |
|
return $this->filesystem->file($this->path . $key); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.