We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 58 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 | * @Inject |
||
| 43 | * @var DocumentRepository |
||
| 44 | */ |
||
| 45 | protected $documentRepository; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Field post-processing hook for the process_datamap() method. |
||
| 49 | * |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @param string $status: 'new' or 'update' |
||
| 53 | * @param string $table: The destination table |
||
| 54 | * @param int $id: The uid of the record |
||
| 55 | * @param array &$fieldArray: Array of field values |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray) |
||
| 60 | { |
||
| 61 | if ($status == 'new') { |
||
| 62 | switch ($table) { |
||
| 63 | // Field post-processing for table "tx_dlf_documents". |
||
| 64 | case 'tx_dlf_documents': |
||
| 65 | // Set sorting field if empty. |
||
| 66 | if ( |
||
| 67 | empty($fieldArray['title_sorting']) |
||
| 68 | && !empty($fieldArray['title']) |
||
| 69 | ) { |
||
| 70 | $fieldArray['title_sorting'] = $fieldArray['title']; |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | // Field post-processing for table "tx_dlf_metadata". |
||
| 74 | case 'tx_dlf_metadata': |
||
| 75 | // Store field in index if it should appear in lists. |
||
| 76 | if (!empty($fieldArray['is_listed'])) { |
||
| 77 | $fieldArray['index_stored'] = 1; |
||
| 78 | } |
||
| 79 | // Index field in index if it should be used for auto-completion. |
||
| 80 | if (!empty($fieldArray['index_autocomplete'])) { |
||
| 81 | $fieldArray['index_indexed'] = 1; |
||
| 82 | } |
||
| 83 | // Field post-processing for tables "tx_dlf_metadata", "tx_dlf_collections", "tx_dlf_libraries" and "tx_dlf_structures". |
||
| 84 | case 'tx_dlf_collections': |
||
| 85 | case 'tx_dlf_libraries': |
||
| 86 | case 'tx_dlf_structures': |
||
| 87 | // Set label as index name if empty. |
||
| 88 | if ( |
||
| 89 | empty($fieldArray['index_name']) |
||
| 90 | && !empty($fieldArray['label']) |
||
| 91 | ) { |
||
| 92 | $fieldArray['index_name'] = $fieldArray['label']; |
||
| 93 | } |
||
| 94 | // Set index name as label if empty. |
||
| 95 | if ( |
||
| 96 | empty($fieldArray['label']) |
||
| 97 | && !empty($fieldArray['index_name']) |
||
| 98 | ) { |
||
| 99 | $fieldArray['label'] = $fieldArray['index_name']; |
||
| 100 | } |
||
| 101 | // Ensure that index names don't get mixed up with sorting values. |
||
| 102 | if (substr($fieldArray['index_name'], -8) == '_sorting') { |
||
| 103 | $fieldArray['index_name'] .= '0'; |
||
| 104 | } |
||
| 105 | break; |
||
| 106 | // Field post-processing for table "tx_dlf_solrcores". |
||
| 107 | case 'tx_dlf_solrcores': |
||
| 108 | // Create new Solr core. |
||
| 109 | $fieldArray['index_name'] = Solr::createCore($fieldArray['index_name']); |
||
| 110 | if (empty($fieldArray['index_name'])) { |
||
| 111 | $this->logger->error('Could not create new Apache Solr core'); |
||
| 112 | } |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } elseif ($status == 'update') { |
||
| 116 | switch ($table) { |
||
| 117 | // Field post-processing for table "tx_dlf_metadata". |
||
| 118 | case 'tx_dlf_metadata': |
||
| 119 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 120 | ->getQueryBuilderForTable($table); |
||
| 121 | |||
| 122 | // Store field in index if it should appear in lists. |
||
| 123 | if (!empty($fieldArray['is_listed'])) { |
||
| 124 | $fieldArray['index_stored'] = 1; |
||
| 125 | } |
||
| 126 | if ( |
||
| 127 | isset($fieldArray['index_stored']) |
||
| 128 | && $fieldArray['index_stored'] == 0 |
||
| 129 | && !isset($fieldArray['is_listed']) |
||
| 130 | ) { |
||
| 131 | // Get current configuration. |
||
| 132 | $result = $queryBuilder |
||
| 133 | ->select($table . '.is_listed AS is_listed') |
||
| 134 | ->from($table) |
||
| 135 | ->where( |
||
| 136 | $queryBuilder->expr()->eq($table . '.uid', intval($id)), |
||
| 137 | Helper::whereExpression($table) |
||
| 138 | ) |
||
| 139 | ->setMaxResults(1) |
||
| 140 | ->execute(); |
||
| 141 | |||
| 142 | if ($resArray = $result->fetch()) { |
||
| 143 | // Reset storing to current. |
||
| 144 | $fieldArray['index_stored'] = $resArray['is_listed']; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | // Index field in index if it should be used for auto-completion. |
||
| 148 | if (!empty($fieldArray['index_autocomplete'])) { |
||
| 149 | $fieldArray['index_indexed'] = 1; |
||
| 150 | } |
||
| 151 | if ( |
||
| 152 | isset($fieldArray['index_indexed']) |
||
| 153 | && $fieldArray['index_indexed'] == 0 |
||
| 154 | && !isset($fieldArray['index_autocomplete']) |
||
| 155 | ) { |
||
| 156 | // Get current configuration. |
||
| 157 | $result = $queryBuilder |
||
| 158 | ->select($table . '.index_autocomplete AS index_autocomplete') |
||
| 159 | ->from($table) |
||
| 160 | ->where( |
||
| 161 | $queryBuilder->expr()->eq($table . '.uid', intval($id)), |
||
| 162 | Helper::whereExpression($table) |
||
| 163 | ) |
||
| 164 | ->setMaxResults(1) |
||
| 165 | ->execute(); |
||
| 166 | |||
| 167 | if ($resArray = $result->fetch()) { |
||
| 168 | // Reset indexing to current. |
||
| 169 | $fieldArray['index_indexed'] = $resArray['index_autocomplete']; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * After database operations hook for the process_datamap() method. |
||
| 179 | * |
||
| 180 | * @access public |
||
| 181 | * |
||
| 182 | * @param string $status: 'new' or 'update' |
||
| 183 | * @param string $table: The destination table |
||
| 184 | * @param int $id: The uid of the record |
||
| 185 | * @param array &$fieldArray: Array of field values |
||
| 186 | * |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray) |
||
| 190 | { |
||
| 191 | if ($status == 'update') { |
||
| 192 | switch ($table) { |
||
| 193 | // After database operations for table "tx_dlf_documents". |
||
| 194 | case 'tx_dlf_documents': |
||
| 195 | // Delete/reindex document in Solr if "hidden" status or collections have changed. |
||
| 196 | if ( |
||
| 197 | isset($fieldArray['hidden']) |
||
| 198 | || isset($fieldArray['collections']) |
||
| 199 | ) { |
||
| 200 | // Get Solr-Core. |
||
| 201 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 202 | ->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
| 203 | |||
| 204 | $result = $queryBuilder |
||
| 205 | ->select( |
||
| 206 | 'tx_dlf_solrcores.uid AS core', |
||
| 207 | 'tx_dlf_solrcores.index_name', |
||
| 208 | 'tx_dlf_documents_join.hidden AS hidden' |
||
| 209 | ) |
||
| 210 | ->innerJoin( |
||
| 211 | 'tx_dlf_solrcores', |
||
| 212 | 'tx_dlf_documents', |
||
| 213 | 'tx_dlf_documents_join', |
||
| 214 | $queryBuilder->expr()->eq( |
||
| 215 | 'tx_dlf_documents_join.solrcore', |
||
| 216 | 'tx_dlf_solrcores.uid' |
||
| 217 | ) |
||
| 218 | ) |
||
| 219 | ->from('tx_dlf_solrcores') |
||
| 220 | ->where( |
||
| 221 | $queryBuilder->expr()->eq( |
||
| 222 | 'tx_dlf_documents_join.uid', |
||
| 223 | intval($id) |
||
| 224 | ) |
||
| 225 | ) |
||
| 226 | ->setMaxResults(1) |
||
| 227 | ->execute(); |
||
| 228 | |||
| 229 | if ($resArray = $result->fetch()) { |
||
| 230 | if ($resArray['hidden']) { |
||
| 231 | // Establish Solr connection. |
||
| 232 | $solr = Solr::getInstance($resArray['core']); |
||
| 233 | if ($solr->ready) { |
||
| 234 | // Delete Solr document. |
||
| 235 | $updateQuery = $solr->service->createUpdate(); |
||
| 236 | $updateQuery->addDeleteQuery('uid:' . $id); |
||
| 237 | $updateQuery->addCommit(); |
||
| 238 | $solr->service->update($updateQuery); |
||
| 239 | } |
||
| 240 | } else { |
||
| 241 | // Reindex document. |
||
| 242 | $document = $this->documentRepository->findByUid($id); |
||
| 243 | $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true); |
||
| 244 | if ($document !== null && $doc !== null) { |
||
| 245 | $document->setDoc($doc); |
||
| 246 | Indexer::add($document); |
||
| 247 | } else { |
||
| 248 | $this->logger->error('Failed to re-index document with UID ' . $id); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | break; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Post-processing hook for the process_cmdmap() method. |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * |
||
| 263 | * @param string $command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete' |
||
| 264 | * @param string $table: The destination table |
||
| 265 | * @param int $id: The uid of the record |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | public function processCmdmap_postProcess($command, $table, $id) |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 387 |