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 | 108 | public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SolrLogManager $solrLogManager = null) |
|
| 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 | 101 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
| 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 | 96 | protected function buildSolrService($host, $port, $path, $scheme) |
|
| 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 | 94 | protected function getConnectionFromConfiguration(array $config) |
|
| 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 | 84 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
|
| 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 | 84 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
|
| 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 | 85 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
| 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 | 10 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Gets all connection configurations found. |
||
| 261 | * |
||
| 262 | * @return array An array of connection configurations. |
||
| 263 | */ |
||
| 264 | 98 | public function getAllConfigurations() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Stores the connections in the registry. |
||
| 275 | * |
||
| 276 | * @param array $solrConfigurations |
||
| 277 | */ |
||
| 278 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
| 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() |
|
| 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 | 31 | public function getConfigurationsBySite(Site $site) |
|
| 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) |
|
| 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) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Updates the connections in the registry. |
||
| 364 | * |
||
| 365 | */ |
||
| 366 | 2 | public function updateConnections() |
|
| 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) |
|
| 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() |
|
| 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) |
||
| 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 |