Total Complexity | 43 |
Total Lines | 330 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like MediaFileService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MediaFileService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
63 | class MediaFileService |
||
64 | { |
||
65 | public const EDIT_RESTRICTIONS = [ |
||
66 | 'locked', |
||
67 | ]; |
||
68 | |||
69 | public const PRIVACY_RESTRICTIONS = [ |
||
70 | 'none', |
||
71 | 'privacy', |
||
72 | 'confidential', |
||
73 | ]; |
||
74 | |||
75 | public const EXTENSION_TO_FORM = [ |
||
76 | 'jpg' => 'jpeg', |
||
77 | 'tif' => 'tiff', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * What is the largest file a user may upload? |
||
82 | */ |
||
83 | public function maxUploadFilesize(): string |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns the given size from an ini value in bytes. |
||
96 | * |
||
97 | * @param string $size |
||
98 | * |
||
99 | * @return int |
||
100 | */ |
||
101 | private function parseIniFileSize(string $size): int |
||
102 | { |
||
103 | $number = (int) $size; |
||
104 | |||
105 | switch (substr($size, -1)) { |
||
106 | case 'g': |
||
107 | case 'G': |
||
108 | return $number * 1073741824; |
||
109 | case 'm': |
||
110 | case 'M': |
||
111 | return $number * 1048576; |
||
112 | case 'k': |
||
113 | case 'K': |
||
114 | return $number * 1024; |
||
115 | default: |
||
116 | return $number; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * A list of media files not already linked to a media object. |
||
122 | * |
||
123 | * @param Tree $tree |
||
124 | * @param FilesystemOperator $data_filesystem |
||
125 | * |
||
126 | * @return array<string> |
||
127 | * @throws FilesystemException |
||
128 | */ |
||
129 | public function unusedFiles(Tree $tree, FilesystemOperator $data_filesystem): array |
||
130 | { |
||
131 | $used_files = DB::table('media_file') |
||
132 | ->where('m_file', '=', $tree->id()) |
||
133 | ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') |
||
134 | ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') |
||
135 | ->pluck('multimedia_file_refn') |
||
136 | ->all(); |
||
137 | |||
138 | $media_filesystem = $disk_files = $tree->mediaFilesystem($data_filesystem); |
||
|
|||
139 | $disk_files = $this->allFilesOnDisk($media_filesystem, '', Filesystem::LIST_DEEP)->all(); |
||
140 | $unused_files = array_diff($disk_files, $used_files); |
||
141 | |||
142 | sort($unused_files); |
||
143 | |||
144 | return array_combine($unused_files, $unused_files); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Store an uploaded file (or URL), either to be added to a media object |
||
149 | * or to create a media object. |
||
150 | * |
||
151 | * @param ServerRequestInterface $request |
||
152 | * |
||
153 | * @return string The value to be stored in the 'FILE' field of the media object. |
||
154 | * @throws FilesystemException |
||
155 | */ |
||
156 | public function uploadFile(ServerRequestInterface $request): string |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Convert the media file attributes into GEDCOM format. |
||
233 | * |
||
234 | * @param string $file |
||
235 | * @param string $type |
||
236 | * @param string $title |
||
237 | * @param string $note |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | public function createMediaFileGedcom(string $file, string $type, string $title, string $note): string |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Fetch a list of all files on disk (in folders used by any tree). |
||
276 | * |
||
277 | * @param FilesystemOperator $filesystem $filesystem to search |
||
278 | * @param string $folder Root folder |
||
279 | * @param bool $subfolders Include subfolders |
||
280 | * |
||
281 | * @return Collection<string> |
||
282 | */ |
||
283 | public function allFilesOnDisk(FilesystemOperator $filesystem, string $folder, bool $subfolders): Collection |
||
284 | { |
||
285 | try { |
||
286 | $files = $filesystem->listContents($folder, $subfolders) |
||
287 | ->filter(function (StorageAttributes $attributes): bool { |
||
288 | return $attributes->isFile() && !$this->isLegacyFolder($attributes->path()); |
||
289 | }) |
||
290 | ->map(static function (StorageAttributes $attributes): string { |
||
291 | return $attributes->path(); |
||
292 | }) |
||
293 | ->toArray(); |
||
294 | } catch (FilesystemException $ex) { |
||
295 | $files = []; |
||
296 | } |
||
297 | |||
298 | return new Collection($files); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Fetch a list of all files on in the database. |
||
303 | * |
||
304 | * @param string $media_folder Root folder |
||
305 | * @param bool $subfolders Include subfolders |
||
306 | * |
||
307 | * @return Collection<string> |
||
308 | */ |
||
309 | public function allFilesInDatabase(string $media_folder, bool $subfolders): Collection |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Generate a list of all folders in either the database or the filesystem. |
||
330 | * |
||
331 | * @param FilesystemOperator $data_filesystem |
||
332 | * |
||
333 | * @return Collection<string,string> |
||
334 | * @throws FilesystemException |
||
335 | */ |
||
336 | public function allMediaFolders(FilesystemOperator $data_filesystem): Collection |
||
337 | { |
||
338 | $db_folders = DB::table('media_file') |
||
339 | ->join('gedcom_setting', 'gedcom_id', '=', 'm_file') |
||
340 | ->where('setting_name', '=', 'MEDIA_DIRECTORY') |
||
341 | ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') |
||
342 | ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') |
||
343 | ->select(new Expression('setting_value || multimedia_file_refn AS path')) |
||
344 | ->pluck('path') |
||
345 | ->map(static function (string $path): string { |
||
346 | return dirname($path) . '/'; |
||
347 | }); |
||
348 | |||
349 | $media_roots = DB::table('gedcom_setting') |
||
350 | ->where('setting_name', '=', 'MEDIA_DIRECTORY') |
||
351 | ->where('gedcom_id', '>', '0') |
||
352 | ->pluck('setting_value') |
||
353 | ->uniqueStrict(); |
||
354 | |||
355 | $disk_folders = new Collection($media_roots); |
||
356 | |||
357 | foreach ($media_roots as $media_folder) { |
||
358 | $tmp = $data_filesystem->listContents($media_folder, Filesystem::LIST_DEEP) |
||
359 | ->filter(function (StorageAttributes $attributes): bool { |
||
360 | return $attributes->isDir() && !$this->isLegacyFolder($attributes->path()); |
||
361 | }) |
||
362 | ->map(static function (StorageAttributes $attributes): string { |
||
363 | return $attributes->path() . '/'; |
||
364 | }) |
||
365 | ->toArray(); |
||
366 | |||
367 | $disk_folders = $disk_folders->concat($tmp); |
||
368 | } |
||
369 | |||
370 | return $disk_folders->concat($db_folders) |
||
371 | ->uniqueStrict() |
||
372 | ->mapWithKeys(static function (string $folder): array { |
||
373 | return [$folder => $folder]; |
||
374 | }); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Some special media folders were created by earlier versions of webtrees. |
||
379 | * |
||
380 | * @param string $path |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | private function isLegacyFolder(string $path): bool |
||
393 | } |
||
394 | } |
||
395 |