We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 60 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DataHandler 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 DataHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class DataHandler implements LoggerAwareInterface |
||
| 38 | { |
||
| 39 | use LoggerAwareTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var DocumentRepository |
||
| 43 | */ |
||
| 44 | protected $documentRepository; |
||
| 45 | |||
| 46 | protected function getDocumentRepository() |
||
| 47 | { |
||
| 48 | if ($this->documentRepository === null) { |
||
| 49 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
| 50 | $this->documentRepository = $objectManager->get(DocumentRepository::class); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this->documentRepository; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Field post-processing hook for the process_datamap() method. |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * |
||
| 61 | * @param string $status: 'new' or 'update' |
||
| 62 | * @param string $table: The destination table |
||
| 63 | * @param int $id: The uid of the record |
||
| 64 | * @param array &$fieldArray: Array of field values |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray) |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * After database operations hook for the process_datamap() method. |
||
| 188 | * |
||
| 189 | * @access public |
||
| 190 | * |
||
| 191 | * @param string $status: 'new' or 'update' |
||
| 192 | * @param string $table: The destination table |
||
| 193 | * @param int $id: The uid of the record |
||
| 194 | * @param array &$fieldArray: Array of field values |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray) |
||
| 199 | { |
||
| 200 | if ($status == 'update') { |
||
| 201 | switch ($table) { |
||
| 202 | // After database operations for table "tx_dlf_documents". |
||
| 203 | case 'tx_dlf_documents': |
||
| 204 | // Delete/reindex document in Solr if "hidden" status or collections have changed. |
||
| 205 | if ( |
||
| 206 | isset($fieldArray['hidden']) |
||
| 207 | || isset($fieldArray['collections']) |
||
| 208 | ) { |
||
| 209 | // Get Solr-Core. |
||
| 210 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 211 | ->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
| 212 | |||
| 213 | $result = $queryBuilder |
||
| 214 | ->select( |
||
| 215 | 'tx_dlf_solrcores.uid AS core', |
||
| 216 | 'tx_dlf_solrcores.index_name', |
||
| 217 | 'tx_dlf_documents_join.hidden AS hidden' |
||
| 218 | ) |
||
| 219 | ->innerJoin( |
||
| 220 | 'tx_dlf_solrcores', |
||
| 221 | 'tx_dlf_documents', |
||
| 222 | 'tx_dlf_documents_join', |
||
| 223 | $queryBuilder->expr()->eq( |
||
| 224 | 'tx_dlf_documents_join.solrcore', |
||
| 225 | 'tx_dlf_solrcores.uid' |
||
| 226 | ) |
||
| 227 | ) |
||
| 228 | ->from('tx_dlf_solrcores') |
||
| 229 | ->where( |
||
| 230 | $queryBuilder->expr()->eq( |
||
| 231 | 'tx_dlf_documents_join.uid', |
||
| 232 | intval($id) |
||
| 233 | ) |
||
| 234 | ) |
||
| 235 | ->setMaxResults(1) |
||
| 236 | ->execute(); |
||
| 237 | |||
| 238 | if ($resArray = $result->fetch()) { |
||
| 239 | if ($resArray['hidden']) { |
||
| 240 | // Establish Solr connection. |
||
| 241 | $solr = Solr::getInstance($resArray['core']); |
||
| 242 | if ($solr->ready) { |
||
| 243 | // Delete Solr document. |
||
| 244 | $updateQuery = $solr->service->createUpdate(); |
||
| 245 | $updateQuery->addDeleteQuery('uid:' . $id); |
||
| 246 | $updateQuery->addCommit(); |
||
| 247 | $solr->service->update($updateQuery); |
||
| 248 | } |
||
| 249 | } else { |
||
| 250 | // Reindex document. |
||
| 251 | $document = $this->getDocumentRepository()->findByUid($id); |
||
| 252 | $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true); |
||
| 253 | if ($document !== null && $doc !== null) { |
||
| 254 | $document->setDoc($doc); |
||
| 255 | Indexer::add($document); |
||
| 256 | } else { |
||
| 257 | $this->logger->error('Failed to re-index document with UID ' . $id); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | break; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Post-processing hook for the process_cmdmap() method. |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * |
||
| 272 | * @param string $command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete' |
||
| 273 | * @param string $table: The destination table |
||
| 274 | * @param int $id: The uid of the record |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | public function processCmdmap_postProcess($command, $table, $id) |
||
| 391 | } |
||
| 392 | } |
||
| 396 |