Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
38 / 38 |
|
100.00% |
33 / 33 |
CRAP | |
100.00% |
1 / 1 |
Storage | |
100.00% |
38 / 38 |
|
100.00% |
33 / 33 |
35 | |
100.00% |
1 / 1 |
createLocal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createS3 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createAzure | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
setBaseDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBaseDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
chdir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mkdir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
rmdir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
listAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
listDirs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
listFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
putFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
putFileContents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
uploadFiles | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
uploadFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
copyFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
copyFileToExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
copyFileFromExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
moveFileToExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
moveFileFromExternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
renameFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
replaceFileContents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fetchFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fetchFileInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fileExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileMTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
md5File | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (https://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@noladev.com> |
7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
8 | * @license https://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Storage; |
15 | |
16 | use Aws\S3\S3Client; |
17 | |
18 | /** |
19 | * Storage class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Storage |
23 | * @author Nick Sagona, III <dev@noladev.com> |
24 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
25 | * @license https://www.popphp.org/license New BSD License |
26 | * @version 2.1.0 |
27 | */ |
28 | class Storage extends AbstractStorage |
29 | { |
30 | |
31 | /** |
32 | * Create storage object with local adapter |
33 | * |
34 | * @param string $directory |
35 | * @return Storage |
36 | */ |
37 | public static function createLocal(string $directory): Storage |
38 | { |
39 | return new self(new Adapter\Local($directory)); |
40 | } |
41 | |
42 | /** |
43 | * Create storage object with S3 adapter |
44 | * |
45 | * @param string $directory |
46 | * @param S3Client $client |
47 | * @return Storage |
48 | */ |
49 | public static function createS3(string $directory, S3Client $client): Storage |
50 | { |
51 | return new self(new Adapter\S3($directory, $client)); |
52 | } |
53 | |
54 | /** |
55 | * Create storage object with S3 adapter |
56 | * |
57 | * @param string $accountName |
58 | * @param string $accountKey |
59 | * @param ?string $container |
60 | * @throws \Pop\Http\Client\Exception |
61 | * @return Storage |
62 | */ |
63 | public static function createAzure(string $accountName, string $accountKey, ?string $container = null): Storage |
64 | { |
65 | $azure = new self(Adapter\Azure::create($accountName, $accountKey)); |
66 | if ($container != null) { |
67 | $azure->setBaseDir($container); |
68 | $azure->chdir(); |
69 | } |
70 | return $azure; |
71 | } |
72 | |
73 | /** |
74 | * Set base directory |
75 | * |
76 | * @param ?string $directory |
77 | * @return void |
78 | */ |
79 | public function setBaseDir(?string $directory = null): void |
80 | { |
81 | $this->adapter->setBaseDir($directory); |
82 | } |
83 | |
84 | /** |
85 | * Get base directory |
86 | * |
87 | * @return ?string |
88 | */ |
89 | public function getBaseDir(): ?string |
90 | { |
91 | return $this->adapter->getBaseDir(); |
92 | } |
93 | |
94 | /** |
95 | * Get current directory |
96 | * |
97 | * @return ?string |
98 | */ |
99 | public function getCurrentDir(): ?string |
100 | { |
101 | return $this->adapter->getCurrentDir(); |
102 | } |
103 | |
104 | /** |
105 | * Change directory |
106 | * |
107 | * @param ?string $directory |
108 | * @return void |
109 | */ |
110 | public function chdir(?string $directory = null): void |
111 | { |
112 | $this->adapter->chdir($directory); |
113 | } |
114 | |
115 | /** |
116 | * Make directory |
117 | * |
118 | * @param string $directory |
119 | * @return void |
120 | */ |
121 | public function mkdir(string $directory): void |
122 | { |
123 | $this->adapter->mkdir($directory); |
124 | } |
125 | |
126 | /** |
127 | * Remove a directory |
128 | * |
129 | * @param string $directory |
130 | * @return void |
131 | */ |
132 | public function rmdir(string $directory): void |
133 | { |
134 | $this->adapter->rmdir($directory); |
135 | } |
136 | |
137 | /** |
138 | * List all |
139 | * |
140 | * @param ?string $search |
141 | * @return array |
142 | */ |
143 | public function listAll(?string $search = null): array |
144 | { |
145 | return $this->adapter->listAll($search); |
146 | } |
147 | |
148 | /** |
149 | * List directories |
150 | * |
151 | * @param ?string $search |
152 | * @return array |
153 | */ |
154 | public function listDirs(?string $search = null): array |
155 | { |
156 | return $this->adapter->listDirs($search); |
157 | } |
158 | |
159 | /** |
160 | * List files |
161 | * |
162 | * @param ?string $search |
163 | * @return array |
164 | */ |
165 | public function listFiles(?string $search = null): array |
166 | { |
167 | return $this->adapter->listFiles($search); |
168 | } |
169 | |
170 | /** |
171 | * Put file |
172 | * |
173 | * @param string $fileFrom |
174 | * @param bool $copy |
175 | * @return void |
176 | */ |
177 | public function putFile(string $fileFrom, bool $copy = true): void |
178 | { |
179 | $this->adapter->putFile($fileFrom, $copy); |
180 | } |
181 | |
182 | /** |
183 | * Put file contents |
184 | * |
185 | * @param string $filename |
186 | * @param string $fileContents |
187 | * @return void |
188 | */ |
189 | public function putFileContents(string $filename, string $fileContents): void |
190 | { |
191 | $this->adapter->putFileContents($filename, $fileContents); |
192 | } |
193 | |
194 | /** |
195 | * Upload files from server request $_FILES |
196 | * |
197 | * @param array $files |
198 | * @return void |
199 | */ |
200 | public function uploadFiles(array $files): void |
201 | { |
202 | foreach ($files as $file) { |
203 | $this->adapter->uploadFile($file); |
204 | } |
205 | } |
206 | |
207 | /** |
208 | * Upload file from server request $_FILES['file'] |
209 | * |
210 | * @param array $file |
211 | * @return void |
212 | */ |
213 | public function uploadFile(array $file): void |
214 | { |
215 | $this->adapter->uploadFile($file); |
216 | } |
217 | |
218 | /** |
219 | * Copy file |
220 | * |
221 | * @param string $sourceFile |
222 | * @param string $destFile |
223 | * @return void |
224 | */ |
225 | public function copyFile(string $sourceFile, string $destFile): void |
226 | { |
227 | $this->adapter->copyFile($sourceFile, $destFile); |
228 | } |
229 | |
230 | /** |
231 | * Copy file to a location external to the current location |
232 | * |
233 | * @param string $sourceFile |
234 | * @param string $externalFile |
235 | * @return void |
236 | */ |
237 | public function copyFileToExternal(string $sourceFile, string $externalFile): void |
238 | { |
239 | $this->adapter->copyFileToExternal($sourceFile, $externalFile); |
240 | } |
241 | |
242 | /** |
243 | * Copy file from a location external to the current location |
244 | * |
245 | * @param string $externalFile |
246 | * @param string $destFile |
247 | * @return void |
248 | */ |
249 | public function copyFileFromExternal(string $externalFile, string $destFile): void |
250 | { |
251 | $this->adapter->copyFileFromExternal($externalFile, $destFile); |
252 | } |
253 | |
254 | /** |
255 | * Move file to a location external to the current location |
256 | * |
257 | * @param string $sourceFile |
258 | * @param string $externalFile |
259 | * @return void |
260 | */ |
261 | public function moveFileToExternal(string $sourceFile, string $externalFile): void |
262 | { |
263 | $this->adapter->moveFileToExternal($sourceFile, $externalFile); |
264 | } |
265 | |
266 | /** |
267 | * Move file from a location external to the current location |
268 | * |
269 | * @param string $externalFile |
270 | * @param string $destFile |
271 | * @return void |
272 | */ |
273 | public function moveFileFromExternal(string $externalFile, string $destFile): void |
274 | { |
275 | $this->adapter->moveFileFromExternal($externalFile, $destFile); |
276 | } |
277 | |
278 | /** |
279 | * Rename file |
280 | * |
281 | * @param string $oldFile |
282 | * @param string $newFile |
283 | * @return void |
284 | */ |
285 | public function renameFile(string $oldFile, string $newFile): void |
286 | { |
287 | $this->adapter->renameFile($oldFile, $newFile); |
288 | } |
289 | |
290 | /** |
291 | * Replace file |
292 | * |
293 | * @param string $filename |
294 | * @param string $fileContents |
295 | * @return void |
296 | */ |
297 | public function replaceFileContents(string $filename, string $fileContents): void |
298 | { |
299 | $this->adapter->replaceFileContents($filename, $fileContents); |
300 | } |
301 | |
302 | /** |
303 | * Delete file |
304 | * |
305 | * @param string $filename |
306 | * @return void |
307 | */ |
308 | public function deleteFile(string $filename): void |
309 | { |
310 | $this->adapter->deleteFile($filename); |
311 | } |
312 | |
313 | /** |
314 | * Fetch file contents |
315 | * |
316 | * @param string $filename |
317 | * @return mixed |
318 | */ |
319 | public function fetchFile(string $filename): mixed |
320 | { |
321 | return $this->adapter->fetchFile($filename); |
322 | } |
323 | |
324 | /** |
325 | * Fetch file info |
326 | * |
327 | * @param string $filename |
328 | * @return array |
329 | */ |
330 | public function fetchFileInfo(string $filename): array |
331 | { |
332 | return $this->adapter->fetchFileInfo($filename); |
333 | } |
334 | |
335 | /** |
336 | * File exists |
337 | * |
338 | * @param string $filename |
339 | * @return bool |
340 | */ |
341 | public function fileExists(string $filename): bool |
342 | { |
343 | return $this->adapter->fileExists($filename); |
344 | } |
345 | |
346 | /** |
347 | * Check if is a dir |
348 | * |
349 | * @param string $directory |
350 | * @return bool |
351 | */ |
352 | public function isDir(string $directory): bool |
353 | { |
354 | return $this->adapter->isDir($directory); |
355 | } |
356 | |
357 | /** |
358 | * Check if is a file |
359 | * |
360 | * @param string $filename |
361 | * @return bool |
362 | */ |
363 | public function isFile(string $filename): bool |
364 | { |
365 | return $this->adapter->isFile($filename); |
366 | } |
367 | |
368 | /** |
369 | * Get file size |
370 | * |
371 | * @param string $filename |
372 | * @return int|bool |
373 | */ |
374 | public function getFileSize(string $filename): int|bool |
375 | { |
376 | return $this->adapter->getFileSize($filename); |
377 | } |
378 | |
379 | /** |
380 | * Get file type |
381 | * |
382 | * @param string $filename |
383 | * @return string|bool |
384 | */ |
385 | public function getFileType(string $filename): string|bool |
386 | { |
387 | return $this->adapter->getFileType($filename); |
388 | } |
389 | |
390 | /** |
391 | * Get file modified time |
392 | * |
393 | * @param string $filename |
394 | * @return int|string|bool |
395 | */ |
396 | public function getFileMTime(string $filename): int|string|bool |
397 | { |
398 | return $this->adapter->getFileMTime($filename); |
399 | } |
400 | |
401 | /** |
402 | * Create MD5 checksum of the file |
403 | * |
404 | * @param string $filename |
405 | * @return string|bool |
406 | */ |
407 | public function md5File(string $filename): string|bool |
408 | { |
409 | return $this->adapter->md5File($filename); |
410 | } |
411 | |
412 | } |