Completed
Branch ci (88e3bf)
by Florian
04:11
created

FileStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 56.41%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 130
ccs 22
cts 39
cp 0.5641
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A runCallbacks() 0 14 3
A store() 0 11 1
A remove() 0 15 3
A removeVariant() 0 23 3
A getStorage() 0 3 1
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage;
18
19
use InvalidArgumentException;
20
use League\Flysystem\AdapterInterface;
21
use League\Flysystem\Config;
22
use Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilder;
23
use Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface;
24
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantException;
25
26
/**
27
 * File Storage Service
28
 */
29
class FileStorage implements FileStorageInterface
30
{
31
    /**
32
     * @var array
33
     */
34
    protected array $callbacks = [
35
        'beforeSave' => [],
36
        'afterSave' => [],
37
        'beforeRemove' => [],
38
        'afterRemove' => [],
39
    ];
40
41
    /**
42
     * @var \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface
43
     */
44
    protected PathBuilderInterface $pathBuilder;
45
46
    /**
47
     * @var \Phauthentic\Infrastructure\Storage\StorageServiceInterface
48
     */
49
    protected StorageServiceInterface $storageService;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param \Phauthentic\Infrastructure\Storage\StorageServiceInterface $storageService Storage Service
55
     * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface $pathBuilder Path Builder
56
     */
57 1
    public function __construct(
58
        StorageServiceInterface $storageService,
59
        ?PathBuilderInterface $pathBuilder = null
60
    ) {
61 1
        $this->pathBuilder = $pathBuilder ?? new PathBuilder();
62 1
        $this->storageService = $storageService;
63 1
    }
64
65
    /**
66
     * @param string $type Type
67
     * @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
68
     * @return \Phauthentic\Infrastructure\Storage\FileInterface
69
     */
70 1
    public function runCallbacks(string $type, FileInterface $file): FileInterface
71
    {
72 1
        if (!isset($this->callbacks[$type])) {
73
            throw new InvalidArgumentException(sprintf(
74
                'Type %s is invalid',
75
                $type
76
            ));
77
        }
78
79 1
        foreach ($this->callbacks[$type] as $callback) {
80
            $file = $callback($file);
81
        }
82
83 1
        return $file;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 1
    public function store(FileInterface $file): FileInterface
90
    {
91 1
        $config = new Config();
92
93 1
        $file = $file->buildPath($this->pathBuilder);
94 1
        $file = $this->runCallbacks('beforeSave', $file);
95
96 1
        $storage = $this->getStorage($file->storage());
97 1
        $storage->writeStream($file->path(), $file->resource(), $config);
98
99 1
        return $this->runCallbacks('afterSave', $file);
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 1
    public function remove(FileInterface $file): FileInterface
106
    {
107 1
        $file = $this->runCallbacks('beforeRemove', $file);
108
109
        // Delete all variants of the file
110 1
        foreach ($file->variants() as $variant) {
111
            if (!empty($variant['path'])) {
112
                $this->getStorage($file->storage())->delete($variant['path']);
113
            }
114
        }
115
116
        // Delete the file
117 1
        $this->getStorage($file->storage())->delete($file->path());
118
119 1
        return $this->runCallbacks('afterRemove', $file);
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function removeVariant(FileInterface $file, string $name): FileInterface
126
    {
127
        if (!$file->hasVariant($name)) {
128
            throw new VariantException(sprintf(
129
                'Variant `%s` does not exist',
130
                $name
131
            ));
132
        }
133
134
        $variant = $file->variant($name);
135
        if (empty($variant['path'])) {
136
            throw new VariantException(sprintf(
137
                'Variant `%s` is missing a path',
138
                $name
139
            ));
140
        }
141
142
        $this->getStorage($file->storage())->delete($variant['path']);
143
144
        $variants = $file->variants();
145
        unset($variants[$name]);
146
147
        return $file->withVariants($variants, false);
0 ignored issues
show
Bug introduced by
It seems like $variants can also be of type null; however, parameter $variants of Phauthentic\Infrastructu...terface::withVariants() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        return $file->withVariants(/** @scrutinizer ignore-type */ $variants, false);
Loading history...
148
    }
149
150
    /**
151
     * Gets the storage abstraction to use
152
     *
153
     * @param string $storage Storage name to use
154
     * @return \League\Flysystem\AdapterInterface
155
     */
156 1
    public function getStorage(string $storage): AdapterInterface
157
    {
158 1
        return $this->storageService->adapter($storage);
159
    }
160
}
161