Complex classes like ConnectionManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ConnectionManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class ConnectionManager implements SingletonInterface, ClearCacheActionsHookInterface |
||
| 51 | { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected static $connections = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gets a Solr connection. |
||
| 60 | * |
||
| 61 | * Instead of generating a new connection with each call, connections are |
||
| 62 | * kept and checked whether the requested connection already exists. If a |
||
| 63 | * connection already exists, it's reused. |
||
| 64 | * |
||
| 65 | * @param string $host Solr host (optional) |
||
| 66 | * @param int $port Solr port (optional) |
||
| 67 | * @param string $path Solr path (optional) |
||
| 68 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
| 69 | * @param string $username Solr user name (optional) |
||
| 70 | * @param string $password Solr password (optional) |
||
| 71 | * @return SolrService A solr connection. |
||
| 72 | */ |
||
| 73 | |||
| 74 | 60 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
| 75 | { |
||
| 76 | 60 | $connection = null; |
|
|
|
|||
| 77 | |||
| 78 | 60 | if (empty($host)) { |
|
| 79 | 1 | GeneralUtility::devLog( |
|
| 80 | 'ApacheSolrForTypo3\Solr\ConnectionManager::getConnection() called with empty |
||
| 81 | host parameter. Using configuration from TSFE, might be |
||
| 82 | inaccurate. Always provide a host or use the getConnectionBy* |
||
| 83 | 1 | methods.', |
|
| 84 | 1 | 'solr', |
|
| 85 | 2 |
||
| 86 | 1 | ); |
|
| 87 | |||
| 88 | 1 | $configuration = Util::getSolrConfiguration(); |
|
| 89 | 1 | $host = $configuration->getSolrHost(); |
|
| 90 | 1 | $port = $configuration->getSolrPort(); |
|
| 91 | 1 | $path = $configuration->getSolrPath(); |
|
| 92 | 1 | $scheme = $configuration->getSolrScheme(); |
|
| 93 | 1 | $username = $configuration->getSolrUsername(); |
|
| 94 | 37 | $password = $configuration->getSolrPassword(); |
|
| 95 | 1 | } |
|
| 96 | |||
| 97 | 60 | $connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password); |
|
| 98 | |||
| 99 | 60 | if (!isset(self::$connections[$connectionHash])) { |
|
| 100 | 60 | $connection = GeneralUtility::makeInstance(SolrService::class, $host, $port, $path, $scheme); |
|
| 101 | 59 | if ($username !== '') { |
|
| 102 | 54 | $connection->setAuthenticationCredentials($username, $password); |
|
| 103 | 54 | } |
|
| 104 | |||
| 105 | 59 | self::$connections[$connectionHash] = $connection; |
|
| 106 | 59 | } |
|
| 107 | |||
| 108 | 59 | return self::$connections[$connectionHash]; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Creates a solr configuration from the configuration array and returns it. |
||
| 113 | * |
||
| 114 | * @param array $config The solr configuration array |
||
| 115 | * @return SolrService |
||
| 116 | */ |
||
| 117 | 57 | protected function getConnectionFromConfiguration(array $config) |
|
| 118 | { |
||
| 119 | 57 | return $this->getConnection( |
|
| 120 | 57 | $config['solrHost'], |
|
| 121 | 57 | $config['solrPort'], |
|
| 122 | 57 | $config['solrPath'], |
|
| 123 | 57 | $config['solrScheme'], |
|
| 124 | 57 | $config['solrUsername'], |
|
| 125 | 57 | $config['solrPassword'] |
|
| 126 | 57 | ); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Gets a Solr configuration for a page ID. |
||
| 131 | * |
||
| 132 | * @param int $pageId A page ID. |
||
| 133 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 134 | * @param string $mount Comma list of MountPoint parameters |
||
| 135 | * @return array A solr configuration. |
||
| 136 | * @throws NoSolrConnectionFoundException |
||
| 137 | */ |
||
| 138 | 48 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Gets a Solr connection for a page ID. |
||
| 166 | * |
||
| 167 | * @param int $pageId A page ID. |
||
| 168 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 169 | * @param string $mount Comma list of MountPoint parameters |
||
| 170 | * @return SolrService A solr connection. |
||
| 171 | * @throws NoSolrConnectionFoundException |
||
| 172 | */ |
||
| 173 | 48 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Gets a Solr configuration for a root page ID. |
||
| 182 | * |
||
| 183 | * @param int $pageId A root page ID. |
||
| 184 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
| 185 | * @return array A solr configuration. |
||
| 186 | * @throws NoSolrConnectionFoundException |
||
| 187 | */ |
||
| 188 | 49 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
| 189 | { |
||
| 190 | 49 | $connectionKey = $pageId . '|' . $language; |
|
| 191 | 49 | $solrServers = $this->getAllConfigurations(); |
|
| 192 | |||
| 193 | 49 | if (isset($solrServers[$connectionKey])) { |
|
| 194 | 47 | $solrConfiguration = $solrServers[$connectionKey]; |
|
| 195 | 47 | } else { |
|
| 196 | /* @var $noSolrConnectionException NoSolrConnectionFoundException */ |
||
| 197 | 2 | $noSolrConnectionException = GeneralUtility::makeInstance( |
|
| 198 | 2 | NoSolrConnectionFoundException::class, |
|
| 199 | 'Could not find a Solr connection for root page [' |
||
| 200 | 2 | . $pageId . '] and language [' . $language . '].', |
|
| 201 | 1275396474 |
||
| 202 | 2 | ); |
|
| 203 | 2 | $noSolrConnectionException->setRootPageId($pageId); |
|
| 204 | 2 | $noSolrConnectionException->setLanguageId($language); |
|
| 205 | |||
| 206 | 2 | throw $noSolrConnectionException; |
|
| 207 | } |
||
| 208 | |||
| 209 | 47 | return $solrConfiguration; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Gets a Solr connection for a root page ID. |
||
| 214 | * |
||
| 215 | * @param int $pageId A root page ID. |
||
| 216 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 217 | * @return SolrService A solr connection. |
||
| 218 | * @throws NoSolrConnectionFoundException |
||
| 219 | */ |
||
| 220 | 5 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Gets all connection configurations found. |
||
| 230 | * |
||
| 231 | * @return array An array of connection configurations. |
||
| 232 | */ |
||
| 233 | 59 | public function getAllConfigurations() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Stores the connections in the registry. |
||
| 244 | * |
||
| 245 | * @param array $solrConfigurations |
||
| 246 | */ |
||
| 247 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Gets all connections found. |
||
| 256 | * |
||
| 257 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
| 258 | */ |
||
| 259 | 6 | public function getAllConnections() |
|
| 260 | { |
||
| 261 | 6 | $connections = []; |
|
| 262 | |||
| 263 | 6 | $solrConfigurations = $this->getAllConfigurations(); |
|
| 264 | 6 | foreach ($solrConfigurations as $solrConfiguration) { |
|
| 265 | 6 | $connections[] = $this->getConnectionFromConfiguration($solrConfiguration); |
|
| 266 | 6 | } |
|
| 267 | |||
| 268 | 6 | return $connections; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Gets all connection configurations for a given site. |
||
| 273 | * |
||
| 274 | * @param Site $site A TYPO3 site |
||
| 275 | * @return array An array of Solr connection configurations for a site |
||
| 276 | */ |
||
| 277 | 15 | public function getConfigurationsBySite(Site $site) |
|
| 278 | { |
||
| 279 | 15 | $solrConfigurations = []; |
|
| 280 | |||
| 281 | 15 | $allConfigurations = $this->getAllConfigurations(); |
|
| 282 | 15 | foreach ($allConfigurations as $configuration) { |
|
| 283 | 15 | if ($configuration['rootPageUid'] == $site->getRootPageId()) { |
|
| 284 | 15 | $solrConfigurations[] = $configuration; |
|
| 285 | 15 | } |
|
| 286 | 15 | } |
|
| 287 | |||
| 288 | 15 | return $solrConfigurations; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Gets all connections configured for a given site. |
||
| 293 | * |
||
| 294 | * @param Site $site A TYPO3 site |
||
| 295 | * @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
| 296 | */ |
||
| 297 | 5 | public function getConnectionsBySite(Site $site) |
|
| 298 | { |
||
| 299 | 5 | $connections = []; |
|
| 300 | |||
| 301 | 5 | $solrServers = $this->getConfigurationsBySite($site); |
|
| 302 | 5 | foreach ($solrServers as $solrServer) { |
|
| 303 | 5 | $connections[] = $this->getConnectionFromConfiguration($solrServer); |
|
| 304 | 5 | } |
|
| 305 | |||
| 306 | 5 | return $connections; |
|
| 307 | } |
||
| 308 | |||
| 309 | // updates |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
| 313 | * |
||
| 314 | * @param array $cacheActions Array of CacheMenuItems |
||
| 315 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
| 316 | */ |
||
| 317 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
| 318 | { |
||
| 319 | if ($GLOBALS['BE_USER']->isAdmin()) { |
||
| 320 | $title = 'Initialize Solr connections'; |
||
| 321 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 322 | |||
| 323 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
| 324 | $cacheActions[] = [ |
||
| 325 | 'id' => 'clearSolrConnectionCache', |
||
| 326 | 'title' => $title, |
||
| 327 | 'href' => $uriBuilder->buildUriFromRoute('ajax_solr_updateConnections'), |
||
| 328 | 'icon' => $iconFactory->getIcon('extensions-solr-module-initsolrconnections', Icon::SIZE_SMALL) |
||
| 329 | ]; |
||
| 330 | $optionValues[] = 'clearSolrConnectionCache'; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Updates the connections in the registry. |
||
| 336 | * |
||
| 337 | */ |
||
| 338 | 2 | public function updateConnections() |
|
| 339 | { |
||
| 340 | 2 | $solrConnections = $this->getConfiguredSolrConnections(); |
|
| 341 | 2 | $solrConnections = $this->filterDuplicateConnections($solrConnections); |
|
| 342 | |||
| 343 | 2 | if (!empty($solrConnections)) { |
|
| 344 | 2 | $this->setAllConfigurations($solrConnections); |
|
| 345 | 2 | } |
|
| 346 | 2 | } |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Entrypoint for the ajax request |
||
| 350 | */ |
||
| 351 | public function updateConnectionsInCacheMenu() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Updates the Solr connections for a specific root page ID / site. |
||
| 358 | * |
||
| 359 | * @param int $rootPageId A site root page id |
||
| 360 | */ |
||
| 361 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
| 362 | { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Finds the configured Solr connections. Also respects multi-site |
||
| 383 | * environments. |
||
| 384 | * |
||
| 385 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
||
| 386 | */ |
||
| 387 | 2 | protected function getConfiguredSolrConnections() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Gets the configured Solr connection for a specific root page and language ID. |
||
| 412 | * |
||
| 413 | * @param array $rootPage A root page record with at least title and uid |
||
| 414 | * @param int $languageId ID of a system language |
||
| 415 | * @return array A solr connection configuration. |
||
| 416 | */ |
||
| 417 | 3 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Gets the language name for a given language ID. |
||
| 472 | * |
||
| 473 | * @param int $languageId language ID |
||
| 474 | * @return string Language name |
||
| 475 | */ |
||
| 476 | 3 | protected function getLanguageName($languageId) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Creates a human readable label from the connections' configuration. |
||
| 497 | * |
||
| 498 | * @param array $connection Connection configuration |
||
| 499 | * @return string Connection label |
||
| 500 | */ |
||
| 501 | 3 | protected function buildConnectionLabel(array $connection) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Filters duplicate connections. When detecting the configured connections |
||
| 517 | * this is done with a little brute force by simply combining all root pages |
||
| 518 | * with all languages, this method filters out the duplicates. |
||
| 519 | * |
||
| 520 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
| 521 | * @return array An array with connections, no duplicates. |
||
| 522 | */ |
||
| 523 | 3 | protected function filterDuplicateConnections(array $connections) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Finds the system's configured languages. |
||
| 546 | * |
||
| 547 | * @return array An array of language IDs |
||
| 548 | */ |
||
| 549 | 3 | protected function getSystemLanguages() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
| 571 | * which usually is the case for pages with pid = 0. |
||
| 572 | * |
||
| 573 | * @return array An array of (partial) root page records, containing the uid and title fields |
||
| 574 | */ |
||
| 575 | 2 | protected function getRootPages() |
|
| 585 | } |
||
| 586 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.