Total Complexity | 94 |
Total Lines | 692 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApiController 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 ApiController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class ApiController extends ActionController |
||
36 | { |
||
37 | /** |
||
38 | * @var \TYPO3\CMS\Extbase\Mvc\View\JsonView |
||
39 | */ |
||
40 | protected $view; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * security |
||
50 | * |
||
51 | * @var \EWW\Dpf\Security\Security |
||
52 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
53 | */ |
||
54 | protected $security = null; |
||
55 | |||
56 | /** |
||
57 | * documentRepository |
||
58 | * |
||
59 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
60 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
61 | */ |
||
62 | protected $documentRepository = null; |
||
63 | |||
64 | /** |
||
65 | * frontendUserRepository |
||
66 | * |
||
67 | * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository |
||
68 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
69 | */ |
||
70 | protected $frontendUserRepository = null; |
||
71 | |||
72 | /** |
||
73 | * documentManager |
||
74 | * |
||
75 | * @var \EWW\Dpf\Services\Document\DocumentManager |
||
76 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
77 | */ |
||
78 | protected $documentManager = null; |
||
79 | |||
80 | /** |
||
81 | * clientRepository |
||
82 | * |
||
83 | * @var \EWW\Dpf\Domain\Repository\ClientRepository |
||
84 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
85 | */ |
||
86 | protected $clientRepository; |
||
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 | * signalSlotDispatcher |
||
98 | * |
||
99 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
100 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
101 | */ |
||
102 | protected $signalSlotDispatcher = null; |
||
103 | |||
104 | /** |
||
105 | * logger |
||
106 | * |
||
107 | * @var \TYPO3\CMS\Core\Log\Logger |
||
108 | */ |
||
109 | protected $logger = null; |
||
110 | |||
111 | protected $frontendUser = null; |
||
112 | |||
113 | protected $validActions = [ |
||
114 | 'show', 'create', 'suggestion', 'importDoiWithoutSaving', |
||
115 | 'importPubmedWithoutSaving', 'importIsbnWithoutSaving', |
||
116 | 'importBibtexWithoutSaving', 'importRisWithoutSaving', 'addFisId' |
||
117 | ]; |
||
118 | |||
119 | public function __construct() |
||
120 | { |
||
121 | /** @var $logger \TYPO3\CMS\Core\Log\Logger */ |
||
122 | $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); |
||
123 | } |
||
124 | |||
125 | public function checkToken($token) { |
||
126 | // check if token exists |
||
127 | |||
128 | $frontendUser = $this->frontendUserRepository->findOneByApiToken($token); |
||
|
|||
129 | |||
130 | if ($frontendUser) { |
||
131 | $this->frontendUser = $frontendUser; |
||
132 | return true; |
||
133 | } else { |
||
134 | return false; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
140 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
141 | */ |
||
142 | public function initializeShowAction() |
||
143 | { |
||
144 | $this->checkMandatoryParameters(['document', 'token']); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $document |
||
149 | * @param string $token |
||
150 | */ |
||
151 | public function showAction($document, $token) { |
||
152 | if ($this->checkToken($token)) { |
||
153 | /** @var Document $doc */ |
||
154 | $doc = $this->documentManager->read($document); |
||
155 | |||
156 | if ($doc) { |
||
157 | $this->security->getUser()->getUid(); |
||
158 | |||
159 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
160 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
161 | |||
162 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
163 | $mapper->setMapping($client->getFisMapping()); |
||
164 | $jsonData = $mapper->getJson($doc); |
||
165 | return $jsonData; |
||
166 | } |
||
167 | |||
168 | return '{"error": "No data found"}'; |
||
169 | } |
||
170 | return '{"error": "Token failed"}'; |
||
171 | } |
||
172 | |||
173 | |||
174 | /** |
||
175 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
176 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
177 | */ |
||
178 | public function initializeCreateAction() |
||
179 | { |
||
180 | $this->checkMandatoryParameters(['json', 'token']); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param string $json |
||
185 | * @param string $token |
||
186 | */ |
||
187 | public function createAction($json, $token) { |
||
188 | |||
189 | if ($this->checkToken($token)) { |
||
190 | |||
191 | if (is_null(json_decode($json))) { |
||
192 | return '{"error": "Invalid data in parameter json"}'; |
||
193 | } |
||
194 | |||
195 | $mapper = $this->objectManager->get(\EWW\Dpf\Services\Api\JsonToDocumentMapper::class); |
||
196 | |||
197 | /** @var Document $document */ |
||
198 | $document = $mapper->getDocument($json); |
||
199 | |||
200 | if ($this->tokenUserId) { |
||
201 | $document->setCreator($this->security->getUser()->getUid()); |
||
202 | } |
||
203 | |||
204 | // xml data fields are limited to 64 KB |
||
205 | if (strlen($document->getXmlData()) >= Document::XML_DATA_SIZE_LIMIT) { |
||
206 | return '{"error": "Maximum document size exceeded"}'; |
||
207 | } |
||
208 | |||
209 | $processNumber = $document->getProcessNumber(); |
||
210 | if (empty($processNumber)) { |
||
211 | $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
||
212 | $processNumber = $processNumberGenerator->getProcessNumber(); |
||
213 | $document->setProcessNumber($processNumber); |
||
214 | } |
||
215 | |||
216 | $this->documentRepository->add($document); |
||
217 | $this->persistenceManager->persistAll(); |
||
218 | |||
219 | // index the document |
||
220 | $this->signalSlotDispatcher->dispatch( |
||
221 | AbstractController::class, 'indexDocument', [$document] |
||
222 | ); |
||
223 | |||
224 | return '{"success": "Document created", "id": "' . $document->getProcessNumber() . '"}'; |
||
225 | } |
||
226 | return '{"error": "Token failed"}'; |
||
227 | |||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
232 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
233 | */ |
||
234 | public function initializeAddFisIdAction() |
||
235 | { |
||
236 | $this->checkMandatoryParameters(['document', 'id', 'token']); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param string $document |
||
241 | * @param string $id |
||
242 | * @param string $token |
||
243 | * @throws \Exception |
||
244 | */ |
||
245 | public function addFisIdAction($document, $id, $token) { |
||
246 | if ($this->checkToken($token)) { |
||
247 | /** @var Document $doc */ |
||
248 | $doc = $this->documentManager->read($document); |
||
249 | |||
250 | $internalFormat = new \EWW\Dpf\Helper\InternalFormat($doc->getXmlData()); |
||
251 | $internalFormat->setFisId($id); |
||
252 | $doc->setXmlData($internalFormat->getXml()); |
||
253 | |||
254 | $processNumber = $doc->getProcessNumber(); |
||
255 | if (empty($processNumber)) { |
||
256 | $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
||
257 | $processNumber = $processNumberGenerator->getProcessNumber(); |
||
258 | $doc->setProcessNumber($processNumber); |
||
259 | } |
||
260 | |||
261 | if ($this->documentManager->update($doc, null,true)) { |
||
262 | return '{"success": "Document '.$document.' added '.$id.'"}'; |
||
263 | } else { |
||
264 | return '{"failed": Could not update the Document"}'; |
||
265 | } |
||
266 | } |
||
267 | return '{"error": "Token failed"}'; |
||
268 | |||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
273 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
274 | */ |
||
275 | public function initializeSuggestionAction() |
||
276 | { |
||
277 | $this->checkMandatoryParameters(['document', 'json', 'comment', 'token']); |
||
278 | if ($this->request->hasArgument('restore')) { |
||
279 | $restore = $this->request->getArgument('restore'); |
||
280 | $this->request->setArgument('restore', ($restore === 'true' || $restore == 1)); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param string $document |
||
286 | * @param string $json |
||
287 | * @param string $comment |
||
288 | * @param string $token |
||
289 | * @param bool $restore |
||
290 | * @return string |
||
291 | */ |
||
292 | public function suggestionAction($document, $json, $comment, $token, $restore = false) { |
||
293 | |||
294 | if ($this->checkToken($token)) { |
||
295 | |||
296 | if ($restore) { |
||
297 | if (!empty($json) && is_null(json_decode($json))) { |
||
298 | return '{"error": "Invalid data in parameter json."}'; |
||
299 | } |
||
300 | } else { |
||
301 | if (empty($json) || json_decode($json,true) === []) { |
||
302 | return '{"error": "Parameter json can not be empty."}'; |
||
303 | } |
||
304 | if (is_null(json_decode($json))) { |
||
305 | return '{"error": "Invalid data in parameter json."}'; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** @var Document $doc */ |
||
310 | $doc = $this->documentManager->read($document); |
||
311 | |||
312 | if (!$doc) { |
||
313 | return '{"failed": "Document does not exist: '.$document.'"}'; |
||
314 | } |
||
315 | |||
316 | if ($doc->getState() === DocumentWorkflow::STATE_NEW_NONE) { |
||
317 | return '{"failed": "Access denied. The document is private."}'; |
||
318 | } |
||
319 | |||
320 | $linkedDocument = $this->documentRepository->findOneByLinkedUid($doc->getUid()); |
||
321 | if (!$linkedDocument && $doc->getObjectIdentifier()) { |
||
322 | $linkedDocument = $this->documentRepository->findOneByLinkedUid($doc->getObjectIdentifier()); |
||
323 | } |
||
324 | |||
325 | if ($linkedDocument) { |
||
326 | return '{"failed": "There is already a suggestion for the document: '.$linkedDocument->getUid().'"}'; |
||
327 | } |
||
328 | |||
329 | $mapper = $this->objectManager->get(\EWW\Dpf\Services\Api\JsonToDocumentMapper::class); |
||
330 | |||
331 | /** @var Document $editOrigDocument */ |
||
332 | $editOrigDocument = $mapper->editDocument($doc, $json); |
||
333 | $editOrigDocument->setCreator($this->frontendUser->getUid()); |
||
334 | $suggestionDocument = $this->documentManager->addSuggestion($editOrigDocument, $restore, $comment); |
||
335 | |||
336 | if ($restore) { |
||
337 | $suggestionDocument->setTransferStatus("RESTORE"); |
||
338 | } |
||
339 | |||
340 | if ($suggestionDocument) { |
||
341 | return '{"success": "Suggestion created", "id": "' . $suggestionDocument->getDocumentIdentifier() . '"}'; |
||
342 | } else { |
||
343 | return '{"failed": "Suggestion not created"}'; |
||
344 | } |
||
345 | } |
||
346 | return '{"error": "Token failed"}'; |
||
347 | } |
||
348 | |||
349 | |||
350 | /** |
||
351 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
352 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
353 | */ |
||
354 | public function initializeImportDoiWithoutSavingAction() |
||
355 | { |
||
356 | $this->checkMandatoryParameters(['doi', 'token']); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param string $doi |
||
361 | * @param string $token |
||
362 | */ |
||
363 | public function importDoiWithoutSavingAction(string $doi, $token) { |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
407 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
408 | */ |
||
409 | public function initializeImportPubmedWithoutSavingAction() |
||
410 | { |
||
411 | $this->checkMandatoryParameters(['pmid', 'token']); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param string $pmid |
||
416 | * @param string $token |
||
417 | * @return string |
||
418 | */ |
||
419 | public function importPubmedWithoutSavingAction($pmid, $token) { |
||
420 | if ($this->checkToken($token)) { |
||
421 | $importer = $this->objectManager->get(PubMedImporter::class); |
||
422 | $importer->deactivateProcessNumberGeneration(); |
||
423 | |||
424 | $externalMetadata = $importer->findByIdentifier($pmid); |
||
425 | |||
426 | if ($externalMetadata) { |
||
427 | // create document |
||
428 | try { |
||
429 | /** @var Document $newDocument */ |
||
430 | $newDocument = $importer->import($externalMetadata); |
||
431 | if ($newDocument) { |
||
432 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
433 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
434 | |||
435 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
436 | $mapper->setMapping($client->getFisMapping()); |
||
437 | $jsonData = $mapper->getJson($newDocument); |
||
438 | return $jsonData; |
||
439 | } else { |
||
440 | return '{"failed": "Import failed"}'; |
||
441 | } |
||
442 | |||
443 | } catch (\Throwable $throwable) { |
||
444 | |||
445 | $this->logger->log(\TYPO3\CMS\Core\Log\LogLevel::ERROR, $throwable->getMessage()); |
||
446 | return '{"failed": "' . $throwable->getMessage() . '"}'; |
||
447 | } |
||
448 | |||
449 | } else { |
||
450 | // error |
||
451 | return '{"failed": "Nothing found"}'; |
||
452 | } |
||
453 | } |
||
454 | return '{"error": "Token failed"}'; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
459 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
460 | */ |
||
461 | public function initializeImportIsbnWithoutSavingAction() |
||
462 | { |
||
463 | $this->checkMandatoryParameters(['isbn', 'token']); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * @param string $isbn |
||
468 | * @param string $token |
||
469 | * @return string |
||
470 | */ |
||
471 | public function importIsbnWithoutSavingAction($isbn, $token) { |
||
472 | if ($this->checkToken($token)) { |
||
473 | $importer = $this->objectManager->get(K10plusImporter::class); |
||
474 | $importer->deactivateProcessNumberGeneration(); |
||
475 | |||
476 | $externalMetadata = $importer->findByIdentifier(str_replace('- ', '', $isbn)); |
||
477 | |||
478 | if ($externalMetadata) { |
||
479 | // create document |
||
480 | try { |
||
481 | /** @var Document $newDocument */ |
||
482 | $newDocument = $importer->import($externalMetadata); |
||
483 | if ($newDocument) { |
||
484 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
485 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
486 | |||
487 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
488 | $mapper->setMapping($client->getFisMapping()); |
||
489 | $jsonData = $mapper->getJson($newDocument); |
||
490 | return $jsonData; |
||
491 | } else { |
||
492 | return '{"failed": "Import failed"}'; |
||
493 | } |
||
494 | |||
495 | } catch (\Throwable $throwable) { |
||
496 | |||
497 | $this->logger->log(\TYPO3\CMS\Core\Log\LogLevel::ERROR, $throwable->getMessage()); |
||
498 | return '{"failed": "' . $throwable->getMessage() . '"}'; |
||
499 | } |
||
500 | |||
501 | } else { |
||
502 | // error |
||
503 | return '{"failed": "Nothing found"}'; |
||
504 | } |
||
505 | } |
||
506 | return '{"error": "Token failed"}'; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
511 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
512 | */ |
||
513 | public function initializeImportBibtexWithoutSavingAction() |
||
514 | { |
||
515 | $this->checkMandatoryParameters(['bibtex', 'token']); |
||
516 | if ($this->request->hasArgument('force')) { |
||
517 | $force = $this->request->getArgument('force'); |
||
518 | $this->request->setArgument('force', ($force === 'true' || $force == 1)); |
||
519 | } |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * @param string $bibtex content of a bibtex file |
||
524 | * @param string $token |
||
525 | * @param bool $force |
||
526 | * @return string |
||
527 | */ |
||
528 | public function importBibtexWithoutSavingAction($bibtex, $token, $force = false) { |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
591 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
592 | */ |
||
593 | public function initializeImportRisWithoutSavingAction() |
||
594 | { |
||
595 | $this->checkMandatoryParameters(['ris', 'token']); |
||
596 | if ($this->request->hasArgument('force')) { |
||
597 | $force = $this->request->getArgument('force'); |
||
598 | $this->request->setArgument('force', ($force === 'true' || $force == 1)); |
||
599 | } |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * @param string $ris |
||
604 | * @param string $token |
||
605 | * @param bool $force |
||
606 | * @return string |
||
607 | */ |
||
608 | public function importRisWithoutSavingAction($ris, $token, $force = false) { |
||
671 | } |
||
672 | |||
673 | |||
674 | /** |
||
675 | * @param $parameterNames array |
||
676 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
677 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException |
||
678 | */ |
||
679 | protected function checkMandatoryParameters($parameterNames) |
||
680 | { |
||
681 | $missingArguments = []; |
||
682 | foreach ($parameterNames as $parameterName) { |
||
683 | if (!$this->request->hasArgument($parameterName)) { |
||
684 | $missingArguments[] = $parameterName; |
||
685 | } |
||
686 | } |
||
687 | |||
688 | if ($missingArguments) { |
||
689 | $this->throwStatus( |
||
690 | 400, |
||
691 | null, |
||
692 | '{"error": "Missing parameters: '.implode(", ", $missingArguments).'"}' |
||
693 | ); |
||
694 | } |
||
695 | |||
696 | } |
||
697 | |||
698 | /** |
||
699 | * Resolves and checks the current action method name |
||
700 | * |
||
701 | * @return string Method name of the current action |
||
702 | */ |
||
703 | protected function resolveActionMethodName() |
||
727 | } |
||
728 | } |
||
729 |