Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 19 |
CRAP | |
0.00% |
0 / 1 |
FileSystemWrapperTrait | |
0.00% |
0 / 63 |
|
0.00% |
0 / 19 |
420 | |
0.00% |
0 / 1 |
has | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
directoryExists | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
read | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
readStream | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
listContents | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
write | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
writeStream | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
copy | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
delete | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
setVisibility | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
fileExists | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
lastModified | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
fileSize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
mimeType | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
visibility | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
deleteDirectory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
createDirectory | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
move | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
wrapFileSystemOperation | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getFileSystem | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getFullPath | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | namespace oat\oatbox\filesystem\utils; |
23 | |
24 | use League\Flysystem\DirectoryListing; |
25 | use League\Flysystem\FilesystemException as FlyFilesystemException; |
26 | use League\Flysystem\FilesystemOperator; |
27 | use oat\oatbox\filesystem\FilesystemException; |
28 | |
29 | /** |
30 | * A trait to facilitate creation of filesystem wrappers |
31 | * |
32 | * @author Joel Bout |
33 | */ |
34 | trait FileSystemWrapperTrait |
35 | { |
36 | /** |
37 | * @see FilesystemOperator::has |
38 | * @throws FilesystemException |
39 | */ |
40 | public function has(string $location): bool |
41 | { |
42 | return $this->wrapFileSystemOperation(function () use ($location) { |
43 | return $this->getFileSystem()->has($this->getFullPath($location)); |
44 | }); |
45 | } |
46 | |
47 | /** |
48 | * @see FilesystemOperator::directoryExists |
49 | * @throws FilesystemException |
50 | */ |
51 | public function directoryExists(string $location): bool |
52 | { |
53 | return $this->wrapFileSystemOperation(function () use ($location) { |
54 | return $this->getFileSystem()->directoryExists($this->getFullPath($location)); |
55 | }); |
56 | } |
57 | |
58 | /** |
59 | * @see FilesystemOperator::read |
60 | * @throws FilesystemException |
61 | */ |
62 | public function read(string $location): string |
63 | { |
64 | return $this->wrapFileSystemOperation(function () use ($location) { |
65 | return $this->getFileSystem()->read($this->getFullPath($location)); |
66 | }); |
67 | } |
68 | |
69 | /** |
70 | * @see FilesystemOperator::readStream |
71 | * @throws FilesystemException |
72 | */ |
73 | public function readStream($path) |
74 | { |
75 | return $this->wrapFileSystemOperation(function () use ($path) { |
76 | return $this->getFileSystem()->readStream($this->getFullPath($path)); |
77 | }); |
78 | } |
79 | |
80 | /** |
81 | * @see FilesystemOperator::listContents |
82 | * @throws FilesystemException |
83 | */ |
84 | public function listContents(string $location = '', bool $deep = self::LIST_SHALLOW): DirectoryListing |
85 | { |
86 | return $this->wrapFileSystemOperation(function () use ($location, $deep) { |
87 | return $this->getFileSystem()->listContents($this->getFullPath($location), $deep); |
88 | }); |
89 | } |
90 | |
91 | /** |
92 | * @see FilesystemOperator::write |
93 | * @throws FilesystemException |
94 | */ |
95 | public function write(string $location, string $contents, array $config = []): void |
96 | { |
97 | $this->wrapFileSystemOperation(function () use ($location, $contents, $config) { |
98 | $this->getFileSystem()->write($this->getFullPath($location), $contents, $config); |
99 | }); |
100 | } |
101 | |
102 | /** |
103 | * @see FilesystemOperator::writeStream |
104 | * @throws FilesystemException |
105 | */ |
106 | public function writeStream(string $location, $contents, array $config = []): void |
107 | { |
108 | $this->wrapFileSystemOperation(function () use ($location, $contents, $config) { |
109 | $this->getFileSystem()->writeStream($this->getFullPath($location), $contents, $config); |
110 | }); |
111 | } |
112 | |
113 | /** |
114 | * @see FilesystemOperator::copy |
115 | * @throws FilesystemException |
116 | */ |
117 | public function copy(string $source, string $destination, array $config = []): void |
118 | { |
119 | $this->wrapFileSystemOperation(function () use ($source, $destination, $config) { |
120 | $this->getFileSystem()->copy($this->getFullPath($source), $destination, $config); |
121 | }); |
122 | } |
123 | |
124 | /** |
125 | * @see FilesystemOperator::delete |
126 | * @throws FilesystemException |
127 | */ |
128 | public function delete(string $location): void |
129 | { |
130 | $this->wrapFileSystemOperation(function () use ($location) { |
131 | $this->getFileSystem()->delete($this->getFullPath($location)); |
132 | }); |
133 | } |
134 | |
135 | /** |
136 | * @see FilesystemOperator::setVisibility |
137 | * @throws FilesystemException |
138 | */ |
139 | public function setVisibility(string $path, string $visibility): void |
140 | { |
141 | $this->wrapFileSystemOperation(function () use ($path, $visibility) { |
142 | $this->getFileSystem()->setVisibility($this->getFullPath($path), $visibility); |
143 | }); |
144 | } |
145 | |
146 | /** |
147 | * @see FilesystemOperator::fileExists |
148 | * @throws FilesystemException |
149 | */ |
150 | public function fileExists(string $location): bool |
151 | { |
152 | return $this->wrapFileSystemOperation(function () use ($location) { |
153 | return $this->getFileSystem()->fileExists($this->getFullPath($location)); |
154 | }); |
155 | } |
156 | |
157 | /** |
158 | * @see FilesystemOperator::lastModified |
159 | * @throws FilesystemException |
160 | */ |
161 | public function lastModified(string $path): int |
162 | { |
163 | return $this->wrapFileSystemOperation(function () use ($path) { |
164 | return $this->getFileSystem()->lastModified($this->getFullPath($path)); |
165 | }); |
166 | } |
167 | |
168 | /** |
169 | * @see FilesystemOperator::fileSize |
170 | * @throws FilesystemException |
171 | */ |
172 | public function fileSize(string $path): int |
173 | { |
174 | return $this->wrapFileSystemOperation(function () use ($path) { |
175 | return $this->getFileSystem()->fileSize($this->getFullPath($path)); |
176 | }); |
177 | } |
178 | |
179 | /** |
180 | * @see FilesystemOperator::mimeType |
181 | * @throws FilesystemException |
182 | */ |
183 | public function mimeType(string $path): string |
184 | { |
185 | return $this->wrapFileSystemOperation(function () use ($path) { |
186 | return $this->getFileSystem()->mimeType($this->getFullPath($path)); |
187 | }); |
188 | } |
189 | |
190 | /** |
191 | * @see FilesystemOperator::visibility |
192 | * @throws FilesystemException |
193 | */ |
194 | public function visibility(string $path): string |
195 | { |
196 | return $this->wrapFileSystemOperation(function () use ($path) { |
197 | return $this->getFileSystem()->visibility($this->getFullPath($path)); |
198 | }); |
199 | } |
200 | |
201 | /** |
202 | * @see FilesystemOperator::deleteDirectory |
203 | * @throws FilesystemException |
204 | */ |
205 | public function deleteDirectory(string $location): void |
206 | { |
207 | $this->wrapFileSystemOperation(function () use ($location) { |
208 | $this->getFileSystem()->deleteDirectory($this->getFullPath($location)); |
209 | }); |
210 | } |
211 | |
212 | /** |
213 | * @see FilesystemOperator::createDirectory |
214 | * @throws FilesystemException |
215 | */ |
216 | public function createDirectory(string $location, array $config = []): void |
217 | { |
218 | $this->wrapFileSystemOperation(function () use ($location, $config) { |
219 | $this->getFileSystem()->createDirectory($this->getFullPath($location), $config); |
220 | }); |
221 | } |
222 | |
223 | /** |
224 | * @see FilesystemOperator::move |
225 | * @throws FilesystemException |
226 | */ |
227 | public function move(string $source, string $destination, array $config = []): void |
228 | { |
229 | $this->wrapFileSystemOperation(function () use ($source, $destination, $config) { |
230 | $this->getFileSystem()->move($this->getFullPath($source), $destination, $config); |
231 | }); |
232 | } |
233 | |
234 | private function wrapFileSystemOperation(callable $operation) |
235 | { |
236 | try { |
237 | return $operation(); |
238 | } catch (FlyFilesystemException $e) { |
239 | throw new FilesystemException($e->getMessage(), $e->getCode(), $e); |
240 | } |
241 | } |
242 | |
243 | abstract protected function getFileSystem(): FilesystemOperator; |
244 | |
245 | abstract protected function getFullPath($path); |
246 | } |