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 |
||
| 49 | class ConnectionManager implements SingletonInterface, ClearCacheActionsHookInterface |
||
| 50 | { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected static $connections = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository |
||
| 59 | */ |
||
| 60 | protected $systemLanguageRepository; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
| 64 | */ |
||
| 65 | protected $logger = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var PagesRepositoryAtExtSolr |
||
| 69 | */ |
||
| 70 | protected $pagesRepositoryAtExtSolr; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param SystemLanguageRepository $systemLanguageRepository |
||
| 74 | * @param PagesRepositoryAtExtSolr|null $pagesRepositoryAtExtSolr |
||
| 75 | * @param SolrLogManager $solrLogManager |
||
| 76 | 108 | */ |
|
| 77 | public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SolrLogManager $solrLogManager = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gets a Solr connection. |
||
| 86 | * |
||
| 87 | * Instead of generating a new connection with each call, connections are |
||
| 88 | * kept and checked whether the requested connection already exists. If a |
||
| 89 | * connection already exists, it's reused. |
||
| 90 | * |
||
| 91 | * @param string $host Solr host (optional) |
||
| 92 | * @param int $port Solr port (optional) |
||
| 93 | * @param string $path Solr path (optional) |
||
| 94 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
| 95 | * @param string $username Solr user name (optional) |
||
| 96 | * @param string $password Solr password (optional) |
||
| 97 | * @return SolrConnection A solr connection. |
||
| 98 | 101 | */ |
|
| 99 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
||
| 124 | |||
| 125 | 100 | /** |
|
| 126 | * Create a Solr Service instance from the passed connection configuration. |
||
| 127 | * |
||
| 128 | * @param string $host |
||
| 129 | * @param int $port |
||
| 130 | * @param string $path |
||
| 131 | * @param string $scheme |
||
| 132 | * @param string $username |
||
| 133 | * @param string $password |
||
| 134 | * @return SolrConnection|object |
||
| 135 | */ |
||
| 136 | protected function buildSolrConnection($host, $port, $path, $scheme, $username = '', $password = '') |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Creates a solr configuration from the configuration array and returns it. |
||
| 143 | * |
||
| 144 | * @param array $config The solr configuration array |
||
| 145 | * @return SolrConnection |
||
| 146 | */ |
||
| 147 | protected function getConnectionFromConfiguration(array $config) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Gets a Solr configuration for a page ID. |
||
| 161 | * |
||
| 162 | * @param int $pageId A page ID. |
||
| 163 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 164 | * @param string $mount Comma list of MountPoint parameters |
||
| 165 | * @return array A solr configuration. |
||
| 166 | * @throws NoSolrConnectionFoundException |
||
| 167 | */ |
||
| 168 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets a Solr connection for a page ID. |
||
| 196 | * |
||
| 197 | * @param int $pageId A page ID. |
||
| 198 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 199 | * @param string $mount Comma list of MountPoint parameters |
||
| 200 | * @return SolrConnection A solr connection. |
||
| 201 | * @throws NoSolrConnectionFoundException |
||
| 202 | */ |
||
| 203 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets a Solr configuration for a root page ID. |
||
| 212 | * |
||
| 213 | * @param int $pageId A root page ID. |
||
| 214 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
| 215 | * @return array A solr configuration. |
||
| 216 | * @throws NoSolrConnectionFoundException |
||
| 217 | */ |
||
| 218 | public function getConfigurationByRootPageId($pageId, $language = 0) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets a Solr connection for a root page ID. |
||
| 244 | * |
||
| 245 | * @param int $pageId A root page ID. |
||
| 246 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
| 247 | * @return SolrConnection A solr connection. |
||
| 248 | * @throws NoSolrConnectionFoundException |
||
| 249 | */ |
||
| 250 | public function getConnectionByRootPageId($pageId, $language = 0) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Gets all connection configurations found. |
||
| 260 | * |
||
| 261 | * @return array An array of connection configurations. |
||
| 262 | */ |
||
| 263 | public function getAllConfigurations() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Stores the connections in the registry. |
||
| 274 | * |
||
| 275 | * @param array $solrConfigurations |
||
| 276 | */ |
||
| 277 | protected function setAllConfigurations(array $solrConfigurations) |
||
| 283 | 3 | ||
| 284 | /** |
||
| 285 | * Gets all connections found. |
||
| 286 | * |
||
| 287 | * @return SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections |
||
| 288 | */ |
||
| 289 | public function getAllConnections() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Gets all connection configurations for a given site. |
||
| 303 | * |
||
| 304 | * @param Site $site A TYPO3 site |
||
| 305 | * @return array An array of Solr connection configurations for a site |
||
| 306 | */ |
||
| 307 | public function getConfigurationsBySite(Site $site) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Gets all connections configured for a given site. |
||
| 323 | * |
||
| 324 | * @param Site $site A TYPO3 site |
||
| 325 | * @return SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection) |
||
| 326 | */ |
||
| 327 | public function getConnectionsBySite(Site $site) |
||
| 338 | |||
| 339 | // updates |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
| 343 | * |
||
| 344 | * @param array $cacheActions Array of CacheMenuItems |
||
| 345 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
| 346 | */ |
||
| 347 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Updates the connections in the registry. |
||
| 363 | * |
||
| 364 | */ |
||
| 365 | public function updateConnections() |
||
| 374 | 2 | ||
| 375 | /** |
||
| 376 | * Updates the Solr connections for a specific root page ID / site. |
||
| 377 | * |
||
| 378 | * @param int $rootPageId A site root page id |
||
| 379 | */ |
||
| 380 | public function updateConnectionByRootPageId($rootPageId) |
||
| 401 | 1 | ||
| 402 | /** |
||
| 403 | * Finds the configured Solr connections. Also respects multi-site |
||
| 404 | * environments. |
||
| 405 | * |
||
| 406 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
||
| 407 | */ |
||
| 408 | protected function getConfiguredSolrConnections() |
||
| 429 | 2 | ||
| 430 | /** |
||
| 431 | * Gets the configured Solr connection for a specific root page and language ID. |
||
| 432 | * |
||
| 433 | * @param array $rootPage A root page record with at least title and uid |
||
| 434 | * @param int $languageId ID of a system language |
||
| 435 | * @return array A solr connection configuration. |
||
| 436 | */ |
||
| 437 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
||
| 489 | 3 | ||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Creates a human readable label from the connections' configuration. |
||
| 494 | * |
||
| 495 | * @param array $connection Connection configuration |
||
| 496 | * @return string Connection label |
||
| 497 | */ |
||
| 498 | protected function buildConnectionLabel(array $connection) |
||
| 509 | 3 | ||
| 510 | /** |
||
| 511 | 3 | * Filters duplicate connections. When detecting the configured connections |
|
| 512 | * this is done with a little brute force by simply combining all root pages |
||
| 513 | * with all languages, this method filters out the duplicates. |
||
| 514 | * |
||
| 515 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
| 516 | * @return array An array with connections, no duplicates. |
||
| 517 | */ |
||
| 518 | protected function filterDuplicateConnections(array $connections) |
||
| 538 | } |
||
| 539 |