Total Complexity | 56 |
Total Lines | 442 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MediaException 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 MediaException, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
26 | #[Package('content')] |
||
27 | class MediaException extends HttpException |
||
28 | { |
||
29 | public const MEDIA_INVALID_CONTENT_LENGTH = 'CONTENT__MEDIA_INVALID_CONTENT_LENGTH'; |
||
30 | public const MEDIA_INVALID_URL = 'CONTENT__MEDIA_INVALID_URL'; |
||
31 | public const MEDIA_ILLEGAL_URL = 'CONTENT__MEDIA_ILLEGAL_URL'; |
||
32 | public const MEDIA_DISABLE_URL_UPLOAD_FEATURE = 'CONTENT__MEDIA_DISABLE_URL_UPLOAD_FEATURE'; |
||
33 | public const MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ = 'CONTENT__MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ'; |
||
34 | public const MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE = 'CONTENT__MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE'; |
||
35 | public const MEDIA_CANNOT_COPY_MEDIA = 'CONTENT__MEDIA_CANNOT_COPY_MEDIA'; |
||
36 | public const MEDIA_FILE_SIZE_LIMIT_EXCEEDED = 'CONTENT__MEDIA_FILE_SIZE_LIMIT_EXCEEDED'; |
||
37 | public const MEDIA_MISSING_FILE_EXTENSION = 'CONTENT__MEDIA_MISSING_FILE_EXTENSION'; |
||
38 | public const MEDIA_ILLEGAL_FILE_NAME = 'CONTENT__MEDIA_ILLEGAL_FILE_NAME'; |
||
39 | public const MEDIA_EMPTY_FILE = 'CONTENT__MEDIA_EMPTY_FILE'; |
||
40 | public const MEDIA_INVALID_FILE = 'CONTENT__MEDIA_INVALID_FILE'; |
||
41 | public const MEDIA_EMPTY_FILE_NAME = 'CONTENT__MEDIA_EMPTY_FILE_NAME'; |
||
42 | public const MEDIA_FOLDER_NOT_FOUND = 'CONTENT__MEDIA_FOLDER_NOT_FOUND'; |
||
43 | public const MEDIA_FOLDER_NAME_NOT_FOUND = 'CONTENT__MEDIA_FOLDER_NAME_NOT_FOUND'; |
||
44 | public const MEDIA_FILE_TYPE_NOT_SUPPORTED = 'CONTENT__MEDIA_FILE_TYPE_NOT_SUPPORTED'; |
||
45 | public const MEDIA_COULD_NOT_RENAME_FILE = 'CONTENT__MEDIA_COULD_NOT_RENAME_FILE'; |
||
46 | public const MEDIA_EMPTY_ID = 'CONTENT__MEDIA_EMPTY_ID'; |
||
47 | public const MEDIA_INVALID_BATCH_SIZE = 'CONTENT__MEDIA_INVALID_BATCH_SIZE'; |
||
48 | public const MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED = 'CONTENT__MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED'; |
||
49 | public const MEDIA_TYPE_NOT_LOADED = 'CONTENT__MEDIA_TYPE_NOT_LOADED'; |
||
50 | public const MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL = 'CONTENT__MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL'; |
||
51 | public const MEDIA_THUMBNAIL_NOT_SAVED = 'CONTENT__MEDIA_THUMBNAIL_NOT_SAVED'; |
||
52 | public const MEDIA_CANNOT_CREATE_IMAGE_HANDLE = 'CONTENT__MEDIA_CANNOT_CREATE_IMAGE_HANDLE'; |
||
53 | public const MEDIA_CONTAINS_NO_THUMBNAILS = 'CONTENT__MEDIA_CONTAINS_NO_THUMBNAILS'; |
||
54 | public const MEDIA_STRATEGY_NOT_FOUND = 'CONTENT__MEDIA_STRATEGY_NOT_FOUND'; |
||
55 | public const MEDIA_INVALID_FILE_SYSTEM_VISIBILITY = 'CONTENT__MEDIA_INVALID_FILE_SYSTEM_VISIBILITY'; |
||
56 | public const MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM = 'CONTENT__MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM'; |
||
57 | public const MEDIA_MISSING_URL_PARAMETER = 'CONTENT__MEDIA_MISSING_URL_PARAMETER'; |
||
58 | public const MEDIA_CANNOT_CREATE_TEMP_FILE = 'CONTENT__MEDIA_CANNOT_CREATE_TEMP_FILE'; |
||
59 | public const MEDIA_FILE_NOT_FOUND = 'CONTENT__MEDIA_FILE_NOT_FOUND'; |
||
60 | public const MEDIA_MISSING_FILE = 'CONTENT__MEDIA_MISSING_FILE'; |
||
61 | public const MEDIA_NOT_FOUND = 'CONTENT__MEDIA_NOT_FOUND'; |
||
62 | public const MEDIA_DUPLICATED_FILE_NAME = 'CONTENT__MEDIA_DUPLICATED_FILE_NAME'; |
||
63 | |||
64 | public static function invalidContentLength(): self |
||
65 | { |
||
66 | if (!Feature::isActive('v6.6.0.0')) { |
||
67 | return new UploadException('Expected content-length did not match actual size.'); |
||
|
|||
68 | } |
||
69 | |||
70 | return new self( |
||
71 | Response::HTTP_BAD_REQUEST, |
||
72 | self::MEDIA_INVALID_CONTENT_LENGTH, |
||
73 | 'Expected content-length did not match actual size.' |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | public static function invalidUrl(string $url): self |
||
78 | { |
||
79 | if (!Feature::isActive('v6.6.0.0')) { |
||
80 | return new UploadException(sprintf('Provided URL "%s" is invalid.', $url)); |
||
81 | } |
||
82 | |||
83 | return new self( |
||
84 | Response::HTTP_BAD_REQUEST, |
||
85 | self::MEDIA_INVALID_URL, |
||
86 | 'Provided URL "{{ url }}" is invalid.', |
||
87 | ['url' => $url] |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | public static function illegalUrl(string $url): self |
||
92 | { |
||
93 | if (!Feature::isActive('v6.6.0.0')) { |
||
94 | return new IllegalUrlException($url); |
||
95 | } |
||
96 | |||
97 | return new self( |
||
98 | Response::HTTP_BAD_REQUEST, |
||
99 | self::MEDIA_ILLEGAL_URL, |
||
100 | 'Provided URL "{{ url }}" is not allowed.', |
||
101 | ['url' => $url] |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | public static function disableUrlUploadFeature(): self |
||
106 | { |
||
107 | if (!Feature::isActive('v6.6.0.0')) { |
||
108 | return new DisabledUrlUploadFeatureException(); |
||
109 | } |
||
110 | |||
111 | return new self( |
||
112 | Response::HTTP_BAD_REQUEST, |
||
113 | self::MEDIA_DISABLE_URL_UPLOAD_FEATURE, |
||
114 | 'The feature to upload a media via URL is disabled.' |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | public static function cannotOpenSourceStreamToRead(string $url): self |
||
119 | { |
||
120 | if (!Feature::isActive('v6.6.0.0')) { |
||
121 | return new UploadException(sprintf('Cannot open source stream to read from %s.', $url)); |
||
122 | } |
||
123 | |||
124 | return new self( |
||
125 | Response::HTTP_BAD_REQUEST, |
||
126 | self::MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ, |
||
127 | 'Cannot open source stream to read from {{ url }}.', |
||
128 | ['url' => $url] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | public static function cannotOpenSourceStreamToWrite(string $fileName): self |
||
133 | { |
||
134 | if (!Feature::isActive('v6.6.0.0')) { |
||
135 | return new UploadException(sprintf('Cannot open source stream to write upload data: %s.', $fileName)); |
||
136 | } |
||
137 | |||
138 | return new self( |
||
139 | Response::HTTP_BAD_REQUEST, |
||
140 | self::MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE, |
||
141 | 'Cannot open source stream to write upload data: {{ fileName }}.', |
||
142 | ['fileName' => $fileName] |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | public static function cannotCopyMedia(): self |
||
147 | { |
||
148 | if (!Feature::isActive('v6.6.0.0')) { |
||
149 | return new UploadException('Error while copying media from source.'); |
||
150 | } |
||
151 | |||
152 | return new self( |
||
153 | Response::HTTP_CONFLICT, |
||
154 | self::MEDIA_CANNOT_COPY_MEDIA, |
||
155 | 'Error while copying media from source.' |
||
156 | ); |
||
157 | } |
||
158 | |||
159 | public static function fileSizeLimitExceeded(): self |
||
160 | { |
||
161 | if (!Feature::isActive('v6.6.0.0')) { |
||
162 | return new UploadException('Source file exceeds maximum file size limit.'); |
||
163 | } |
||
164 | |||
165 | return new self( |
||
166 | Response::HTTP_BAD_REQUEST, |
||
167 | self::MEDIA_FILE_SIZE_LIMIT_EXCEEDED, |
||
168 | 'Source file exceeds maximum file size limit.' |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | public static function missingFileExtension(): self |
||
173 | { |
||
174 | if (!Feature::isActive('v6.6.0.0')) { |
||
175 | return new MissingFileExtensionException(); |
||
176 | } |
||
177 | |||
178 | return new self( |
||
179 | Response::HTTP_BAD_REQUEST, |
||
180 | self::MEDIA_MISSING_FILE_EXTENSION, |
||
181 | 'No file extension provided. Please use the "extension" query parameter to specify the extension of the uploaded file.' |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | public static function illegalFileName(string $filename, string $cause): self |
||
186 | { |
||
187 | if (!Feature::isActive('v6.6.0.0')) { |
||
188 | return new IllegalFileNameException($filename, $cause); |
||
189 | } |
||
190 | |||
191 | return new self( |
||
192 | Response::HTTP_BAD_REQUEST, |
||
193 | self::MEDIA_ILLEGAL_FILE_NAME, |
||
194 | 'Provided filename "{{ fileName }}" is not permitted: {{ cause }}', |
||
195 | ['fileName' => $filename, 'cause' => $cause] |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | public static function mediaNotFound(string $mediaId): self |
||
200 | { |
||
201 | if (!Feature::isActive('v6.6.0.0')) { |
||
202 | return new MediaNotFoundException($mediaId); |
||
203 | } |
||
204 | |||
205 | return new self( |
||
206 | Response::HTTP_NOT_FOUND, |
||
207 | self::MEDIA_NOT_FOUND, |
||
208 | 'Media for id {{ mediaId }} not found.', |
||
209 | ['mediaId' => $mediaId] |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | public static function invalidFile(string $cause): self |
||
214 | { |
||
215 | if (!Feature::isActive('v6.6.0.0')) { |
||
216 | return new UploadException(sprintf('Provided file is invalid: %s.', $cause)); |
||
217 | } |
||
218 | |||
219 | return new self( |
||
220 | Response::HTTP_BAD_REQUEST, |
||
221 | self::MEDIA_INVALID_FILE, |
||
222 | 'Provided file is invalid: {{ cause }}.', |
||
223 | ['cause' => $cause] |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | public static function emptyMediaFilename(): self |
||
228 | { |
||
229 | if (!Feature::isActive('v6.6.0.0')) { |
||
230 | return new EmptyMediaFilenameException(); |
||
231 | } |
||
232 | |||
233 | return new self( |
||
234 | Response::HTTP_BAD_REQUEST, |
||
235 | self::MEDIA_EMPTY_FILE_NAME, |
||
236 | 'A valid filename must be provided.' |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | public static function duplicatedMediaFileName(string $fileName, string $fileExtension): self |
||
241 | { |
||
242 | if (!Feature::isActive('v6.6.0.0')) { |
||
243 | return new DuplicatedMediaFileNameException($fileName, $fileExtension); |
||
244 | } |
||
245 | |||
246 | return new self( |
||
247 | Response::HTTP_CONFLICT, |
||
248 | self::MEDIA_DUPLICATED_FILE_NAME, |
||
249 | 'A file with the name "{{ fileName }}.{{ fileExtension }}" already exists.', |
||
250 | ['fileName' => $fileName, 'fileExtension' => $fileExtension] |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | public static function missingFile(string $mediaId): self |
||
255 | { |
||
256 | if (!Feature::isActive('v6.6.0.0')) { |
||
257 | return new MissingFileException($mediaId); |
||
258 | } |
||
259 | |||
260 | return new self( |
||
261 | Response::HTTP_NOT_FOUND, |
||
262 | self::MEDIA_MISSING_FILE, |
||
263 | 'Could not find file for media with id: "{{ mediaId }}"', |
||
264 | ['mediaId' => $mediaId] |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | public static function mediaFolderIdNotFound(string $folderId): self |
||
269 | { |
||
270 | if (!Feature::isActive('v6.6.0.0')) { |
||
271 | return new MediaFolderNotFoundException($folderId); |
||
272 | } |
||
273 | |||
274 | return new self( |
||
275 | Response::HTTP_NOT_FOUND, |
||
276 | self::MEDIA_FOLDER_NOT_FOUND, |
||
277 | 'Could not find media folder with id: "{{ folderId }}"', |
||
278 | ['folderId' => $folderId] |
||
279 | ); |
||
280 | } |
||
281 | |||
282 | public static function mediaFolderNameNotFound(string $folderName): self |
||
283 | { |
||
284 | return new self( |
||
285 | Response::HTTP_NOT_FOUND, |
||
286 | self::MEDIA_FOLDER_NAME_NOT_FOUND, |
||
287 | 'Could not find a folder with the name: "{{ folderName }}"', |
||
288 | ['folderName' => $folderName] |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | public static function fileExtensionNotSupported(string $mediaId, string $extension): self |
||
293 | { |
||
294 | if (!Feature::isActive('v6.6.0.0')) { |
||
295 | return new FileExtensionNotSupportedException($mediaId, $extension); |
||
296 | } |
||
297 | |||
298 | return new self( |
||
299 | Response::HTTP_BAD_REQUEST, |
||
300 | self::MEDIA_FILE_TYPE_NOT_SUPPORTED, |
||
301 | 'The file extension "{{ extension }}" for media object with id {{ mediaId }} is not supported.', |
||
302 | ['mediaId' => $mediaId, 'extension' => $extension] |
||
303 | ); |
||
304 | } |
||
305 | |||
306 | public static function couldNotRenameFile(string $mediaId, string $oldFileName): self |
||
307 | { |
||
308 | if (!Feature::isActive('v6.6.0.0')) { |
||
309 | return new CouldNotRenameFileException($mediaId, $oldFileName); |
||
310 | } |
||
311 | |||
312 | return new self( |
||
313 | Response::HTTP_CONFLICT, |
||
314 | self::MEDIA_COULD_NOT_RENAME_FILE, |
||
315 | 'Could not rename file for media with id: {{ mediaId }}. Rollback to filename: "{{ oldFileName }}"', |
||
316 | ['mediaId' => $mediaId, 'oldFileName' => $oldFileName] |
||
317 | ); |
||
318 | } |
||
319 | |||
320 | public static function emptyMediaId(): self |
||
321 | { |
||
322 | if (!Feature::isActive('v6.6.0.0')) { |
||
323 | return new EmptyMediaIdException(); |
||
324 | } |
||
325 | |||
326 | return new self( |
||
327 | Response::HTTP_BAD_REQUEST, |
||
328 | self::MEDIA_EMPTY_ID, |
||
329 | 'A media id must be provided.' |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | public static function invalidBatchSize(): self |
||
334 | { |
||
335 | return new self( |
||
336 | Response::HTTP_BAD_REQUEST, |
||
337 | self::MEDIA_INVALID_BATCH_SIZE, |
||
338 | 'Provided batch size is invalid.' |
||
339 | ); |
||
340 | } |
||
341 | |||
342 | public static function thumbnailAssociationNotLoaded(): self |
||
343 | { |
||
344 | return new self( |
||
345 | Response::HTTP_BAD_REQUEST, |
||
346 | self::MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED, |
||
347 | 'Thumbnail association not loaded - please pre load media thumbnails.' |
||
348 | ); |
||
349 | } |
||
350 | |||
351 | public static function mediaTypeNotLoaded(string $mediaId): self |
||
352 | { |
||
353 | return new self( |
||
354 | Response::HTTP_BAD_REQUEST, |
||
355 | self::MEDIA_TYPE_NOT_LOADED, |
||
356 | 'Media type, for id {{ mediaId }}, not loaded', |
||
357 | ['mediaId' => $mediaId] |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | public static function thumbnailNotSupported(string $mediaId): self |
||
362 | { |
||
363 | if (!Feature::isActive('v6.6.0.0')) { |
||
364 | return new ThumbnailNotSupportedException($mediaId); |
||
365 | } |
||
366 | |||
367 | return new self( |
||
368 | Response::HTTP_BAD_REQUEST, |
||
369 | self::MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL, |
||
370 | 'The file for media object with id {{ mediaId }} is not supported for creating thumbnails.', |
||
371 | ['mediaId' => $mediaId] |
||
372 | ); |
||
373 | } |
||
374 | |||
375 | public static function thumbnailCouldNotBeSaved(string $url): self |
||
376 | { |
||
377 | if (!Feature::isActive('v6.6.0.0')) { |
||
378 | return new ThumbnailCouldNotBeSavedException($url); |
||
379 | } |
||
380 | |||
381 | return new self( |
||
382 | Response::HTTP_CONFLICT, |
||
383 | self::MEDIA_THUMBNAIL_NOT_SAVED, |
||
384 | 'Thumbnail could not be saved to location: {{ location }}.', |
||
385 | ['location' => $url] |
||
386 | ); |
||
387 | } |
||
388 | |||
389 | public static function cannotCreateImage(): self |
||
390 | { |
||
391 | return new self( |
||
392 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
393 | self::MEDIA_CANNOT_CREATE_IMAGE_HANDLE, |
||
394 | 'Can not create image handle.' |
||
395 | ); |
||
396 | } |
||
397 | |||
398 | public static function mediaContainsNoThumbnails(): self |
||
399 | { |
||
400 | return new self( |
||
401 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
402 | self::MEDIA_CONTAINS_NO_THUMBNAILS, |
||
403 | 'Media contains no thumbnails.' |
||
404 | ); |
||
405 | } |
||
406 | |||
407 | public static function strategyNotFound(string $strategyName): self |
||
418 | ); |
||
419 | } |
||
420 | |||
421 | public static function invalidFilesystemVisibility(): self |
||
422 | { |
||
423 | return new self( |
||
424 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
425 | self::MEDIA_INVALID_FILE_SYSTEM_VISIBILITY, |
||
426 | 'Invalid filesystem visibility.' |
||
427 | ); |
||
428 | } |
||
429 | |||
430 | public static function fileIsNotInstanceOfFileSystem(): self |
||
431 | { |
||
432 | return new self( |
||
433 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
434 | self::MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM, |
||
435 | 'File is not an instance of FileSystem' |
||
436 | ); |
||
437 | } |
||
438 | |||
439 | public static function missingUrlParameter(): self |
||
440 | { |
||
441 | if (!Feature::isActive('v6.6.0.0')) { |
||
442 | return new UploadException('Parameter url is missing.'); |
||
443 | } |
||
444 | |||
445 | return new self( |
||
446 | Response::HTTP_BAD_REQUEST, |
||
447 | self::MEDIA_MISSING_URL_PARAMETER, |
||
448 | 'Parameter url is missing.' |
||
449 | ); |
||
450 | } |
||
451 | |||
452 | public static function cannotCreateTempFile(): self |
||
458 | ); |
||
459 | } |
||
460 | |||
461 | public static function fileNotFound(string $path): self |
||
462 | { |
||
463 | return new self( |
||
464 | Response::HTTP_NOT_FOUND, |
||
465 | self::MEDIA_FILE_NOT_FOUND, |
||
466 | 'The file "{{ path }}" does not exist', |
||
467 | ['path' => $path] |
||
468 | ); |
||
469 | } |
||
470 | } |
||
471 |