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