-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathLocalFileStorage.php
More file actions
39 lines (33 loc) · 1.07 KB
/
Copy pathLocalFileStorage.php
File metadata and controls
39 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace App\Structural\Adapter;
class LocalFileStorage implements FileAdapter
{
public const STORAGE_PATH = "storage" . DIRECTORY_SEPARATOR;
/**
* @throws \Exception
*/
public function get(string $name): File
{
$fullPath = self::STORAGE_PATH . $name;
if (file_exists($fullPath)) {
return new File($name, file_get_contents($fullPath));
} else {
throw new \Exception("File {$fullPath} does not exist in local storage");
}
}
public function save(string $path, string $name): void
{
$contents = file_get_contents($path);
if ($contents === false) {
throw new \RuntimeException("Failed to read source file: {$path}");
}
if (@file_put_contents(self::STORAGE_PATH . $name, $contents) === false) {
throw new \RuntimeException("Failed to write file: " . self::STORAGE_PATH . $name);
}
}
public function delete(string $path): bool
{
return @unlink(self::STORAGE_PATH . $path);
}
}