damax-solutions /
media
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Damax\Media\Gaufrette; |
||
| 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\Type\Types; |
||
| 12 | use Gaufrette\Exception as GaufretteException; |
||
| 13 | use Gaufrette\FilesystemMap; |
||
| 14 | use RuntimeException; |
||
| 15 | |||
| 16 | final class GaufretteStorage extends AbstractStorage |
||
| 17 | { |
||
| 18 | private $filesystems; |
||
| 19 | |||
| 20 | public function __construct(Types $types, Keys $keys, FilesystemMap $filesystems) |
||
| 21 | { |
||
| 22 | parent::__construct($types, $keys); |
||
| 23 | |||
| 24 | $this->filesystems = $filesystems; |
||
| 25 | } |
||
| 26 | |||
| 27 | protected function readFile(File $file): string |
||
| 28 | { |
||
| 29 | return $this->filesystems |
||
| 30 | ->get($file->storage()) |
||
| 31 | ->get($file->key()) |
||
| 32 | ->getContent() |
||
| 33 | ; |
||
| 34 | } |
||
| 35 | |||
| 36 | protected function streamFile(File $file) |
||
| 37 | { |
||
| 38 | return fopen('gaufrette://' . $file->storage() . '/' . $file->key(), 'rb'); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 39 | } |
||
| 40 | |||
| 41 | protected function deleteFile(File $file): void |
||
| 42 | { |
||
| 43 | $this->filesystems |
||
| 44 | ->get($file->storage()) |
||
| 45 | ->delete($file->key()) |
||
| 46 | ; |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function writeFile(string $key, string $storage, $stream): void |
||
| 50 | { |
||
| 51 | if (!$this->filesystems->has($storage)) { |
||
| 52 | throw StorageFailure::unsupported($storage); |
||
| 53 | } |
||
| 54 | |||
| 55 | try { |
||
| 56 | $this->filesystems |
||
| 57 | ->get($storage) |
||
| 58 | ->write($key, stream_get_contents($stream)) |
||
| 59 | ; |
||
| 60 | } catch (GaufretteException | RuntimeException $e) { |
||
| 61 | throw StorageFailure::invalidWrite($key, $e); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 |