| Total Complexity | 55 |
| Total Lines | 480 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 36 | class ApiController extends ActionController |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var \TYPO3\CMS\Extbase\Mvc\View\JsonView |
||
| 40 | */ |
||
| 41 | protected $view; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * security |
||
| 51 | * |
||
| 52 | * @var \EWW\Dpf\Security\Security |
||
| 53 | * @inject |
||
| 54 | */ |
||
| 55 | protected $security = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * documentRepository |
||
| 59 | * |
||
| 60 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
| 61 | * @inject |
||
| 62 | */ |
||
| 63 | protected $documentRepository = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * frontendUserRepository |
||
| 67 | * |
||
| 68 | * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository |
||
| 69 | * @inject |
||
| 70 | */ |
||
| 71 | protected $frontendUserRepository = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * documentManager |
||
| 75 | * |
||
| 76 | * @var \EWW\Dpf\Services\Document\DocumentManager |
||
| 77 | * @inject |
||
| 78 | */ |
||
| 79 | protected $documentManager = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * clientRepository |
||
| 83 | * |
||
| 84 | * @var \EWW\Dpf\Domain\Repository\ClientRepository |
||
| 85 | * @inject |
||
| 86 | */ |
||
| 87 | protected $clientRepository; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * persistence manager |
||
| 91 | * |
||
| 92 | * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface |
||
| 93 | * @inject |
||
| 94 | */ |
||
| 95 | protected $persistenceManager; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * signalSlotDispatcher |
||
| 99 | * |
||
| 100 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
| 101 | * @inject |
||
| 102 | */ |
||
| 103 | protected $signalSlotDispatcher = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * logger |
||
| 107 | * |
||
| 108 | * @var \TYPO3\CMS\Core\Log\Logger |
||
| 109 | */ |
||
| 110 | protected $logger = null; |
||
| 111 | |||
| 112 | protected $frontendUser = null; |
||
| 113 | |||
| 114 | |||
| 115 | public function __construct() |
||
| 116 | { |
||
| 117 | /** @var $logger \TYPO3\CMS\Core\Log\Logger */ |
||
| 118 | $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function checkToken($token) { |
||
| 122 | // check if token exists |
||
| 123 | $frontendUser = $this->frontendUserRepository->findOneByApiToken($token); |
||
|
|
|||
| 124 | |||
| 125 | if ($frontendUser) { |
||
| 126 | $this->frontendUser = $frontendUser; |
||
| 127 | return true; |
||
| 128 | } else { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $document |
||
| 135 | * @param string $token |
||
| 136 | */ |
||
| 137 | public function showAction($document, $token) { |
||
| 138 | if ($this->checkToken($token)) { |
||
| 139 | /** @var Document $doc */ |
||
| 140 | $doc = $this->documentManager->read($document); |
||
| 141 | //$doc = $this->documentRepository->findByIdentifier($document); |
||
| 142 | |||
| 143 | if ($doc) { |
||
| 144 | $this->security->getUser()->getUid(); |
||
| 145 | |||
| 146 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
| 147 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
| 148 | |||
| 149 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
| 150 | $mapper->setMapping($client->getFisMapping()); |
||
| 151 | $jsonData = $mapper->getJson($doc); |
||
| 152 | return $jsonData; |
||
| 153 | } |
||
| 154 | |||
| 155 | return '{"error": "No data found"}'; |
||
| 156 | } |
||
| 157 | return '{"error": "Token failed"}'; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $json |
||
| 162 | * @param string $token |
||
| 163 | */ |
||
| 164 | public function createAction($json, $token) { |
||
| 165 | if ($this->checkToken($token)) { |
||
| 166 | if ($json) { |
||
| 167 | $jsonData = $json; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (empty($jsonData)) { |
||
| 171 | return '{"error": "invalid data"}'; |
||
| 172 | } |
||
| 173 | |||
| 174 | $mapper = $this->objectManager->get(\EWW\Dpf\Services\Api\JsonToDocumentMapper::class); |
||
| 175 | |||
| 176 | /** @var Document $document */ |
||
| 177 | $document = $mapper->getDocument($jsonData); |
||
| 178 | |||
| 179 | if ($this->tokenUserId) { |
||
| 180 | $document->setCreator($this->security->getUser()->getUid()); |
||
| 181 | } |
||
| 182 | |||
| 183 | // xml data fields are limited to 64 KB |
||
| 184 | if (strlen($document->getXmlData()) >= 64 * 1024) { |
||
| 185 | return '{"error": "Maximum document size exceeded"}'; |
||
| 186 | } |
||
| 187 | |||
| 188 | $processNumber = $document->getProcessNumber(); |
||
| 189 | if (empty($processNumber)) { |
||
| 190 | $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
||
| 191 | $processNumber = $processNumberGenerator->getProcessNumber(); |
||
| 192 | $document->setProcessNumber($processNumber); |
||
| 193 | } |
||
| 194 | |||
| 195 | $this->documentRepository->add($document); |
||
| 196 | $this->persistenceManager->persistAll(); |
||
| 197 | |||
| 198 | // index the document |
||
| 199 | $this->signalSlotDispatcher->dispatch( |
||
| 200 | AbstractController::class, 'indexDocument', [$document] |
||
| 201 | ); |
||
| 202 | |||
| 203 | return '{"success": "Document created", "id": "' . $document->getProcessNumber() . '"}'; |
||
| 204 | } |
||
| 205 | return '{"error": "Token failed"}'; |
||
| 206 | |||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $document |
||
| 211 | * @param string $id |
||
| 212 | * @param string $token |
||
| 213 | * @throws \Exception |
||
| 214 | */ |
||
| 215 | public function addFisIdAction($document, $id, $token) { |
||
| 216 | if ($this->checkToken($token)) { |
||
| 217 | /** @var Document $doc */ |
||
| 218 | $doc = $this->documentManager->read($document); |
||
| 219 | |||
| 220 | $internalFormat = new \EWW\Dpf\Helper\InternalFormat($doc->getXmlData()); |
||
| 221 | $internalFormat->setFisId($id); |
||
| 222 | $doc->setXmlData($internalFormat->getXml()); |
||
| 223 | |||
| 224 | $processNumber = $doc->getProcessNumber(); |
||
| 225 | if (empty($processNumber)) { |
||
| 226 | $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
||
| 227 | $processNumber = $processNumberGenerator->getProcessNumber(); |
||
| 228 | $doc->setProcessNumber($processNumber); |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->documentManager->update($doc); |
||
| 232 | |||
| 233 | // index the document |
||
| 234 | $this->signalSlotDispatcher->dispatch( |
||
| 235 | AbstractController::class, 'indexDocument', [$doc] |
||
| 236 | ); |
||
| 237 | |||
| 238 | return '{"success": "Document '.$document.' added '.$id.'"}'; |
||
| 239 | } |
||
| 240 | return '{"error": "Token failed"}'; |
||
| 241 | |||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $document |
||
| 246 | * @param string $json |
||
| 247 | * @param string $token |
||
| 248 | * @param bool $restore |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function suggestionAction($document, $json, $token, $restore = false) { |
||
| 252 | if ($this->checkToken($token)) { |
||
| 253 | /** @var Document $doc */ |
||
| 254 | $doc = $this->documentManager->read($document); |
||
| 255 | |||
| 256 | if ($json) { |
||
| 257 | $jsonData = $json; |
||
| 258 | } |
||
| 259 | |||
| 260 | if (empty($jsonData) && $restore == false) { |
||
| 261 | return '{"error": "invalid data"}'; |
||
| 262 | } |
||
| 263 | |||
| 264 | $mapper = $this->objectManager->get(\EWW\Dpf\Services\Api\JsonToDocumentMapper::class); |
||
| 265 | |||
| 266 | /** @var Document $editOrigDocument */ |
||
| 267 | $editOrigDocument = $mapper->editDocument($doc, $jsonData); |
||
| 268 | |||
| 269 | $suggestionDocument = $this->documentManager->addSuggestion($editOrigDocument); |
||
| 270 | |||
| 271 | if ($restore) { |
||
| 272 | $suggestionDocument->setTransferStatus("RESTORE"); |
||
| 273 | } |
||
| 274 | |||
| 275 | if ($suggestionDocument) { |
||
| 276 | return '{"success": "Suggestion created", "id": "' . $suggestionDocument->getDocumentIdentifier() . '"}'; |
||
| 277 | } else { |
||
| 278 | return '{"failed": "Suggestion not created"}'; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | return '{"error": "Token failed"}'; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $doi |
||
| 286 | * @param string $token |
||
| 287 | */ |
||
| 288 | public function importDoiWithoutSavingAction(string $doi, $token) { |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $pmid |
||
| 330 | * @param string $token |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function importPubmedWithoutSavingAction($pmid, $token) { |
||
| 334 | if ($this->checkToken($token)) { |
||
| 335 | $importer = $this->objectManager->get(PubMedImporter::class); |
||
| 336 | $externalMetadata = $importer->findByIdentifier($pmid); |
||
| 337 | |||
| 338 | if ($externalMetadata) { |
||
| 339 | // create document |
||
| 340 | try { |
||
| 341 | /** @var Document $newDocument */ |
||
| 342 | $newDocument = $importer->import($externalMetadata); |
||
| 343 | if ($newDocument) { |
||
| 344 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
| 345 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
| 346 | |||
| 347 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
| 348 | $mapper->setMapping($client->getFisMapping()); |
||
| 349 | $jsonData = $mapper->getJson($newDocument); |
||
| 350 | return $jsonData; |
||
| 351 | } else { |
||
| 352 | return '{"failed": "Import failed"}'; |
||
| 353 | } |
||
| 354 | |||
| 355 | } catch (\Throwable $throwable) { |
||
| 356 | |||
| 357 | $this->logger->error($throwable->getMessage()); |
||
| 358 | return '{"failed": "' . $throwable->getMessage() . '"}'; |
||
| 359 | } |
||
| 360 | |||
| 361 | } else { |
||
| 362 | // error |
||
| 363 | return '{"failed": "Nothing found"}'; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | return '{"error": "Token failed"}'; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $isbn |
||
| 371 | * @param string $token |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function importIsbnWithoutSavingAction($isbn, $token) { |
||
| 375 | if ($this->checkToken($token)) { |
||
| 376 | $importer = $this->objectManager->get(K10plusImporter::class); |
||
| 377 | $externalMetadata = $importer->findByIdentifier(str_replace('- ', '', $isbn)); |
||
| 378 | |||
| 379 | if ($externalMetadata) { |
||
| 380 | // create document |
||
| 381 | try { |
||
| 382 | /** @var Document $newDocument */ |
||
| 383 | $newDocument = $importer->import($externalMetadata); |
||
| 384 | if ($newDocument) { |
||
| 385 | /** @var $client \EWW\Dpf\Domain\Model\Client */ |
||
| 386 | $client = $this->clientRepository->findAllByPid($this->frontendUser->getPid())->current(); |
||
| 387 | |||
| 388 | $mapper = new \EWW\Dpf\Services\Api\DocumentToJsonMapper(); |
||
| 389 | $mapper->setMapping($client->getFisMapping()); |
||
| 390 | $jsonData = $mapper->getJson($newDocument); |
||
| 391 | return $jsonData; |
||
| 392 | } else { |
||
| 393 | return '{"failed": "Import failed"}'; |
||
| 394 | } |
||
| 395 | |||
| 396 | } catch (\Throwable $throwable) { |
||
| 397 | |||
| 398 | $this->logger->error($throwable->getMessage()); |
||
| 399 | return '{"failed": "' . $throwable->getMessage() . '"}'; |
||
| 400 | } |
||
| 401 | |||
| 402 | } else { |
||
| 403 | // error |
||
| 404 | return '{"failed": "Nothing found"}'; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | return '{"error": "Token failed"}'; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $bibtex content of a bibtex file |
||
| 412 | * @param string $token |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function importBibtexWithoutSavingAction($bibtex, $token) { |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $ris |
||
| 466 | * @param string $token |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function importRisWithoutSavingAction($ris, $token) { |
||
| 516 | } |
||
| 517 | |||
| 518 | |||
| 519 | // /** |
||
| 520 | // * Resolves and checks the current action method name |
||
| 521 | // * |
||
| 522 | // * @return string Method name of the current action |
||
| 544 |