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 |
||
| 48 | class ConnectionManager implements SingletonInterface, ClearCacheActionsHookInterface |
||
| 49 | { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected static $connections = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository |
||
| 58 | */ |
||
| 59 | protected $systemLanguageRepository; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 63 | */ |
||
| 64 | protected $logger = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var PagesRepositoryAtExtSolr |
||
| 68 | */ |
||
| 69 | protected $pagesRepositoryAtExtSolr; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param SystemLanguageRepository $systemLanguageRepository |
||
| 73 | * @param PagesRepositoryAtExtSolr|null $pagesRepositoryAtExtSolr |
||
| 74 | * @param SolrLogManager $solrLogManager |
||
| 75 | */ |
||
| 76 | 101 | public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SolrLogManager $solrLogManager = null) |
|
| 77 | { |
||
| 78 | 101 | $this->systemLanguageRepository = isset($systemLanguageRepository) ? $systemLanguageRepository : GeneralUtility::makeInstance(SystemLanguageRepository::class); |
|
| 79 | 101 | $this->pagesRepositoryAtExtSolr = isset($pagesRepositoryAtExtSolr) ? $pagesRepositoryAtExtSolr : GeneralUtility::makeInstance(PagesRepositoryAtExtSolr::class); |
|
| 80 | 101 | $this->logger = isset($solrLogManager) ? $solrLogManager : GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
| 81 | 101 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Gets a Solr connection. |
||
| 85 | * |
||
| 86 | * Instead of generating a new connection with each call, connections are |
||
| 87 | * kept and checked whether the requested connection already exists. If a |
||
| 88 | * connection already exists, it's reused. |
||
| 89 | * |
||
| 90 | * @param string $host Solr host (optional) |
||
| 91 | * @param int $port Solr port (optional) |
||
| 92 | * @param string $path Solr path (optional) |
||
| 93 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
| 94 | * @param string $username Solr user name (optional) |
||
| 95 | * @param string $password Solr password (optional) |
||
| 96 | * @return SolrService A solr connection. |
||
| 97 | */ |
||
| 98 | 94 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
| 99 | { |
||
| 100 | 94 | if (empty($host)) { |
|
| 101 | 1 | $this->logger->log( |
|
| 102 | 1 | SolrLogManager::WARNING, |
|
| 103 | 1 | 'ApacheSolrForTypo3\Solr\ConnectionManager::getConnection() called with empty host parameter. Using configuration from TSFE, might be inaccurate. Always provide a host or use the getConnectionBy* methods.' |
|
| 104 | ); |
||
| 105 | |||
| 106 | 1 | $configuration = Util::getSolrConfiguration(); |
|
| 107 | 1 | $host = $configuration->getSolrHost(); |
|
| 108 | 1 | $port = $configuration->getSolrPort(); |
|
| 109 | 1 | $path = $configuration->getSolrPath(); |
|
| 110 | 1 | $scheme = $configuration->getSolrScheme(); |
|
| 111 | 1 | $username = $configuration->getSolrUsername(); |
|
| 112 | 1 | $password = $configuration->getSolrPassword(); |
|
| 113 | } |
||
| 114 | |||
| 115 | 94 | $connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password); |
|
| 116 | 94 | if (!isset(self::$connections[$connectionHash])) { |
|
| 117 | 94 | $connection = $this->buildSolrService($host, $port, $path, $scheme); |
|
| 118 | 93 | if (trim($username) !== '') { |
|
| 119 | 1 | $connection->setAuthenticationCredentials($username, $password); |
|
| 120 | } |
||
| 121 | |||
| 122 | 93 | self::$connections[$connectionHash] = $connection; |
|
| 123 | } |
||
| 124 | |||
| 125 | 93 | return self::$connections[$connectionHash]; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Create a Solr Service instance from the passed connection configuration. |
||
| 130 | * |
||
| 131 | * @param string $host |
||
| 132 | * @param int $port |
||
| 133 | * @param string $path |
||
| 134 | * @param string $scheme |
||
| 135 | * @return SolrService|object |
||
| 136 | */ |
||
| 137 | 89 | protected function buildSolrService($host, $port, $path, $scheme) |
|
| 138 | { |
||
| 139 | 89 | return GeneralUtility::makeInstance(SolrService::class, $host, $port, $path, $scheme); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a solr configuration from the configuration array and returns it. |
||
| 144 | * |
||
| 145 | * @param array $config The solr configuration array |
||
| 146 | * @return SolrService |
||
| 147 | */ |
||
| 148 | 87 | protected function getConnectionFromConfiguration(array $config) |
|
| 149 | { |
||
| 150 | 87 | return $this->getConnection( |
|
| 151 | 87 | $config['solrHost'], |
|
| 152 | 87 | $config['solrPort'], |
|
| 153 | 87 | $config['solrPath'], |
|
| 154 | 87 | $config['solrScheme'], |
|
| 155 | 87 | $config['solrUsername'], |
|
| 156 | 87 | $config['solrPassword'] |
|
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Gets a Solr configuration for a page ID. |
||
| 162 | * |
||
| 163 | * @param int $pageId A page ID. |
||
| 164 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 165 | * @param string $mount Comma list of MountPoint parameters |
||
| 166 | * @return array A solr configuration. |
||
| 167 | * @throws NoSolrConnectionFoundException |
||
| 168 | */ |
||
| 169 | 77 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
|
| 170 | { |
||
| 171 | // find the root page |
||
| 172 | 77 | $pageSelect = GeneralUtility::makeInstance(PageRepository::class); |
|
| 173 | |||
| 174 | /** @var Rootline $rootLine */ |
||
| 175 | 77 | $rootLine = GeneralUtility::makeInstance(Rootline::class, $pageSelect->getRootLine($pageId, $mount)); |
|
| 176 | 77 | $siteRootPageId = $rootLine->getRootPageId(); |
|
| 177 | |||
| 178 | try { |
||
| 179 | 77 | $solrConfiguration = $this->getConfigurationByRootPageId($siteRootPageId, $language); |
|
| 180 | 4 | } catch (NoSolrConnectionFoundException $nscfe) { |
|
| 181 | /* @var $noSolrConnectionException NoSolrConnectionFoundException */ |
||
| 182 | 4 | $noSolrConnectionException = GeneralUtility::makeInstance( |
|
| 183 | 4 | NoSolrConnectionFoundException::class, |
|
| 184 | 4 | $nscfe->getMessage() . ' Initial page used was [' . $pageId . ']', |
|
| 185 | 4 | 1275399922 |
|
| 186 | ); |
||
| 187 | 4 | $noSolrConnectionException->setPageId($pageId); |
|
| 188 | |||
| 189 | 4 | throw $noSolrConnectionException; |
|
| 190 | } |
||
| 191 | |||
| 192 | 74 | return $solrConfiguration; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets a Solr connection for a page ID. |
||
| 197 | * |
||
| 198 | * @param int $pageId A page ID. |
||
| 199 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 200 | * @param string $mount Comma list of MountPoint parameters |
||
| 201 | * @return SolrService A solr connection. |
||
| 202 | * @throws NoSolrConnectionFoundException |
||
| 203 | */ |
||
| 204 | 77 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
|
| 205 | { |
||
| 206 | 77 | $solrServer = $this->getConfigurationByPageId($pageId, $language, $mount); |
|
| 207 | 74 | $solrConnection = $this->getConnectionFromConfiguration($solrServer); |
|
| 208 | 74 | return $solrConnection; |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets a Solr configuration for a root page ID. |
||
| 213 | * |
||
| 214 | * @param int $pageId A root page ID. |
||
| 215 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
| 216 | * @return array A solr configuration. |
||
| 217 | * @throws NoSolrConnectionFoundException |
||
| 218 | */ |
||
| 219 | 78 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
| 220 | { |
||
| 221 | 78 | $connectionKey = $pageId . '|' . $language; |
|
| 222 | 78 | $solrServers = $this->getAllConfigurations(); |
|
| 223 | |||
| 224 | 78 | if (isset($solrServers[$connectionKey])) { |
|
| 225 | 76 | $solrConfiguration = $solrServers[$connectionKey]; |
|
| 226 | } else { |
||
| 227 | /* @var $noSolrConnectionException NoSolrConnectionFoundException */ |
||
| 228 | 5 | $noSolrConnectionException = GeneralUtility::makeInstance( |
|
| 229 | 5 | NoSolrConnectionFoundException::class, |
|
| 230 | 'Could not find a Solr connection for root page [' |
||
| 231 | 5 | . $pageId . '] and language [' . $language . '].', |
|
| 232 | 5 | 1275396474 |
|
| 233 | ); |
||
| 234 | 5 | $noSolrConnectionException->setRootPageId($pageId); |
|
| 235 | 5 | $noSolrConnectionException->setLanguageId($language); |
|
| 236 | |||
| 237 | 5 | throw $noSolrConnectionException; |
|
| 238 | } |
||
| 239 | |||
| 240 | 76 | return $solrConfiguration; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Gets a Solr connection for a root page ID. |
||
| 245 | * |
||
| 246 | * @param int $pageId A root page ID. |
||
| 247 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 248 | * @return SolrService A solr connection. |
||
| 249 | * @throws NoSolrConnectionFoundException |
||
| 250 | */ |
||
| 251 | 9 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
| 252 | { |
||
| 253 | 9 | $config = $this->getConfigurationByRootPageId($pageId, $language); |
|
| 254 | 9 | $solrConnection = $this->getConnectionFromConfiguration($config); |
|
| 255 | |||
| 256 | 9 | return $solrConnection; |
|
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets all connection configurations found. |
||
| 261 | * |
||
| 262 | * @return array An array of connection configurations. |
||
| 263 | */ |
||
| 264 | 91 | public function getAllConfigurations() |
|
| 265 | { |
||
| 266 | /** @var $registry Registry */ |
||
| 267 | 91 | $registry = GeneralUtility::makeInstance(Registry::class); |
|
| 268 | 91 | $solrConfigurations = $registry->get('tx_solr', 'servers', []); |
|
| 269 | |||
| 270 | 91 | return $solrConfigurations; |
|
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Stores the connections in the registry. |
||
| 275 | * |
||
| 276 | * @param array $solrConfigurations |
||
| 277 | */ |
||
| 278 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
| 279 | { |
||
| 280 | /** @var $registry Registry */ |
||
| 281 | 3 | $registry = GeneralUtility::makeInstance(Registry::class); |
|
| 282 | 3 | $registry->set('tx_solr', 'servers', $solrConfigurations); |
|
| 283 | 3 | } |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Gets all connections found. |
||
| 287 | * |
||
| 288 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
| 289 | */ |
||
| 290 | 7 | public function getAllConnections() |
|
| 291 | { |
||
| 292 | 7 | $solrConnections = []; |
|
| 293 | |||
| 294 | 7 | $solrConfigurations = $this->getAllConfigurations(); |
|
| 295 | 7 | foreach ($solrConfigurations as $solrConfiguration) { |
|
| 296 | 7 | $solrConnections[] = $this->getConnectionFromConfiguration($solrConfiguration); |
|
| 297 | } |
||
| 298 | |||
| 299 | 7 | return $solrConnections; |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Gets all connection configurations for a given site. |
||
| 304 | * |
||
| 305 | * @param Site $site A TYPO3 site |
||
| 306 | * @return array An array of Solr connection configurations for a site |
||
| 307 | */ |
||
| 308 | 28 | public function getConfigurationsBySite(Site $site) |
|
| 309 | { |
||
| 310 | 28 | $solrConfigurations = []; |
|
| 311 | |||
| 312 | 28 | $allConfigurations = $this->getAllConfigurations(); |
|
| 313 | 28 | foreach ($allConfigurations as $configuration) { |
|
| 314 | 28 | if ($configuration['rootPageUid'] == $site->getRootPageId()) { |
|
| 315 | 28 | $solrConfigurations[] = $configuration; |
|
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | 28 | return $solrConfigurations; |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets all connections configured for a given site. |
||
| 324 | * |
||
| 325 | * @param Site $site A TYPO3 site |
||
| 326 | * @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
| 327 | */ |
||
| 328 | 16 | public function getConnectionsBySite(Site $site) |
|
| 329 | { |
||
| 330 | 16 | $connections = []; |
|
| 331 | |||
| 332 | 16 | $solrServers = $this->getConfigurationsBySite($site); |
|
| 333 | 16 | foreach ($solrServers as $solrServer) { |
|
| 334 | 16 | $connections[] = $this->getConnectionFromConfiguration($solrServer); |
|
| 335 | } |
||
| 336 | |||
| 337 | 16 | return $connections; |
|
| 338 | } |
||
| 339 | |||
| 340 | // updates |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
| 344 | * |
||
| 345 | * @param array $cacheActions Array of CacheMenuItems |
||
| 346 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
| 347 | */ |
||
| 348 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
| 349 | { |
||
| 350 | if ($GLOBALS['BE_USER']->isAdmin()) { |
||
| 351 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
| 352 | $optionValues[] = 'clearSolrConnectionCache'; |
||
| 353 | $cacheActions[] = [ |
||
| 354 | 'id' => 'clearSolrConnectionCache', |
||
| 355 | 'title' => 'LLL:EXT:solr/Resources/Private/Language/locallang.xlf:cache_initialize_solr_connections', |
||
| 356 | 'href' => $uriBuilder->buildUriFromRoute('ajax_solr_updateConnections'), |
||
| 357 | 'iconIdentifier' => 'extensions-solr-module-initsolrconnections' |
||
| 358 | ]; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Updates the connections in the registry. |
||
| 364 | * |
||
| 365 | */ |
||
| 366 | 2 | public function updateConnections() |
|
| 367 | { |
||
| 368 | 2 | $solrConnections = $this->getConfiguredSolrConnections(); |
|
| 369 | 2 | $solrConnections = $this->filterDuplicateConnections($solrConnections); |
|
| 370 | |||
| 371 | 2 | if (!empty($solrConnections)) { |
|
| 372 | 2 | $this->setAllConfigurations($solrConnections); |
|
| 373 | } |
||
| 374 | 2 | } |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Updates the Solr connections for a specific root page ID / site. |
||
| 378 | * |
||
| 379 | * @param int $rootPageId A site root page id |
||
| 380 | */ |
||
| 381 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
| 382 | { |
||
| 383 | 1 | $systemLanguages = $this->systemLanguageRepository->findSystemLanguages(); |
|
| 384 | 1 | $siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
|
| 385 | 1 | $site = $siteRepository->getSiteByRootPageId($rootPageId); |
|
| 386 | 1 | $rootPage = $site->getRootPage(); |
|
| 387 | |||
| 388 | 1 | $updatedSolrConnections = []; |
|
| 389 | 1 | foreach ($systemLanguages as $languageId) { |
|
| 390 | 1 | $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId); |
|
| 391 | |||
| 392 | 1 | if (!empty($connection)) { |
|
| 393 | 1 | $updatedSolrConnections[$connection['connectionKey']] = $connection; |
|
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | 1 | $solrConnections = $this->getAllConfigurations(); |
|
| 398 | 1 | $solrConnections = array_merge($solrConnections, $updatedSolrConnections); |
|
| 399 | 1 | $solrConnections = $this->filterDuplicateConnections($solrConnections); |
|
| 400 | 1 | $this->setAllConfigurations($solrConnections); |
|
| 401 | 1 | } |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Finds the configured Solr connections. Also respects multi-site |
||
| 405 | * environments. |
||
| 406 | * |
||
| 407 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
||
| 408 | */ |
||
| 409 | 2 | protected function getConfiguredSolrConnections() |
|
| 410 | { |
||
| 411 | 2 | $configuredSolrConnections = []; |
|
| 412 | // find website roots and languages for this installation |
||
| 413 | $rootPages = $this->pagesRepositoryAtExtSolr->findAllRootPages(); |
||
| 414 | 2 | $languages = $this->systemLanguageRepository->findSystemLanguages(); |
|
| 415 | 2 | ||
| 416 | // find solr configurations and add them as function menu entries |
||
| 417 | foreach ($rootPages as $rootPage) { |
||
| 418 | 2 | foreach ($languages as $languageId) { |
|
| 419 | 2 | $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, |
|
| 420 | 2 | $languageId); |
|
| 421 | |||
| 422 | if (!empty($connection)) { |
||
| 423 | 2 | $configuredSolrConnections[$connection['connectionKey']] = $connection; |
|
| 424 | 2 | } |
|
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | return $configuredSolrConnections; |
||
| 429 | 2 | } |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Gets the configured Solr connection for a specific root page and language ID. |
||
| 433 | * |
||
| 434 | * @param array $rootPage A root page record with at least title and uid |
||
| 435 | * @param int $languageId ID of a system language |
||
| 436 | * @return array A solr connection configuration. |
||
| 437 | */ |
||
| 438 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
||
| 490 | |||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Creates a human readable label from the connections' configuration. |
||
| 495 | * |
||
| 496 | * @param array $connection Connection configuration |
||
| 497 | * @return string Connection label |
||
| 498 | */ |
||
| 499 | protected function buildConnectionLabel(array $connection) |
||
| 500 | 3 | { |
|
| 501 | return $connection['rootPageTitle'] |
||
| 502 | 3 | . ' (pid: ' . $connection['rootPageUid'] |
|
| 503 | 3 | . ', language: ' . $this->systemLanguageRepository->findOneLanguageTitleByLanguageId($connection['language']) |
|
| 504 | 3 | . ') - ' |
|
| 505 | 3 | // . $connection['solrScheme'] . '://' |
|
| 506 | . $connection['solrHost'] . ':' |
||
| 507 | 3 | . $connection['solrPort'] |
|
| 510 | |||
| 511 | 3 | /** |
|
| 512 | * Filters duplicate connections. When detecting the configured connections |
||
| 513 | * this is done with a little brute force by simply combining all root pages |
||
| 514 | * with all languages, this method filters out the duplicates. |
||
| 515 | * |
||
| 516 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
| 517 | * @return array An array with connections, no duplicates. |
||
| 518 | */ |
||
| 519 | protected function filterDuplicateConnections(array $connections) |
||
| 539 | } |
||
| 540 |