| Total Complexity | 52 |
| Total Lines | 548 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AjaxBackofficeController 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 AjaxBackofficeController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class AjaxBackofficeController extends \EWW\Dpf\Controller\AbstractController |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * bookmarkRepository |
||
| 36 | * |
||
| 37 | * @var \EWW\Dpf\Domain\Repository\BookmarkRepository |
||
| 38 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 39 | */ |
||
| 40 | protected $bookmarkRepository = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * frontendUserRepository |
||
| 44 | * |
||
| 45 | * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository |
||
| 46 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 47 | */ |
||
| 48 | protected $frontendUserRepository = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * metadataGroupRepository |
||
| 52 | * |
||
| 53 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
| 54 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 55 | */ |
||
| 56 | protected $metadataGroupRepository = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * externalMetadataRepository |
||
| 60 | * |
||
| 61 | * @var \EWW\Dpf\Domain\Repository\ExternalMetadataRepository |
||
| 62 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 63 | */ |
||
| 64 | protected $externalMetadataRepository = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Adds a the given document identifier to the bookmark list of the current fe user. |
||
| 68 | * |
||
| 69 | * @param string $identifier |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function addBookmarkAction($identifier) |
||
| 73 | { |
||
| 74 | /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */ |
||
| 75 | $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier); |
||
| 76 | if (!$bookmark) { |
||
|
|
|||
| 77 | $bookmark = $this->objectManager->get(\EWW\Dpf\Domain\Model\Bookmark::class); |
||
| 78 | $bookmark->setDocumentIdentifier($identifier); |
||
| 79 | $bookmark->setFeUserUid($this->security->getUser()->getUid()); |
||
| 80 | $this->bookmarkRepository->add($bookmark); |
||
| 81 | return true; |
||
| 82 | } |
||
| 83 | |||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Removes the given document from the bookmark list of the current fe user. |
||
| 89 | * |
||
| 90 | * @param string $identifier |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function removeBookmarkAction($identifier) |
||
| 94 | { |
||
| 95 | /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */ |
||
| 96 | $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier); |
||
| 97 | if ($bookmark) { |
||
| 98 | $this->bookmarkRepository->remove($bookmark); |
||
| 99 | return true; |
||
| 100 | } |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Adds a workspace filter to the session. |
||
| 107 | * |
||
| 108 | * @param string $name |
||
| 109 | * @param array $values |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function addWorkspaceFilterAction($name, $values = []) |
||
| 113 | { |
||
| 114 | /** @var SearchSessionData $workspaceSessionData */ |
||
| 115 | $workspaceSessionData = $this->session->getWorkspaceData(); |
||
| 116 | $workspaceSessionData->setFilter($name, $values); |
||
| 117 | $this->session->setWorkspaceData($workspaceSessionData); |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Adds a workspace sort to the session. |
||
| 123 | * |
||
| 124 | * @param string $field |
||
| 125 | * @param string $order |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function addWorkspaceSortAction($field, $order) |
||
| 129 | { |
||
| 130 | /** @var SearchSessionData $workspaceSessionData */ |
||
| 131 | $workspaceSessionData = $this->session->getWorkspaceData(); |
||
| 132 | $workspaceSessionData->setSortField($field); |
||
| 133 | $workspaceSessionData->setSortOrder($order); |
||
| 134 | $this->session->setWorkspaceData($workspaceSessionData); |
||
| 135 | return true; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Toggles the filter to exclude discarded documents. |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function toggleWorkspaceExcludeDiscardedAction() |
||
| 144 | { |
||
| 145 | /** @var SearchSessionData $workspaceSessionData */ |
||
| 146 | $workspaceSessionData = $this->session->getWorkspaceData(); |
||
| 147 | $workspaceSessionData->toggleExcludeDiscardedFilter(); |
||
| 148 | $this->session->setWorkspaceData($workspaceSessionData); |
||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Toggles the filter to hide bookmarked documents. |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function toggleWorkspaceBookmarksOnlyAction() |
||
| 158 | { |
||
| 159 | /** @var SearchSessionData $workspaceSessionData */ |
||
| 160 | $workspaceSessionData = $this->session->getWorkspaceData(); |
||
| 161 | $workspaceSessionData->toggleBookmarksOnlyFilter(); |
||
| 162 | $this->session->setWorkspaceData($workspaceSessionData); |
||
| 163 | return true; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the items per page for the workspace list. |
||
| 168 | * |
||
| 169 | * @param int $itemsPerPage |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function setWorkspaceItemsPerPageAction($itemsPerPage) |
||
| 173 | { |
||
| 174 | /** @var SearchSessionData $workspaceSessionData */ |
||
| 175 | $workspaceSessionData = $this->session->getWorkspaceData(); |
||
| 176 | $workspaceSessionData->setItemsPerPage($itemsPerPage); |
||
| 177 | $this->session->setWorkspaceData($workspaceSessionData); |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Save an extended search query. |
||
| 184 | * |
||
| 185 | * @param string $name |
||
| 186 | * @param string $query |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function saveExtendedSearchAction($name, $query) |
||
| 190 | { |
||
| 191 | $search = new \EWW\Dpf\Domain\Model\StoredSearch(); |
||
| 192 | $search->setName($name); |
||
| 193 | $search->setQuery($query); |
||
| 194 | |||
| 195 | /** @var FrontendUser $feUser */ |
||
| 196 | $feUser = $this->security->getUser(); |
||
| 197 | $feUser->addStoredSearch($search); |
||
| 198 | $this->frontendUserRepository->update($feUser); |
||
| 199 | |||
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Loads a stored extended search query. |
||
| 205 | * |
||
| 206 | * @param int $id |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function loadExtendedSearchAction($id) |
||
| 210 | { |
||
| 211 | /** @var FrontendUser $feUser */ |
||
| 212 | $feUser = $this->security->getUser(); |
||
| 213 | $searches = $feUser->getStoredSearches(); |
||
| 214 | |||
| 215 | foreach ($searches as $search) { |
||
| 216 | if ($search->getUid() == $id) { |
||
| 217 | return $search->getQuery(); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Loads a list of all stored extended search queries. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function loadExtendedSearchListAction() |
||
| 230 | { |
||
| 231 | /** @var FrontendUser $feUser */ |
||
| 232 | $feUser = $this->security->getUser(); |
||
| 233 | |||
| 234 | $searches = []; |
||
| 235 | foreach ($feUser->getStoredSearches() as $search) { |
||
| 236 | $searches[] = [ |
||
| 237 | 'uid' => $search->getUid(), |
||
| 238 | 'name' => $search->getName(), |
||
| 239 | 'query' => $search->getQuery() |
||
| 240 | ]; |
||
| 241 | } |
||
| 242 | |||
| 243 | return json_encode($searches); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $searchTerm |
||
| 248 | * @param string $type |
||
| 249 | * @return false|string |
||
| 250 | */ |
||
| 251 | public function searchFisAction($searchTerm, $type = 'person') { |
||
| 252 | $fisUserDataService = new FisDataService(); |
||
| 253 | $methodName = 'search'.ucfirst($type).'Request'; |
||
| 254 | $result = $fisUserDataService->{$methodName}($searchTerm); |
||
| 255 | |||
| 256 | return json_encode($result); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $dataId |
||
| 261 | * @param int $groupId |
||
| 262 | * @param int $groupIndex |
||
| 263 | * @param int $fieldIndex |
||
| 264 | * @param int $pageId |
||
| 265 | * @param string $type |
||
| 266 | * @return false|string |
||
| 267 | */ |
||
| 268 | public function getFisDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') { |
||
| 269 | $fisDataService = new FisDataService(); |
||
| 270 | $methodName = 'get'.ucfirst($type).'Data'; |
||
| 271 | $fisData = $fisDataService->{$methodName}($dataId); |
||
| 272 | |||
| 273 | $result = $this->getApiMappingArray($groupId, $fisData, $groupIndex, $fieldIndex, $pageId, 'getFis'.ucfirst($type).'Mapping'); |
||
| 274 | |||
| 275 | return json_encode($result); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $searchTerm |
||
| 280 | * @param string $type |
||
| 281 | * @return false|string |
||
| 282 | */ |
||
| 283 | public function searchGndAction($searchTerm, $type = 'person') { |
||
| 284 | $gndUserDataService = new GndDataService(); |
||
| 285 | $methodName = 'search'.ucfirst($type).'Request'; |
||
| 286 | $result = $gndUserDataService->{$methodName}($searchTerm); |
||
| 287 | |||
| 288 | return json_encode($result); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $dataId |
||
| 293 | * @param int $groupId |
||
| 294 | * @param int $groupIndex |
||
| 295 | * @param int $fieldIndex |
||
| 296 | * @param int $pageId |
||
| 297 | * @param string $type |
||
| 298 | */ |
||
| 299 | public function getGndDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') { |
||
| 300 | $gndUserDataService = new GndDataService(); |
||
| 301 | $methodName = 'get'.ucfirst($type).'Data'; |
||
| 302 | $gndData = $gndUserDataService->{$methodName}($dataId); |
||
| 303 | |||
| 304 | $result = $this->getApiMappingArray($groupId, $gndData, $groupIndex, $fieldIndex, $pageId, 'getGnd'.ucfirst($type).'Mapping'); |
||
| 305 | |||
| 306 | return json_encode($result); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * search ROR API |
||
| 311 | * API is organisation only! |
||
| 312 | * @param string $searchTerm |
||
| 313 | * @return false|string |
||
| 314 | */ |
||
| 315 | public function searchRorAction($searchTerm) { |
||
| 316 | $rorUserDataService = new RorDataService(); |
||
| 317 | $result = $rorUserDataService->searchOrganisationRequest($searchTerm); |
||
| 318 | |||
| 319 | return json_encode($result); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $dataId |
||
| 324 | * @param int $groupId |
||
| 325 | * @param int $groupIndex |
||
| 326 | * @param int $fieldIndex |
||
| 327 | * @param int $pageId |
||
| 328 | */ |
||
| 329 | public function getRorDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) { |
||
| 330 | $rorUserDataService = new RorDataService(); |
||
| 331 | $rorData = $rorUserDataService->getOrganisationData($dataId); |
||
| 332 | |||
| 333 | $result = $this->getApiMappingArray($groupId, $rorData, $groupIndex, $fieldIndex, $pageId, 'getRorMapping'); |
||
| 334 | |||
| 335 | return json_encode($result); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $searchTerm |
||
| 340 | * @return false|string |
||
| 341 | */ |
||
| 342 | public function searchZdbAction($searchTerm) { |
||
| 343 | $zdbUserDataService = new ZdbDataService(); |
||
| 344 | $result = $zdbUserDataService->searchRequest($searchTerm); |
||
| 345 | |||
| 346 | return json_encode($result); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param string $dataId |
||
| 351 | * @param int $groupId |
||
| 352 | * @param int $groupIndex |
||
| 353 | * @param int $fieldIndex |
||
| 354 | * @param int $pageId |
||
| 355 | * @return false|string |
||
| 356 | */ |
||
| 357 | public function getZdbDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) { |
||
| 358 | $zdbDataService = new ZdbDataService(); |
||
| 359 | $zdbData = $zdbDataService->getDataRequest($dataId); |
||
| 360 | |||
| 361 | $result = $this->getApiMappingArray($groupId, $zdbData, $groupIndex, $fieldIndex, $pageId, 'getZdbMapping'); |
||
| 362 | |||
| 363 | return json_encode($result); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $searchTerm |
||
| 368 | */ |
||
| 369 | public function searchUnpaywallAction($searchTerm) { |
||
| 370 | $unpaywallUserDataService = new UnpaywallDataService(); |
||
| 371 | $result = $unpaywallUserDataService->searchRequest($searchTerm); |
||
| 372 | |||
| 373 | return json_encode($result); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $dataId |
||
| 378 | * @param int $groupId |
||
| 379 | * @param int $groupIndex |
||
| 380 | * @param int $fieldIndex |
||
| 381 | * @param int $pageId |
||
| 382 | * @return false|string |
||
| 383 | */ |
||
| 384 | public function getUnpaywallDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) { |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $searchTerm |
||
| 395 | * @param string $type |
||
| 396 | */ |
||
| 397 | public function searchOrcidAction($searchTerm, $type = 'person') { |
||
| 398 | $orcidUserDataService = new OrcidDataService(); |
||
| 399 | $methodName = 'search'.ucfirst($type).'Request'; |
||
| 400 | $result = $orcidUserDataService->{$methodName}($searchTerm); |
||
| 401 | |||
| 402 | return json_encode($result); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $dataId |
||
| 407 | * @param int $groupId |
||
| 408 | * @param int $groupIndex |
||
| 409 | * @param int $fieldIndex |
||
| 410 | * @param int $pageId |
||
| 411 | * @param string $type |
||
| 412 | * @return false|string |
||
| 413 | */ |
||
| 414 | public function getOrcidDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') { |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Preparing data from api and returning an array to identify specific field in frontend |
||
| 426 | * @param $groupId |
||
| 427 | * @param $data |
||
| 428 | * @param $groupIndex |
||
| 429 | * @param $fieldIndex |
||
| 430 | * @param $pageId |
||
| 431 | * @param $methodMappingName |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | public function getApiMappingArray($groupId, $data, $groupIndex, $fieldIndex, $pageId, $methodMappingName) { |
||
| 435 | // get mapping |
||
| 436 | /** @var MetadataGroup $group */ |
||
| 437 | $group = $this->metadataGroupRepository->findByUid($groupId); |
||
| 438 | |||
| 439 | foreach ($group->getChildren() as $key => $value) { |
||
| 440 | if (!empty($value->{$methodMappingName}())) { |
||
| 441 | // for configuration field1->field1a |
||
| 442 | $mappingPart = explode('->', $value->{$methodMappingName}()); |
||
| 443 | $apiData = ''; |
||
| 444 | $i = 0; |
||
| 445 | foreach ($mappingPart as $mapping) { |
||
| 446 | if ($i == 0) { |
||
| 447 | $apiData = $data->{$mapping}; |
||
| 448 | } else { |
||
| 449 | if (is_array($apiData)) { |
||
| 450 | foreach ($apiData as $fisArrayValue) { |
||
| 451 | $apiDataArray[] = $fisArrayValue->{$mapping}; |
||
| 452 | } |
||
| 453 | } else { |
||
| 454 | $apiData = $apiData->{$mapping}; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | $i++; |
||
| 458 | } |
||
| 459 | |||
| 460 | if (!empty($apiData) || !empty($apiDataArray)) { |
||
| 461 | if (!empty($apiDataArray)) { |
||
| 462 | foreach ($apiDataArray as $key => $apiDataValue) { |
||
| 463 | $result[$pageId . '-' . $groupId . '-' . $groupIndex . '-' . $value->getUid() . '-' . $key] = $apiDataValue; |
||
| 464 | } |
||
| 465 | $apiDataArray = []; |
||
| 466 | } else { |
||
| 467 | $result[$pageId . '-' . $groupId . '-' . $groupIndex . '-' . $value->getUid() . '-' . $fieldIndex] = $apiData; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | return $result; |
||
| 474 | } |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Selects or unselects an external metadata record to be imported later as a document. |
||
| 479 | * |
||
| 480 | * @param string $identifier |
||
| 481 | * @return bool |
||
| 482 | * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException |
||
| 483 | */ |
||
| 484 | public function toggleBulkImportRecordAction($identifier) |
||
| 485 | { |
||
| 486 | $externalMetaData = $this->externalMetadataRepository->findOneByPublicationIdentifier($identifier); |
||
| 487 | |||
| 488 | if ($externalMetaData) { |
||
| 489 | $this->externalMetadataRepository->remove($externalMetaData); |
||
| 490 | } else { |
||
| 491 | /** @var BulkImportSessionData $bulkImportSessionData */ |
||
| 492 | $bulkImportSessionData = $this->session->getBulkImportData(); |
||
| 493 | $currentResults = $bulkImportSessionData->getCurrentMetadataItems(); |
||
| 494 | if ($currentResults && is_array($currentResults)) { |
||
| 495 | $this->externalMetadataRepository->add($currentResults[$identifier]); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | return true; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Activates/deactivates the author search for the bulk import. |
||
| 504 | * |
||
| 505 | * @param string $apiName |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function toggleBulkImportAuthorSearchAction($apiName) |
||
| 509 | { |
||
| 510 | /** @var BulkImportSessionData $bulkImportData */ |
||
| 511 | $bulkImportSessionData = $this->session->getBulkImportData(); |
||
| 512 | |||
| 513 | switch ($apiName) { |
||
| 514 | case 'CrossRef': |
||
| 515 | $searchField = $bulkImportSessionData->getCrossRefSearchField(); |
||
| 516 | if ($searchField === 'author') { |
||
| 517 | $searchField = ''; |
||
| 518 | } else { |
||
| 519 | $searchField = 'author'; |
||
| 520 | } |
||
| 521 | $bulkImportSessionData->setCrossRefSearchField($searchField); |
||
| 522 | break; |
||
| 523 | case 'PubMed': |
||
| 524 | $searchField = $bulkImportSessionData->getPubMedSearchField(); |
||
| 525 | if ($searchField === 'author') { |
||
| 526 | $searchField = ''; |
||
| 527 | } else { |
||
| 528 | $searchField = 'author'; |
||
| 529 | } |
||
| 530 | $bulkImportSessionData->setPubMedSearchField($searchField); |
||
| 531 | break; |
||
| 532 | default: |
||
| 533 | return false; |
||
| 534 | } |
||
| 535 | |||
| 536 | $this->session->setBulkImportData($bulkImportSessionData); |
||
| 537 | |||
| 538 | return true; |
||
| 539 | } |
||
| 540 | |||
| 541 | public function initializeAction() |
||
| 542 | { |
||
| 543 | $this->authorizationChecker->denyAccessUnlessLoggedIn(); |
||
| 544 | |||
| 545 | parent::initializeAction(); |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param int $feUser |
||
| 550 | * @return false|string |
||
| 551 | */ |
||
| 552 | public function generateApiTokenAction($feUser) { |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param int $feUser |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | public function removeApiTokenAction($feUser) { |
||
| 580 | } |
||
| 581 | |||
| 582 | } |
||
| 583 | |||
| 584 | } |
||
| 585 |