damax-solutions /
media
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Damax\Media\Flysystem; |
||
| 6 | |||
| 7 | use Damax\Media\Domain\Exception\StorageFailure; |
||
| 8 | use Damax\Media\Domain\Model\File; |
||
| 9 | use Damax\Media\Domain\Storage\AbstractStorage; |
||
| 10 | use Damax\Media\Domain\Storage\Keys\Keys; |
||
| 11 | use Damax\Media\Flysystem\Registry\Registry; |
||
| 12 | use Damax\Media\Type\Types; |
||
| 13 | use Throwable; |
||
| 14 | |||
| 15 | final class FlysystemStorage extends AbstractStorage |
||
| 16 | { |
||
| 17 | private $registry; |
||
| 18 | |||
| 19 | public function __construct(Types $types, Keys $keys, Registry $registry) |
||
| 20 | { |
||
| 21 | parent::__construct($types, $keys); |
||
| 22 | |||
| 23 | $this->registry = $registry; |
||
| 24 | } |
||
| 25 | |||
| 26 | protected function readFile(File $file): string |
||
| 27 | { |
||
| 28 | return $this->registry |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 29 | ->get($file->storage()) |
||
| 30 | ->read($file->key()) |
||
| 31 | ; |
||
| 32 | } |
||
| 33 | |||
| 34 | protected function streamFile(File $file) |
||
| 35 | { |
||
| 36 | return $this->registry |
||
|
0 ignored issues
–
show
|
|||
| 37 | ->get($file->storage()) |
||
| 38 | ->readStream($file->key()) |
||
| 39 | ; |
||
| 40 | } |
||
| 41 | |||
| 42 | protected function deleteFile(File $file): void |
||
| 43 | { |
||
| 44 | $this->registry |
||
| 45 | ->get($file->storage()) |
||
| 46 | ->delete($file->key()) |
||
| 47 | ; |
||
| 48 | } |
||
| 49 | |||
| 50 | protected function writeFile(string $key, string $storage, $stream): void |
||
| 51 | { |
||
| 52 | if (!$this->registry->has($storage)) { |
||
| 53 | throw StorageFailure::unsupported($storage); |
||
| 54 | } |
||
| 55 | |||
| 56 | try { |
||
| 57 | $result = $this->registry->get($storage)->writeStream($key, $stream); |
||
| 58 | } catch (Throwable $e) { |
||
| 59 | throw StorageFailure::invalidWrite($key, $e); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!$result) { |
||
| 63 | throw StorageFailure::invalidWrite($key); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 |