FlysystemStorage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeFile() 0 14 4
A readFile() 0 5 1
A streamFile() 0 5 1
A deleteFile() 0 5 1
A __construct() 0 5 1
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
The expression return $this->registry->...())->read($file->key()) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
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
Bug Best Practice introduced by
The expression return $this->registry->...eadStream($file->key()) also could return the type false which is incompatible with the return type mandated by Damax\Media\Domain\Stora...ctStorage::streamFile() of resource.
Loading history...
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