Total Complexity | 46 |
Total Lines | 387 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 | * documentManager |
||
34 | * |
||
35 | * @var \EWW\Dpf\Services\Document\DocumentManager |
||
36 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
37 | */ |
||
38 | protected $documentManager = null; |
||
39 | |||
40 | /** |
||
41 | * documentRepository |
||
42 | * |
||
43 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
44 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
45 | */ |
||
46 | protected $documentRepository = null; |
||
47 | |||
48 | /** |
||
49 | * fileRepository |
||
50 | * |
||
51 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
52 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
53 | */ |
||
54 | protected $fileRepository = null; |
||
55 | |||
56 | /** |
||
57 | * documentTypeRepository |
||
58 | * |
||
59 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
60 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
61 | */ |
||
62 | protected $documentTypeRepository = null; |
||
63 | |||
64 | /** |
||
65 | * metadataGroupRepository |
||
66 | * |
||
67 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
68 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
69 | */ |
||
70 | protected $metadataGroupRepository = null; |
||
71 | |||
72 | /** |
||
73 | * metadataObjectRepository |
||
74 | * |
||
75 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
76 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
77 | */ |
||
78 | protected $metadataObjectRepository = null; |
||
79 | |||
80 | /** |
||
81 | * depositLicenseLogRepository |
||
82 | * |
||
83 | * @var \EWW\Dpf\Domain\Repository\DepositLicenseLogRepository |
||
84 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
85 | */ |
||
86 | protected $depositLicenseLogRepository = null; |
||
87 | |||
88 | /** |
||
89 | * persistence manager |
||
90 | * |
||
91 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
92 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
93 | */ |
||
94 | protected $persistenceManager; |
||
95 | |||
96 | /** |
||
97 | * fisDataService |
||
98 | * |
||
99 | * @var \EWW\Dpf\Services\FeUser\FisDataService |
||
100 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
101 | */ |
||
102 | protected $fisDataService = null; |
||
103 | |||
104 | /** |
||
105 | * @var Document |
||
106 | */ |
||
107 | protected $newDocument = null; |
||
108 | |||
109 | /** |
||
110 | * action list |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | public function listAction() |
||
115 | { |
||
116 | $documents = $this->documentRepository->findAll(); |
||
117 | $docTypes = $this->documentTypeRepository->getDocumentTypesAlphabetically(); |
||
118 | |||
119 | if ($this->request->hasArgument('message')) { |
||
120 | $this->view->assign('message', $this->request->getArgument('message')); |
||
121 | } |
||
122 | |||
123 | if ($this->request->hasArgument('errorFiles')) { |
||
124 | $this->view->assign('errorFiles', $this->request->getArgument('errorFiles')); |
||
125 | } |
||
126 | |||
127 | $this->view->assign('documentTypes', $docTypes); |
||
128 | $this->view->assign('documents', $documents); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * initialize newAction |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function initializeNewAction() |
||
137 | { |
||
138 | |||
139 | $requestArguments = $this->request->getArguments(); |
||
140 | |||
141 | if (array_key_exists('documentData', $requestArguments)) { |
||
142 | die('Error: initializeNewAction'); |
||
|
|||
143 | } elseif (array_key_exists('documentType', $requestArguments)) { |
||
144 | $docTypeUid = $this->request->getArgument('documentType'); |
||
145 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
146 | $document = $this->objectManager->get(Document::class); |
||
147 | $document->setDocumentType($documentType); |
||
148 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
149 | $docForm = $mapper->getDocumentForm($document); |
||
150 | } elseif (array_key_exists('newDocumentForm', $requestArguments)) { |
||
151 | $docForm = $this->request->getArgument('newDocumentForm'); |
||
152 | if (is_numeric($docForm)) { |
||
153 | $sessionData = $this->session->getData(); |
||
154 | $docForm = null; |
||
155 | if (array_key_exists('newDocumentForm', $sessionData)) { |
||
156 | $docForm = unserialize($sessionData['newDocumentForm']); |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $requestArguments['newDocumentForm'] = $docForm; |
||
162 | $this->request->setArguments($requestArguments); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * action new |
||
167 | * |
||
168 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
169 | * @param int $returnDocumentId |
||
170 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("newDocumentForm") |
||
171 | * @return void |
||
172 | */ |
||
173 | public function newAction(DocumentForm $newDocumentForm = null, $returnDocumentId = 0) |
||
174 | { |
||
175 | $this->view->assign('returnDocumentId', $returnDocumentId); |
||
176 | $this->view->assign('documentForm', $newDocumentForm); |
||
177 | |||
178 | if (!empty($this->security->getUserAccessToGroups())) { |
||
179 | $this->view->assign('currentUserAccessToGroup', $this->security->getUserAccessToGroups()); |
||
180 | } |
||
181 | |||
182 | if ($this->fisDataService->getPersonData($this->security->getFisPersId())) { |
||
183 | $this->view->assign('fisPersId', $this->security->getFisPersId()); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | public function initializeCreateAction() |
||
188 | { |
||
189 | $this->documentFormMapping(); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * action create |
||
194 | * |
||
195 | * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm |
||
196 | * @return void |
||
197 | */ |
||
198 | public function createAction(DocumentForm $newDocumentForm) |
||
199 | { |
||
200 | $documentMapper = $this->objectManager->get(DocumentMapper::class); |
||
201 | |||
202 | /* @var $newDocument \EWW\Dpf\Domain\Model\Document */ |
||
203 | $newDocument = $documentMapper->getDocument($newDocumentForm); |
||
204 | $this->newDocument = $newDocument; |
||
205 | |||
206 | $workflow = $this->objectManager->get(DocumentWorkflow::class)->getWorkflow(); |
||
207 | |||
208 | $workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE); |
||
209 | |||
210 | if ($this->request->getPluginName() === "Backoffice") { |
||
211 | $newDocument->setCreator($this->security->getUser()->getUid()); |
||
212 | //$workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE); |
||
213 | } else { |
||
214 | $newDocument->setCreator(0); |
||
215 | $newDocument->setTemporary(true); |
||
216 | //$workflow->apply($newDocument, DocumentWorkflow::TRANSITION_CREATE_REGISTER); |
||
217 | } |
||
218 | |||
219 | // xml data fields are limited to 64 KB |
||
220 | if (strlen($newDocument->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
221 | throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded."); |
||
222 | } |
||
223 | |||
224 | $this->documentRepository->add($newDocument); |
||
225 | $this->persistenceManager->persistAll(); |
||
226 | |||
227 | $newDocument = $this->documentRepository->findByUid($newDocument->getUid()); |
||
228 | $this->persistenceManager->persistAll(); |
||
229 | |||
230 | $depositLicenseLog = $this->depositLicenseLogRepository->findOneByProcessNumber($newDocument->getProcessNumber()); |
||
231 | if (empty($depositLicenseLog) && $newDocument->getDepositLicense()) { |
||
232 | // Only if there was no deposit license a notification may be sent |
||
233 | |||
234 | /** @var DepositLicenseLog $depositLicenseLog */ |
||
235 | $depositLicenseLog = $this->objectManager->get(DepositLicenseLog::class); |
||
236 | $depositLicenseLog->setUsername($this->security->getUsername()); |
||
237 | $depositLicenseLog->setObjectIdentifier($newDocument->getObjectIdentifier()); |
||
238 | $depositLicenseLog->setProcessNumber($newDocument->getProcessNumber()); |
||
239 | $depositLicenseLog->setTitle($newDocument->getTitle()); |
||
240 | $depositLicenseLog->setUrn($newDocument->getPrimaryUrn()); |
||
241 | $depositLicenseLog->setLicenceUri($newDocument->getDepositLicense()); |
||
242 | |||
243 | if ($newDocument->hasFiles()) { |
||
244 | $fileList = []; |
||
245 | foreach ($newDocument->getFile() as $file) { |
||
246 | if (!$file->isFileGroupDeleted()) { |
||
247 | $fileList[] = $file->getTitle(); |
||
248 | } |
||
249 | } |
||
250 | $depositLicenseLog->setFileNames(implode(", ", $fileList)); |
||
251 | } |
||
252 | |||
253 | $this->depositLicenseLogRepository->add($depositLicenseLog); |
||
254 | |||
255 | if (!$newDocument->isTemporary()) { |
||
256 | /** @var Notifier $notifier */ |
||
257 | $notifier = $this->objectManager->get(Notifier::class); |
||
258 | $notifier->sendDepositLicenseNotification($newDocument); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | // Add or update files |
||
263 | $files = $newDocumentForm->getFiles(); |
||
264 | // TODO: Is this still necessary? |
||
265 | if (is_array($files)) { |
||
266 | foreach ($files as $file) { |
||
267 | // TODO: Is this still necessary? |
||
268 | if ($file->getUID()) { |
||
269 | $this->fileRepository->update($file); |
||
270 | } else { |
||
271 | $file->setDocument($newDocument); |
||
272 | $this->fileRepository->add($file); |
||
273 | $newDocument->addFile($file); |
||
274 | $this->documentRepository->update($newDocument); |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | $this->persistenceManager->persistAll(); |
||
279 | |||
280 | if (!$newDocument->isTemporary()) { |
||
281 | // index the document |
||
282 | $this->signalSlotDispatcher->dispatch(AbstractController::class, 'indexDocument', [$newDocument]); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | public function initializeEditAction() |
||
287 | { |
||
288 | $requestArguments = $this->request->getArguments(); |
||
289 | |||
290 | if (array_key_exists('document', $requestArguments)) { |
||
291 | |||
292 | $document = $this->documentManager->read( |
||
293 | $this->request->getArgument('document') |
||
294 | ); |
||
295 | |||
296 | if ($document) { |
||
297 | $mapper = $this->objectManager->get(DocumentMapper::class); |
||
298 | $documentForm = $mapper->getDocumentForm($document); |
||
299 | } |
||
300 | |||
301 | } elseif (array_key_exists('documentForm', $requestArguments)) { |
||
302 | $documentForm = $this->request->getArgument('documentForm'); |
||
303 | } |
||
304 | |||
305 | $requestArguments['documentForm'] = $documentForm; |
||
306 | $this->request->setArguments($requestArguments); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * action edit |
||
311 | * |
||
312 | * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm |
||
313 | * @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("documentForm") |
||
314 | * @return void |
||
315 | */ |
||
316 | public function editAction(DocumentForm $documentForm) |
||
326 | } |
||
327 | } |
||
328 | |||
329 | public function initializeUpdateAction() |
||
330 | { |
||
331 | $requestArguments = $this->request->getArguments(); |
||
332 | |||
333 | if ($this->request->hasArgument('documentData')) { |
||
334 | $documentData = $this->request->getArgument('documentData'); |
||
335 | |||
336 | $formDataReader = $this->objectManager->get(FormDataReader::class); |
||
337 | $formDataReader->setFormData($documentData); |
||
338 | $docForm = $formDataReader->getDocumentForm(); |
||
339 | |||
340 | if (!$docForm->hasValidCsrfToken()) { |
||
341 | throw new Exception("Invalid CSRF Token"); |
||
342 | } |
||
343 | |||
344 | $requestArguments['documentForm'] = $docForm; |
||
345 | |||
346 | $docTypeUid = $documentData['type']; |
||
347 | $documentType = $this->documentTypeRepository->findByUid($docTypeUid); |
||
348 | $virtualType = $documentType->getVirtualType(); |
||
349 | |||
350 | |||
351 | if (!$formDataReader->uploadError() || $virtualType === true) { |
||
352 | $this->request->setArguments($requestArguments); |
||
353 | } else { |
||
354 | $t = $docForm->getFileNames(); |
||
355 | $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t)); |
||
356 | } |
||
357 | } else { |
||
358 | $this->redirectToList("UPLOAD_POST_SIZE_ERROR"); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * action cancel |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | public function cancelAction() |
||
368 | { |
||
369 | $this->redirectToList(); |
||
370 | } |
||
371 | |||
372 | protected function redirectAfterUpdate() |
||
375 | } |
||
376 | |||
377 | protected function redirectToList($message = null) |
||
378 | { |
||
379 | $this->redirect('list'); |
||
380 | } |
||
381 | |||
382 | protected function documentFormMapping() |
||
417 | } |
||
418 | } |
||
419 | |||
421 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.