Total Complexity | 44 |
Total Lines | 395 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractDocumentFormController 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 AbstractDocumentFormController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class AbstractDocumentFormController extends AbstractController |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * documentRepository |
||
35 | * |
||
36 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
37 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
38 | */ |
||
39 | protected $documentRepository = null; |
||
40 | |||
41 | /** |
||
42 | * fileRepository |
||
43 | * |
||
44 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
45 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
46 | */ |
||
47 | protected $fileRepository = null; |
||
48 | |||
49 | /** |
||
50 | * documentTypeRepository |
||
51 | * |
||
52 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
53 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
54 | */ |
||
55 | protected $documentTypeRepository = null; |
||
56 | |||
57 | /** |
||
58 | * metadataGroupRepository |
||
59 | * |
||
60 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
61 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
62 | */ |
||
63 | protected $metadataGroupRepository = null; |
||
64 | |||
65 | /** |
||
66 | * metadataObjectRepository |
||
67 | * |
||
68 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
69 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
70 | */ |
||
71 | protected $metadataObjectRepository = null; |
||
72 | |||
73 | /** |
||
74 | * depositLicenseLogRepository |
||
75 | * |
||
76 | * @var \EWW\Dpf\Domain\Repository\DepositLicenseLogRepository |
||
77 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
78 | */ |
||
79 | protected $depositLicenseLogRepository = null; |
||
80 | |||
81 | /** |
||
82 | * persistence manager |
||
83 | * |
||
84 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
85 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
86 | */ |
||
87 | protected $persistenceManager; |
||
88 | |||
89 | /** |
||
90 | * fisDataService |
||
91 | * |
||
92 | * @var \EWW\Dpf\Services\FeUser\FisDataService |
||
93 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
94 | */ |
||
95 | protected $fisDataService = null; |
||
96 | |||
97 | /** |
||
98 | * action list |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function listAction() |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * initialize newAction |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function initializeNewAction() |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * action new |
||
148 | * |
||
149 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
150 | * @param int $returnDocumentId |
||
151 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm") |
||
152 | * @return void |
||
153 | */ |
||
154 | public function newAction(DocumentForm $newDocumentForm = null, $returnDocumentId = 0) |
||
155 | { |
||
156 | $this->view->assign('returnDocumentId', $returnDocumentId); |
||
157 | $this->view->assign('documentForm', $newDocumentForm); |
||
158 | |||
159 | if (!empty($this->security->getUserAccessToGroups())) { |
||
160 | $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups()); |
||
161 | } |
||
162 | |||
163 | if ($this->fisDataService->getPersonData($this->security->getFisPersId())) { |
||
164 | $this->view->assign('fisPersId', $this->security->getFisPersId()); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public function initializeCreateAction() |
||
169 | { |
||
170 | |||
171 | $requestArguments = $this->request->getArguments(); |
||
172 | |||
173 | if ($this->request->hasArgument('documentData')) { |
||
174 | $documentData = $this->request->getArgument('documentData'); |
||
175 | |||
176 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
177 | $formDataReader->setFormData($documentData); |
||
178 | |||
179 | $docForm = $formDataReader->getDocumentForm(); |
||
180 | $requestArguments['newDocumentForm'] = $docForm; |
||
181 | |||
182 | $docTypeUid = $documentData['type']; |
||
183 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
184 | $virtualType = $documentType->getVirtualType(); |
||
185 | |||
186 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
187 | $this->request->setArguments($requestArguments); |
||
188 | } else { |
||
189 | $t = $docForm->getNewFileNames(); |
||
190 | $this->redirect('list', 'DocumentForm', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
191 | } |
||
192 | } else { |
||
193 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * action create |
||
199 | * |
||
200 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
201 | * @return void |
||
202 | */ |
||
203 | public function createAction(DocumentForm $newDocumentForm) |
||
281 | |||
282 | } |
||
283 | |||
284 | public function initializeEditAction() |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * action edit |
||
310 | * |
||
311 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
312 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm") |
||
313 | * @return void |
||
314 | */ |
||
315 | public function editAction(DocumentForm $documentForm) |
||
325 | } |
||
326 | } |
||
327 | |||
328 | public function initializeUpdateAction() |
||
329 | { |
||
330 | $requestArguments = $this->request->getArguments(); |
||
331 | |||
332 | if ($this->request->hasArgument('documentData')) { |
||
333 | $documentData = $this->request->getArgument('documentData'); |
||
334 | |||
335 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
336 | $formDataReader->setFormData($documentData); |
||
337 | $docForm = $formDataReader->getDocumentForm(); |
||
338 | |||
339 | $requestArguments['documentForm'] = $docForm; |
||
340 | |||
341 | $docTypeUid = $documentData['type']; |
||
342 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
343 | $virtualType = $documentType->getVirtualType(); |
||
344 | |||
345 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
346 | $this->request->setArguments($requestArguments); |
||
347 | } else { |
||
348 | $t = $docForm->getNewFileNames(); |
||
349 | $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
350 | } |
||
351 | } else { |
||
352 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * action update |
||
358 | * |
||
359 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
360 | * @return void |
||
361 | */ |
||
362 | public function updateAction(DocumentForm $documentForm) |
||
363 | { |
||
364 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
365 | |||
366 | /* @var $updateDocument \EWW\Dpf\Domain\Model\Document */ |
||
367 | $updateDocument = $documentMapper->getDocument($documentForm); |
||
368 | |||
369 | // xml data fields are limited to 64 KB |
||
370 | if (strlen($updateDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
371 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
372 | } |
||
373 | |||
374 | // add document to local es index |
||
375 | $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class); |
||
376 | $json = $elasticsearchMapper->getElasticsearchJson($updateDocument); |
||
377 | |||
378 | $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class); |
||
379 | // send document to index |
||
380 | $elasticsearchRepository->add($updateDocument, $json); |
||
381 | |||
382 | $updateDocument->setChanged(true); |
||
383 | $this->documentRepository->update($updateDocument); |
||
384 | |||
385 | |||
386 | // Delete files |
||
387 | foreach ($documentForm->getDeletedFiles() as $deleteFile) { |
||
388 | $deleteFile->setStatus(File::STATUS_DELETED); |
||
389 | $this->fileRepository->update($deleteFile); |
||
390 | } |
||
391 | |||
392 | // Add or update files |
||
393 | foreach ($documentForm->getNewFiles() as $newFile) { |
||
394 | |||
395 | if ($newFile->getUID()) { |
||
396 | $this->fileRepository->update($newFile); |
||
397 | } else { |
||
398 | $updateDocument->addFile($newFile); |
||
399 | } |
||
400 | |||
401 | } |
||
402 | |||
403 | // index the document |
||
404 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$updateDocument]); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * action cancel |
||
409 | * |
||
410 | * @return void |
||
411 | */ |
||
412 | public function cancelAction() |
||
413 | { |
||
414 | $this->redirectToList(); |
||
415 | } |
||
416 | |||
417 | protected function redirectAfterUpdate() |
||
420 | } |
||
421 | |||
422 | protected function redirectToList($message = null) |
||
423 | { |
||
428 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.