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); |
|
|
|
|
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
|
|
|
|