Total Complexity | 94 |
Total Lines | 527 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AjaxController 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 AjaxController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class AjaxController |
||
50 | { |
||
51 | /** |
||
52 | * @return JsonResponse |
||
53 | */ |
||
54 | public function getNewStorageUrlAction(): JsonResponse |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return JsonResponse |
||
74 | */ |
||
75 | public function damGetLogoutUrlAction(): JsonResponse |
||
76 | { |
||
77 | $backendUser = $this->getBackendUser(); |
||
78 | if (!$backendUser) { |
||
|
|||
79 | return new JsonExceptionResponse(new ControllerException('User is not logged in', 1554380677)); |
||
80 | } |
||
81 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
82 | return new JsonResponse([ (string)$uriBuilder->buildUriFromRoute('logout') ]); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Set module state of BE user. Send a json array as ['data'] POST |
||
87 | * |
||
88 | * @param ServerRequestInterface $request |
||
89 | * @return JsonResponse |
||
90 | */ |
||
91 | public function setStateAction(ServerRequestInterface $request): JsonResponse |
||
92 | { |
||
93 | $backendUser = $this->getBackendUser(); |
||
94 | $backendUser->uc['digital_asset_management'] = $request->getParsedBody()['data'] ?? []; |
||
95 | $backendUser->writeUC(); |
||
96 | return new JsonResponse(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return JsonResponse |
||
101 | */ |
||
102 | public function getStateAction(): JsonResponse |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param ServerRequestInterface $request |
||
109 | * |
||
110 | * @return JsonResponse |
||
111 | */ |
||
112 | public function createFolderAction(ServerRequestInterface $request): JsonResponse |
||
113 | { |
||
114 | $identifier = $request->getQueryParams()['identifier'] ?? ''; |
||
115 | if (empty($identifier)) { |
||
116 | return new JsonExceptionResponse(new ControllerException('Identifier needed', 1554204780)); |
||
117 | } |
||
118 | try { |
||
119 | $folder = $this->createFolderRecursive($identifier); |
||
120 | return new JsonResponse([new FolderItemFolder($folder)]); |
||
121 | } catch (ResourceException $e) { |
||
122 | return new JsonExceptionResponse($e); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param ServerRequestInterface $request |
||
128 | * |
||
129 | * @return JsonResponse |
||
130 | */ |
||
131 | public function fileUploadAction(ServerRequestInterface $request): JsonResponse |
||
132 | { |
||
133 | $identifier = $request->getQueryParams()['identifier'] ?? ''; |
||
134 | $conflictMode = $request->getQueryParams()['conflictMode'] ?? ''; |
||
135 | $tempFilename = ''; |
||
136 | try { |
||
137 | if (empty($identifier)) { |
||
138 | throw new ControllerException('Identifier needed', 1554132801); |
||
139 | } |
||
140 | if (empty($conflictMode) || !in_array($conflictMode, ['replace', 'cancel', 'rename'], true)) { |
||
141 | throw new ControllerException('conflictMode must be one of "replace", "cancel", "rename"'); |
||
142 | } |
||
143 | $folderIdentifier = dirname($identifier) . '/'; |
||
144 | $fileIdentifier = basename($identifier); |
||
145 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
146 | try { |
||
147 | $folder = $resourceFactory->retrieveFileOrFolderObject($folderIdentifier); |
||
148 | } catch (ResourceDoesNotExistException $e) { |
||
149 | $folder = $this->createFolderRecursive($folderIdentifier); |
||
150 | } |
||
151 | $tempFilename = tempnam(sys_get_temp_dir(), 'upload_'); |
||
152 | file_put_contents($tempFilename, $request->getBody()); |
||
153 | $file = $folder->addFile($tempFilename, $fileIdentifier, (string)DuplicationBehavior::cast($conflictMode)); |
||
154 | $fileExtension = strtolower($file->getExtension()); |
||
155 | if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension) |
||
156 | || GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['mediafile_ext'], $fileExtension) |
||
157 | ) { |
||
158 | return new JsonResponse([new FolderItemImage($file)]); |
||
159 | } |
||
160 | return new JsonResponse([new FolderItemFile($file)]); |
||
161 | } catch (ResourceException $e) { |
||
162 | if (!empty($tempFilename) && file_exists($tempFilename)) { |
||
163 | unlink($tempFilename); |
||
164 | } |
||
165 | return new JsonExceptionResponse($e); |
||
166 | } catch (ControllerException $e) { |
||
167 | return new JsonExceptionResponse($e); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $folderIdentifier |
||
173 | * |
||
174 | * @return Folder |
||
175 | */ |
||
176 | protected function createFolderRecursive(string $folderIdentifier): Folder |
||
177 | { |
||
178 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
179 | $stack = []; |
||
180 | while (true) { |
||
181 | $parentName = dirname($folderIdentifier); |
||
182 | $folderName = basename($folderIdentifier); |
||
183 | $stack[] = $folderName; |
||
184 | try { |
||
185 | $parentObject = $resourceFactory->retrieveFileOrFolderObject($parentName); |
||
186 | break; |
||
187 | } catch (ResourceDoesNotExistException $e) { |
||
188 | $folderIdentifier = $parentName; |
||
189 | } |
||
190 | } |
||
191 | while ($folderName = array_pop($stack)) { |
||
192 | try { |
||
193 | $parentObject = $parentObject->createFolder($folderName); |
||
194 | } catch (ResourceException $e) { |
||
195 | } |
||
196 | } |
||
197 | return $parentObject; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param ServerRequestInterface $request |
||
202 | * |
||
203 | * @return JsonResponse |
||
204 | */ |
||
205 | public function fileExistsAction(ServerRequestInterface $request): JsonResponse |
||
206 | { |
||
207 | $identifier = $request->getQueryParams()['identifier']; |
||
208 | if (empty($identifier)) { |
||
209 | return new JsonExceptionResponse(new ControllerException('Identifier needed', 1554125449)); |
||
210 | } |
||
211 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
212 | $folderIdentifier = dirname($identifier) . '/'; |
||
213 | $fileIdentifier = basename($identifier); |
||
214 | try { |
||
215 | $folder = $resourceFactory->retrieveFileOrFolderObject($folderIdentifier); |
||
216 | } catch (ResourceDoesNotExistException $e) { |
||
217 | return new FileExistsResponse(FileExistsResponse::PARENT_FOLDER_DOES_NOT_EXIST); |
||
218 | } |
||
219 | $fileName = $folder->getStorage()->sanitizeFileName($fileIdentifier, $folder); |
||
220 | if ($folder->hasFile($fileName)) { |
||
221 | $file = $resourceFactory->getFileObjectFromCombinedIdentifier($folderIdentifier . $fileName); |
||
222 | // If file is an image or media, create image object, else file object |
||
223 | $fileExtension = strtolower($file->getExtension()); |
||
224 | if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension) |
||
225 | || GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['mediafile_ext'], $fileExtension) |
||
226 | ) { |
||
227 | return new JsonResponse([new FolderItemImage($file)]); |
||
228 | } |
||
229 | return new JsonResponse([new FolderItemFile($file)]); |
||
230 | } else { |
||
231 | return new FileExistsResponse(FileExistsResponse::FILE_DOES_NOT_EXIST); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Return item list (folders, files, images) of a storage:path |
||
237 | * FAL folder identifier. GET request with identifier argument. |
||
238 | * |
||
239 | * @param ServerRequestInterface $request |
||
240 | * |
||
241 | * @return JsonResponse |
||
242 | */ |
||
243 | public function getFolderItemsAction(ServerRequestInterface $request): JsonResponse |
||
244 | { |
||
245 | try { |
||
246 | $identifier = $request->getQueryParams()['identifier'] ?? ''; |
||
247 | if (empty($identifier)) { |
||
248 | throw new ControllerException('Identifier needed', 1553699828); |
||
249 | } |
||
250 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
251 | $folderObject = $resourceFactory->getObjectFromCombinedIdentifier($identifier); |
||
252 | if (!$folderObject instanceof Folder) { |
||
253 | throw new ControllerException('Identifier is not a folder', 1553701684); |
||
254 | } |
||
255 | $subFolders = $folderObject->getSubfolders(); |
||
256 | $folders = []; |
||
257 | foreach ($subFolders as $subFolder) { |
||
258 | $folders[] = new FolderItemFolder($subFolder); |
||
259 | } |
||
260 | $allFiles = $folderObject->getFiles(); |
||
261 | $files = []; |
||
262 | $images = []; |
||
263 | foreach ($allFiles as $file) { |
||
264 | // If file is an image or media, create image object, else file object |
||
265 | $fileExtension = strtolower($file->getExtension()); |
||
266 | if (GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $fileExtension) |
||
267 | || GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['mediafile_ext'], $fileExtension) |
||
268 | ) { |
||
269 | $images[] = new FolderItemImage($file); |
||
270 | } else { |
||
271 | $files[] = new FolderItemFile($file); |
||
272 | } |
||
273 | } |
||
274 | return new FolderItemsResponse($folders, $files, $images); |
||
275 | } catch (ResourceException $e) { |
||
276 | return new JsonExceptionResponse($e); |
||
277 | } catch (ControllerException $e) { |
||
278 | return new JsonExceptionResponse($e); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Returns list of storages (admins), or file mounts (non-admin). Admins |
||
284 | * do NOT receive a list of file mounts, just the storages. |
||
285 | * |
||
286 | * Storages are returned in no particular order, file mounts are ordered |
||
287 | * by 'sorting' DB field. |
||
288 | * |
||
289 | * Return structure is an array of Storage or FileMount objects. |
||
290 | * |
||
291 | * @return JsonResponse |
||
292 | */ |
||
293 | public function getStoragesAndMountsAction(): JsonResponse |
||
294 | { |
||
295 | $backendUser = $this->getBackendUser(); |
||
296 | $storages = $backendUser->getFileStorages(); |
||
297 | $entities = []; |
||
298 | if ($backendUser->isAdmin()) { |
||
299 | foreach ($storages as $storage) { |
||
300 | $entities[] = new Storage($storage); |
||
301 | } |
||
302 | } else { |
||
303 | foreach ($storages as $storage) { |
||
304 | $fileMounts = $storage->getFileMounts(); |
||
305 | foreach ($fileMounts as $fileMount) { |
||
306 | $entities[] = new FileMount($storage, $fileMount); |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | return new StoragesAndMountsResponse($entities); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Returns list of folders only. No files, no images |
||
315 | * Result is sorted by name |
||
316 | * |
||
317 | * Return structure is an array of TreeItemFolder objects. |
||
318 | * |
||
319 | * @param ServerRequestInterface $request |
||
320 | * |
||
321 | * @return JsonResponse |
||
322 | */ |
||
323 | public function getTreeFoldersAction(ServerRequestInterface $request): JsonResponse |
||
345 | } |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Copy files or folders |
||
350 | * Query parameters |
||
351 | * 'identifiers' array of identifier to copy |
||
352 | * 'targetFolderIdentifier' string the target identifier. Must be a folder. |
||
353 | * 'conflictMode' string one of: "replace", "cancel", "rename", as defined in \TYPO3\CMS\Core\Resource\DuplicationBehavior |
||
354 | * |
||
355 | * @param ServerRequestInterface $request |
||
356 | * |
||
357 | * @return JsonResponse |
||
358 | */ |
||
359 | public function copyResourcesAction(ServerRequestInterface $request): JsonResponse |
||
360 | { |
||
361 | try { |
||
362 | $identifiers = $request->getQueryParams()['identifiers']; |
||
363 | $conflictMode = $request->getQueryParams()['conflictMode'] ?? ''; |
||
364 | $targetFolderIdentifier = $request->getQueryParams()['targetFolderIdentifier']; |
||
365 | if (empty($identifiers)) { |
||
366 | throw new ControllerException('Identifiers needed', 1553699828); |
||
367 | } |
||
368 | if (empty($conflictMode) || !in_array($conflictMode, ['replace', 'cancel', 'rename'], true)) { |
||
369 | throw new ControllerException('conflictMode must be one of "replace", "cancel", "rename"'); |
||
370 | } |
||
371 | if (empty($targetFolderIdentifier)) { |
||
372 | throw new ControllerException( |
||
373 | 'Target folder identifier needed', |
||
374 | 1554122023 |
||
375 | ); |
||
376 | } |
||
377 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
378 | $targetFolderObject = $resourceFactory->getObjectFromCombinedIdentifier($targetFolderIdentifier); |
||
379 | if (!$targetFolderObject instanceof Folder) { |
||
380 | throw new ControllerException('Target identifier is not a folder', 1553701684); |
||
381 | } |
||
382 | } catch (ResourceException $e) { |
||
383 | return new JsonExceptionResponse($e); |
||
384 | } catch (ControllerException $e) { |
||
385 | return new JsonExceptionResponse($e); |
||
386 | } |
||
387 | $resources = []; |
||
388 | foreach ($identifiers as $identifier) { |
||
389 | $state = FileOperationResult::FAILED; |
||
390 | $message = ''; |
||
391 | $resultEntity = null; |
||
392 | try { |
||
393 | $sourceObject = $resourceFactory->getObjectFromCombinedIdentifier($identifier); |
||
394 | if ($resultObject = $sourceObject->copyTo($targetFolderObject, null, (string)DuplicationBehavior::cast($conflictMode))) { |
||
395 | if ($resultObject instanceof Folder) { |
||
396 | $resultEntity = new FolderItemFolder($resultObject); |
||
397 | } else { |
||
398 | $resultEntity = new FolderItemFile($resultObject); |
||
399 | } |
||
400 | $state = FileOperationResult::COPIED; |
||
401 | } |
||
402 | } catch (InvalidTargetFolderException $e) { |
||
403 | $message = $e->getMessage(); |
||
404 | } catch (ResourceException $e) { |
||
405 | $message = $e->getMessage(); |
||
406 | } |
||
407 | $resources[] = new FileOperationResult($identifier, $state, $message, $resultEntity); |
||
408 | } |
||
409 | return new FileOperationResponse($resources); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Move files or folders |
||
414 | * Query parameters |
||
415 | * 'identifiers' array of identifier to move |
||
416 | * 'targetFolderIdentifier' string the target identifier. Must be a folder. |
||
417 | * 'conflictMode' string one of: "replace", "cancel", "rename", as defined in \TYPO3\CMS\Core\Resource\DuplicationBehavior |
||
418 | * |
||
419 | * @param ServerRequestInterface $request |
||
420 | * |
||
421 | * @return JsonResponse |
||
422 | */ |
||
423 | public function moveResourcesAction(ServerRequestInterface $request): JsonResponse |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * rename file or folder |
||
475 | * Query parameters |
||
476 | * 'identifier' string identifier to rename |
||
477 | * 'targetName' string The new name of file or folder. |
||
478 | * 'conflictMode' string one of: "replace", "cancel", "rename" |
||
479 | * |
||
480 | * @param ServerRequestInterface $request |
||
481 | * |
||
482 | * @return JsonResponse |
||
483 | */ |
||
484 | public function renameResourcesAction(ServerRequestInterface $request): JsonResponse |
||
485 | { |
||
486 | try { |
||
487 | $identifier = $request->getQueryParams()['identifier']; |
||
488 | $targetName = $request->getQueryParams()['targetName']; |
||
489 | $conflictMode = $request->getQueryParams()['conflictMode'] ?? ''; |
||
490 | if (empty($identifier)) { |
||
491 | throw new ControllerException('Identifier needed', 1553699828); |
||
492 | } |
||
493 | if (empty($conflictMode) || !in_array($conflictMode, ['replace', 'cancel', 'rename'], true)) { |
||
494 | throw new ControllerException('conflictMode must be one of "replace", "cancel", "rename"'); |
||
495 | } |
||
496 | if (empty($targetName)) { |
||
497 | throw new ControllerException('Target name needed', 1554193259); |
||
498 | } |
||
499 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
500 | $fileOrFolder = $resourceFactory->retrieveFileOrFolderObject($identifier); |
||
501 | } catch (ResourceException $e) { |
||
502 | return new JsonExceptionResponse($e); |
||
503 | } catch (ControllerException $e) { |
||
504 | return new JsonExceptionResponse($e); |
||
505 | } |
||
506 | $resources = []; |
||
507 | $state = FileOperationResult::FAILED; |
||
508 | $resultEntity = null; |
||
509 | try { |
||
510 | if ($fileOrFolder === null) { |
||
511 | throw new ResourceException\ResourceDoesNotExistException('Resource does not exist'); |
||
512 | } else { |
||
513 | $resultObject = $fileOrFolder->rename($targetName, (string)DuplicationBehavior::cast($conflictMode)); |
||
514 | if ($resultObject instanceof Folder) { |
||
515 | $resultEntity = new FolderItemFolder($resultObject); |
||
516 | $message = 'Folder was successfully renamed'; |
||
517 | } else { |
||
518 | $resultEntity = new FolderItemFile($resultObject); |
||
519 | $message = 'File was successfully renamed'; |
||
520 | } |
||
521 | $state = FileOperationResult::RENAMED; |
||
522 | } |
||
523 | } catch (ResourceException $e) { |
||
524 | $message = $e->getMessage(); |
||
525 | } |
||
526 | $resources[] = new FileOperationResult($identifier, $state, $message, $resultEntity); |
||
527 | return new JsonResponse($resources); |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * delete file or folder |
||
532 | * Query parameters |
||
533 | * 'identifiers' array of strings identifier of file or folder to delete |
||
534 | * |
||
535 | * @param ServerRequestInterface $request |
||
536 | * |
||
537 | * @return JsonResponse |
||
538 | */ |
||
539 | public function deleteResourcesAction(ServerRequestInterface $request): JsonResponse |
||
540 | { |
||
541 | try { |
||
542 | $identifiers = $request->getQueryParams()['identifiers']; |
||
543 | if (empty($identifiers)) { |
||
544 | throw new ControllerException('Identifiers needed', 1553699828); |
||
545 | } |
||
546 | } catch (ControllerException $e) { |
||
547 | return new JsonExceptionResponse($e); |
||
548 | } |
||
549 | $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class); |
||
550 | $resources = []; |
||
551 | foreach ($identifiers as $identifier) { |
||
552 | try { |
||
553 | $sourceObject = $resourceFactory->getObjectFromCombinedIdentifier($identifier); |
||
554 | if ($sourceObject->delete(true)) { |
||
555 | $state = FileOperationResult::DELETED; |
||
556 | $message = 'Resource deleted'; |
||
557 | } else { |
||
558 | $state = FileOperationResult::FAILED; |
||
559 | $message = 'Resource could not be deleted'; |
||
560 | } |
||
561 | } catch (ResourceException $e) { |
||
562 | $state = FileOperationResult::FAILED; |
||
563 | $message = $e->getMessage(); |
||
564 | } |
||
565 | $resources[] = new FileOperationResult($identifier, $state, $message, null); |
||
566 | } |
||
567 | return new FileOperationResponse($resources); |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * @return BackendUserAuthentication |
||
572 | */ |
||
573 | protected function getBackendUser(): BackendUserAuthentication |
||
576 | } |
||
577 | } |
||
578 |