| Total Complexity | 45 |
| Total Lines | 655 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like tx_dlf_em 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 tx_dlf_em, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class tx_dlf_em { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * This holds the current configuration |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | * @access protected |
||
| 27 | */ |
||
| 28 | protected $conf = array (); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * This holds the output ready to return |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | * @access protected |
||
| 35 | */ |
||
| 36 | protected $content = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check if a connection to a Solr server could be established with the given credentials. |
||
| 40 | * |
||
| 41 | * @access public |
||
| 42 | * |
||
| 43 | * @param array &$params: An array with parameters |
||
| 44 | * @param \TYPO3\CMS\Core\TypoScript\ConfigurationForm &$pObj: The parent object |
||
|
|
|||
| 45 | * |
||
| 46 | * @return string Message informing the user of success or failure |
||
| 47 | */ |
||
| 48 | public function checkSolrConnection(&$params, &$pObj) { |
||
| 49 | |||
| 50 | // Prepend username and password to hostname. |
||
| 51 | if (!empty($this->conf['solrUser']) && !empty($this->conf['solrPass'])) { |
||
| 52 | |||
| 53 | $host = $this->conf['solrUser'].':'.$this->conf['solrPass'].'@'.(!empty($this->conf['solrHost']) ? $this->conf['solrHost'] : 'localhost'); |
||
| 54 | |||
| 55 | } else { |
||
| 56 | |||
| 57 | $host = (!empty($this->conf['solrHost']) ? $this->conf['solrHost'] : 'localhost'); |
||
| 58 | |||
| 59 | } |
||
| 60 | |||
| 61 | // Set port if not set. |
||
| 62 | $port = (!empty($this->conf['solrPort']) ? \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->conf['solrPort'], 0, 65535, 8180) : 8180); |
||
| 63 | |||
| 64 | // Trim path and append trailing slash. |
||
| 65 | $path = (!empty($this->conf['solrPath']) ? trim($this->conf['solrPath'], '/').'/' : ''); |
||
| 66 | |||
| 67 | // Build request URI. |
||
| 68 | $url = 'http://'.$host.':'.$port.'/'.$path.'admin/cores?wt=xml'; |
||
| 69 | |||
| 70 | $context = stream_context_create(array ( |
||
| 71 | 'http' => array ( |
||
| 72 | 'method' => 'GET', |
||
| 73 | 'user_agent' => (!empty($this->conf['useragent']) ? $this->conf['useragent'] : ini_get('user_agent')) |
||
| 74 | ) |
||
| 75 | )); |
||
| 76 | |||
| 77 | // Try to connect to Solr server. |
||
| 78 | $response = @simplexml_load_string(file_get_contents($url, FALSE, $context)); |
||
| 79 | |||
| 80 | // Check status code. |
||
| 81 | if ($response) { |
||
| 82 | |||
| 83 | $status = $response->xpath('//lst[@name="responseHeader"]/int[@name="status"]'); |
||
| 84 | |||
| 85 | if (is_array($status)) { |
||
| 86 | |||
| 87 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 88 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 89 | sprintf($GLOBALS['LANG']->getLL('solr.status'), (string) $status[0]), |
||
| 90 | $GLOBALS['LANG']->getLL('solr.connected'), |
||
| 91 | ($status[0] == 0 ? \TYPO3\CMS\Core\Messaging\FlashMessage::OK : \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING), |
||
| 92 | FALSE |
||
| 93 | ); |
||
| 94 | |||
| 95 | $this->content .= $message->render(); |
||
| 96 | |||
| 97 | return $this->content; |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 104 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 105 | sprintf($GLOBALS['LANG']->getLL('solr.error'), $url), |
||
| 106 | $GLOBALS['LANG']->getLL('solr.notConnected'), |
||
| 107 | \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING, |
||
| 108 | FALSE |
||
| 109 | ); |
||
| 110 | |||
| 111 | $this->content .= $message->render(); |
||
| 112 | |||
| 113 | return $this->content; |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Make sure a backend user exists and is configured properly. |
||
| 119 | * |
||
| 120 | * @access protected |
||
| 121 | * |
||
| 122 | * @param boolean $checkOnly: Just check the user or change it, too? |
||
| 123 | * @param integer $groupUid: UID of the corresponding usergroup |
||
| 124 | * |
||
| 125 | * @return integer UID of user or 0 if something is wrong |
||
| 126 | */ |
||
| 127 | protected function checkCliUser($checkOnly, $groupUid) { |
||
| 128 | |||
| 129 | // Set default return value. |
||
| 130 | $usrUid = 0; |
||
| 131 | |||
| 132 | // Check if user "_cli_dlf" exists, is no admin and is not disabled. |
||
| 133 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 134 | 'uid,admin,usergroup', |
||
| 135 | 'be_users', |
||
| 136 | 'username='.$GLOBALS['TYPO3_DB']->fullQuoteStr('_cli_dlf', 'be_users').\TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('be_users') |
||
| 137 | ); |
||
| 138 | |||
| 139 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result) > 0) { |
||
| 140 | |||
| 141 | $resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result); |
||
| 142 | |||
| 143 | // Explode comma-separated list. |
||
| 144 | $resArray['usergroup'] = explode(',', $resArray['usergroup']); |
||
| 145 | |||
| 146 | // Check if user is not disabled. |
||
| 147 | $result2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 148 | '1', |
||
| 149 | 'be_users', |
||
| 150 | 'uid='.intval($resArray['uid']).\TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields('be_users') |
||
| 151 | ); |
||
| 152 | |||
| 153 | // Check if user is configured properly. |
||
| 154 | if (count(array_diff(array ($groupUid), $resArray['usergroup'])) == 0 |
||
| 155 | && !$resArray['admin'] |
||
| 156 | && $GLOBALS['TYPO3_DB']->sql_num_rows($result2) > 0) { |
||
| 157 | |||
| 158 | $usrUid = $resArray['uid']; |
||
| 159 | |||
| 160 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 161 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 162 | $GLOBALS['LANG']->getLL('cliUserGroup.usrOkayMsg'), |
||
| 163 | $GLOBALS['LANG']->getLL('cliUserGroup.usrOkay'), |
||
| 164 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 165 | FALSE |
||
| 166 | ); |
||
| 167 | |||
| 168 | } else { |
||
| 169 | |||
| 170 | if (!$checkOnly && $groupUid) { |
||
| 171 | |||
| 172 | // Keep exisiting values and add the new ones. |
||
| 173 | $usergroup = array_unique(array_merge(array ($groupUid), $resArray['usergroup'])); |
||
| 174 | |||
| 175 | // Try to configure user. |
||
| 176 | $data = array (); |
||
| 177 | $data['be_users'][$resArray['uid']] = array ( |
||
| 178 | 'admin' => 0, |
||
| 179 | 'usergroup' => implode(',', $usergroup), |
||
| 180 | $GLOBALS['TCA']['be_users']['ctrl']['enablecolumns']['disabled'] => 0, |
||
| 181 | $GLOBALS['TCA']['be_users']['ctrl']['enablecolumns']['starttime'] => 0, |
||
| 182 | $GLOBALS['TCA']['be_users']['ctrl']['enablecolumns']['endtime'] => 0 |
||
| 183 | ); |
||
| 184 | |||
| 185 | tx_dlf_helper::processDBasAdmin($data); |
||
| 186 | |||
| 187 | // Check if configuration was successful. |
||
| 188 | if ($this->checkCliUser(TRUE, $groupUid)) { |
||
| 189 | |||
| 190 | $usrUid = $resArray['uid']; |
||
| 191 | |||
| 192 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 193 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 194 | $GLOBALS['LANG']->getLL('cliUserGroup.usrConfiguredMsg'), |
||
| 195 | $GLOBALS['LANG']->getLL('cliUserGroup.usrConfigured'), |
||
| 196 | \TYPO3\CMS\Core\Messaging\FlashMessage::INFO, |
||
| 197 | FALSE |
||
| 198 | ); |
||
| 199 | |||
| 200 | } else { |
||
| 201 | |||
| 202 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 203 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 204 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotConfiguredMsg'), |
||
| 205 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotConfigured'), |
||
| 206 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 207 | FALSE |
||
| 208 | ); |
||
| 209 | |||
| 210 | } |
||
| 211 | |||
| 212 | } else { |
||
| 213 | |||
| 214 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 215 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 216 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotConfiguredMsg'), |
||
| 217 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotConfigured'), |
||
| 218 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 219 | FALSE |
||
| 220 | ); |
||
| 221 | |||
| 222 | } |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | } else { |
||
| 227 | |||
| 228 | if (!$checkOnly && $groupUid) { |
||
| 229 | |||
| 230 | // Try to create user. |
||
| 231 | $tempUid = uniqid('NEW'); |
||
| 232 | |||
| 233 | $data = array (); |
||
| 234 | $data['be_users'][$tempUid] = array ( |
||
| 235 | 'pid' => 0, |
||
| 236 | 'username' => '_cli_dlf', |
||
| 237 | 'password' => md5($tempUid), |
||
| 238 | 'realName' => $GLOBALS['LANG']->getLL('cliUserGroup.usrRealName'), |
||
| 239 | 'usergroup' => intval($groupUid) |
||
| 240 | ); |
||
| 241 | |||
| 242 | $substUid = tx_dlf_helper::processDBasAdmin($data); |
||
| 243 | |||
| 244 | // Check if creation was successful. |
||
| 245 | if (!empty($substUid[$tempUid])) { |
||
| 246 | |||
| 247 | $usrUid = $substUid[$tempUid]; |
||
| 248 | |||
| 249 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 250 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 251 | $GLOBALS['LANG']->getLL('cliUserGroup.usrCreatedMsg'), |
||
| 252 | $GLOBALS['LANG']->getLL('cliUserGroup.usrCreated'), |
||
| 253 | \TYPO3\CMS\Core\Messaging\FlashMessage::INFO, |
||
| 254 | FALSE |
||
| 255 | ); |
||
| 256 | |||
| 257 | } else { |
||
| 258 | |||
| 259 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 260 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 261 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotCreatedMsg'), |
||
| 262 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotCreated'), |
||
| 263 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 264 | FALSE |
||
| 265 | ); |
||
| 266 | |||
| 267 | } |
||
| 268 | |||
| 269 | } else { |
||
| 270 | |||
| 271 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 272 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 273 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotCreatedMsg'), |
||
| 274 | $GLOBALS['LANG']->getLL('cliUserGroup.usrNotCreated'), |
||
| 275 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 276 | FALSE |
||
| 277 | ); |
||
| 278 | |||
| 279 | } |
||
| 280 | |||
| 281 | } |
||
| 282 | |||
| 283 | $this->content = $message->render(); |
||
| 284 | |||
| 285 | return $usrUid; |
||
| 286 | |||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Make sure a backend usergroup exists and is configured properly. |
||
| 291 | * |
||
| 292 | * @access protected |
||
| 293 | * |
||
| 294 | * @param boolean $checkOnly: Just check the usergroup or change it, too? |
||
| 295 | * @param array $settings: Array with default settings |
||
| 296 | * |
||
| 297 | * @return integer UID of usergroup or 0 if something is wrong |
||
| 298 | */ |
||
| 299 | protected function checkCliGroup($checkOnly, $settings = array ()) { |
||
| 501 | |||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Make sure a CLI user and group exist. |
||
| 506 | * |
||
| 507 | * @access public |
||
| 508 | * |
||
| 509 | * @param array &$params: An array with parameters |
||
| 510 | * @param \TYPO3\CMS\Core\TypoScript\ConfigurationForm &$pObj: The parent object |
||
| 511 | * |
||
| 512 | * @return string Message informing the user of success or failure |
||
| 513 | */ |
||
| 514 | public function checkCliUserGroup(&$params, &$pObj) { |
||
| 515 | |||
| 516 | // Check if usergroup "_cli_dlf" exists and is configured properly. |
||
| 517 | $groupUid = $this->checkCliGroup(empty($this->conf['makeCliUserGroup'])); |
||
| 518 | |||
| 519 | // Save output because it will be overwritten by the user check method. |
||
| 520 | $content = $this->content; |
||
| 521 | |||
| 522 | // Check if user "_cli_dlf" exists and is configured properly. |
||
| 523 | $userUid = $this->checkCliUser(empty($this->conf['makeCliUserGroup']), $groupUid); |
||
| 524 | |||
| 525 | // Merge output from usergroup and user checks. |
||
| 526 | $this->content .= $content; |
||
| 527 | |||
| 528 | // Check if CLI dispatcher is executable. |
||
| 529 | if (is_executable(PATH_typo3.'cli_dispatch.phpsh')) { |
||
| 530 | |||
| 531 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 532 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 533 | $GLOBALS['LANG']->getLL('cliUserGroup.cliOkayMsg'), |
||
| 534 | $GLOBALS['LANG']->getLL('cliUserGroup.cliOkay'), |
||
| 535 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 536 | FALSE |
||
| 537 | ); |
||
| 538 | |||
| 539 | } else { |
||
| 540 | |||
| 541 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 542 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 543 | $GLOBALS['LANG']->getLL('cliUserGroup.cliNotOkayMsg'), |
||
| 544 | $GLOBALS['LANG']->getLL('cliUserGroup.cliNotOkay'), |
||
| 545 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 546 | FALSE |
||
| 547 | ); |
||
| 548 | |||
| 549 | } |
||
| 550 | |||
| 551 | $this->content .= $message->render(); |
||
| 552 | |||
| 553 | return $this->content; |
||
| 554 | |||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Make sure the essential namespaces are defined. |
||
| 559 | * |
||
| 560 | * @access public |
||
| 561 | * |
||
| 562 | * @param array &$params: An array with parameters |
||
| 563 | * @param \TYPO3\CMS\Core\TypoScript\ConfigurationForm &$pObj: The parent object |
||
| 564 | * |
||
| 565 | * @return string Message informing the user of success or failure |
||
| 566 | */ |
||
| 567 | public function checkMetadataFormats(&$params, &$pObj) { |
||
| 568 | |||
| 569 | $nsDefined = array ( |
||
| 570 | 'MODS' => FALSE, |
||
| 571 | 'TEIHDR' => FALSE |
||
| 572 | ); |
||
| 573 | |||
| 574 | // Check if formats "MODS" and "TEIHDR" exist. |
||
| 575 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 576 | 'type', |
||
| 577 | 'tx_dlf_formats', |
||
| 578 | '(type='.$GLOBALS['TYPO3_DB']->fullQuoteStr('MODS', 'tx_dlf_formats').' OR type='.$GLOBALS['TYPO3_DB']->fullQuoteStr('TEIHDR', 'tx_dlf_formats').')'.tx_dlf_helper::whereClause('tx_dlf_formats') |
||
| 579 | ); |
||
| 580 | |||
| 581 | while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) { |
||
| 582 | |||
| 583 | $nsDefined[$resArray['type']] = TRUE; |
||
| 584 | |||
| 585 | } |
||
| 586 | |||
| 587 | // Build data array. |
||
| 588 | $data = array (); |
||
| 589 | |||
| 590 | // Add MODS namespace. |
||
| 591 | if (!$nsDefined['MODS']) { |
||
| 592 | |||
| 593 | $data['tx_dlf_formats'][uniqid('NEW')] = array ( |
||
| 594 | 'pid' => 0, |
||
| 595 | 'type' => 'MODS', |
||
| 596 | 'root' => 'mods', |
||
| 597 | 'namespace' => 'http://www.loc.gov/mods/v3', |
||
| 598 | 'class' => 'tx_dlf_mods' |
||
| 599 | ); |
||
| 600 | |||
| 601 | } |
||
| 602 | |||
| 603 | // Add TEIHDR namespace. |
||
| 604 | if (!$nsDefined['TEIHDR']) { |
||
| 605 | |||
| 606 | $data['tx_dlf_formats'][uniqid('NEW')] = array ( |
||
| 607 | 'pid' => 0, |
||
| 608 | 'type' => 'TEIHDR', |
||
| 609 | 'root' => 'teiHeader', |
||
| 610 | 'namespace' => 'http://www.tei-c.org/ns/1.0', |
||
| 611 | 'class' => 'tx_dlf_teihdr' |
||
| 612 | ); |
||
| 613 | |||
| 614 | } |
||
| 615 | |||
| 616 | if (!empty($data)) { |
||
| 617 | |||
| 618 | // Process changes. |
||
| 619 | $substUid = tx_dlf_helper::processDBasAdmin($data); |
||
| 620 | |||
| 621 | if (!empty($substUid)) { |
||
| 622 | |||
| 623 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 624 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 625 | $GLOBALS['LANG']->getLL('metadataFormats.nsCreatedMsg'), |
||
| 626 | $GLOBALS['LANG']->getLL('metadataFormats.nsCreated'), |
||
| 627 | \TYPO3\CMS\Core\Messaging\FlashMessage::INFO, |
||
| 628 | FALSE |
||
| 629 | ); |
||
| 630 | |||
| 631 | } else { |
||
| 632 | |||
| 633 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 634 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 635 | $GLOBALS['LANG']->getLL('metadataFormats.nsNotCreatedMsg'), |
||
| 636 | $GLOBALS['LANG']->getLL('metadataFormats.nsNotCreated'), |
||
| 637 | \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, |
||
| 638 | FALSE |
||
| 639 | ); |
||
| 640 | |||
| 641 | } |
||
| 642 | |||
| 643 | } else { |
||
| 644 | |||
| 645 | $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( |
||
| 646 | 'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', |
||
| 647 | $GLOBALS['LANG']->getLL('metadataFormats.nsOkayMsg'), |
||
| 648 | $GLOBALS['LANG']->getLL('metadataFormats.nsOkay'), |
||
| 649 | \TYPO3\CMS\Core\Messaging\FlashMessage::OK, |
||
| 650 | FALSE |
||
| 651 | ); |
||
| 652 | |||
| 653 | } |
||
| 654 | |||
| 655 | $this->content .= $message->render(); |
||
| 656 | |||
| 657 | return $this->content; |
||
| 658 | |||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * This is the constructor. |
||
| 663 | * |
||
| 664 | * @access public |
||
| 665 | * |
||
| 666 | * @return void |
||
| 667 | */ |
||
| 668 | public function __construct() { |
||
| 675 | |||
| 676 | } |
||
| 677 | |||
| 678 | } |
||
| 679 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths