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 |
||
47 | class ConnectionManager implements SingletonInterface, ClearCacheActionsHookInterface |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected static $connections = []; |
||
54 | |||
55 | /** |
||
56 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
57 | */ |
||
58 | protected $logger = null; |
||
59 | |||
60 | /** |
||
61 | * Gets a Solr connection. |
||
62 | * |
||
63 | * Instead of generating a new connection with each call, connections are |
||
64 | * kept and checked whether the requested connection already exists. If a |
||
65 | * connection already exists, it's reused. |
||
66 | * |
||
67 | * @param string $host Solr host (optional) |
||
68 | * @param int $port Solr port (optional) |
||
69 | * @param string $path Solr path (optional) |
||
70 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
71 | * @param string $username Solr user name (optional) |
||
72 | * @param string $password Solr password (optional) |
||
73 | * @return SolrService A solr connection. |
||
74 | */ |
||
75 | |||
76 | 73 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
106 | |||
107 | /** |
||
108 | * Create a Solr Service instance from the passed connection configuration. |
||
109 | * |
||
110 | * @param string $host |
||
111 | * @param int $port |
||
112 | * @param string $path |
||
113 | * @param string $scheme |
||
114 | * @return SolrService|object |
||
115 | */ |
||
116 | 68 | protected function buildSolrService($host, $port, $path, $scheme) |
|
120 | |||
121 | /** |
||
122 | * Creates a solr configuration from the configuration array and returns it. |
||
123 | * |
||
124 | * @param array $config The solr configuration array |
||
125 | * @return SolrService |
||
126 | */ |
||
127 | 68 | protected function getConnectionFromConfiguration(array $config) |
|
138 | |||
139 | /** |
||
140 | * Gets a Solr configuration for a page ID. |
||
141 | * |
||
142 | * @param int $pageId A page ID. |
||
143 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
144 | * @param string $mount Comma list of MountPoint parameters |
||
145 | * @return array A solr configuration. |
||
146 | * @throws NoSolrConnectionFoundException |
||
147 | */ |
||
148 | 58 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
|
173 | |||
174 | /** |
||
175 | * Gets a Solr connection for a page ID. |
||
176 | * |
||
177 | * @param int $pageId A page ID. |
||
178 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
179 | * @param string $mount Comma list of MountPoint parameters |
||
180 | * @return SolrService A solr connection. |
||
181 | * @throws NoSolrConnectionFoundException |
||
182 | */ |
||
183 | 58 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
|
189 | |||
190 | /** |
||
191 | * Gets a Solr configuration for a root page ID. |
||
192 | * |
||
193 | * @param int $pageId A root page ID. |
||
194 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
195 | * @return array A solr configuration. |
||
196 | * @throws NoSolrConnectionFoundException |
||
197 | */ |
||
198 | 59 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
221 | |||
222 | /** |
||
223 | * Gets a Solr connection for a root page ID. |
||
224 | * |
||
225 | * @param int $pageId A root page ID. |
||
226 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
227 | * @return SolrService A solr connection. |
||
228 | * @throws NoSolrConnectionFoundException |
||
229 | */ |
||
230 | 6 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
237 | |||
238 | /** |
||
239 | * Gets all connection configurations found. |
||
240 | * |
||
241 | * @return array An array of connection configurations. |
||
242 | */ |
||
243 | 70 | public function getAllConfigurations() |
|
251 | |||
252 | /** |
||
253 | * Stores the connections in the registry. |
||
254 | * |
||
255 | * @param array $solrConfigurations |
||
256 | */ |
||
257 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
263 | |||
264 | /** |
||
265 | * Gets all connections found. |
||
266 | * |
||
267 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
268 | */ |
||
269 | 7 | public function getAllConnections() |
|
280 | |||
281 | /** |
||
282 | * Gets all connection configurations for a given site. |
||
283 | * |
||
284 | * @param Site $site A TYPO3 site |
||
285 | * @return array An array of Solr connection configurations for a site |
||
286 | */ |
||
287 | 22 | public function getConfigurationsBySite(Site $site) |
|
300 | |||
301 | /** |
||
302 | * Gets all connections configured for a given site. |
||
303 | * |
||
304 | * @param Site $site A TYPO3 site |
||
305 | * @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
306 | */ |
||
307 | 11 | public function getConnectionsBySite(Site $site) |
|
318 | |||
319 | // updates |
||
320 | |||
321 | /** |
||
322 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
323 | * |
||
324 | * @param array $cacheActions Array of CacheMenuItems |
||
325 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
326 | */ |
||
327 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
343 | |||
344 | /** |
||
345 | * Updates the connections in the registry. |
||
346 | * |
||
347 | */ |
||
348 | 2 | public function updateConnections() |
|
357 | |||
358 | /** |
||
359 | * Entrypoint for the ajax request |
||
360 | */ |
||
361 | public function updateConnectionsInCacheMenu() |
||
365 | |||
366 | /** |
||
367 | * Updates the Solr connections for a specific root page ID / site. |
||
368 | * |
||
369 | * @param int $rootPageId A site root page id |
||
370 | */ |
||
371 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
390 | |||
391 | /** |
||
392 | * Finds the configured Solr connections. Also respects multi-site |
||
393 | * environments. |
||
394 | * |
||
395 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
||
396 | */ |
||
397 | 2 | protected function getConfiguredSolrConnections() |
|
419 | |||
420 | /** |
||
421 | * Gets the configured Solr connection for a specific root page and language ID. |
||
422 | * |
||
423 | * @param array $rootPage A root page record with at least title and uid |
||
424 | * @param int $languageId ID of a system language |
||
425 | * @return array A solr connection configuration. |
||
426 | */ |
||
427 | 3 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
479 | |||
480 | /** |
||
481 | * Gets the language name for a given language ID. |
||
482 | * |
||
483 | * @param int $languageId language ID |
||
484 | * @return string Language name |
||
485 | */ |
||
486 | 3 | protected function getLanguageName($languageId) |
|
504 | |||
505 | /** |
||
506 | * Creates a human readable label from the connections' configuration. |
||
507 | * |
||
508 | * @param array $connection Connection configuration |
||
509 | * @return string Connection label |
||
510 | */ |
||
511 | 3 | protected function buildConnectionLabel(array $connection) |
|
524 | |||
525 | /** |
||
526 | * Filters duplicate connections. When detecting the configured connections |
||
527 | * this is done with a little brute force by simply combining all root pages |
||
528 | * with all languages, this method filters out the duplicates. |
||
529 | * |
||
530 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
531 | * @return array An array with connections, no duplicates. |
||
532 | */ |
||
533 | 3 | protected function filterDuplicateConnections(array $connections) |
|
553 | |||
554 | /** |
||
555 | * Finds the system's configured languages. |
||
556 | * |
||
557 | * @return array An array of language IDs |
||
558 | */ |
||
559 | 3 | protected function getSystemLanguages() |
|
578 | |||
579 | /** |
||
580 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
581 | * which usually is the case for pages with pid = 0. |
||
582 | * |
||
583 | * @return array An array of (partial) root page records, containing the uid and title fields |
||
584 | */ |
||
585 | 2 | protected function getRootPages() |
|
595 | } |
||
596 |