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 | */ |
||
75 | 101 | public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null) |
|
80 | |||
81 | /** |
||
82 | * Gets a Solr connection. |
||
83 | * |
||
84 | * Instead of generating a new connection with each call, connections are |
||
85 | * kept and checked whether the requested connection already exists. If a |
||
86 | * connection already exists, it's reused. |
||
87 | * |
||
88 | * @param string $host Solr host (optional) |
||
89 | * @param int $port Solr port (optional) |
||
90 | * @param string $path Solr path (optional) |
||
91 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
92 | * @param string $username Solr user name (optional) |
||
93 | * @param string $password Solr password (optional) |
||
94 | * @return SolrService A solr connection. |
||
95 | */ |
||
96 | 94 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
126 | |||
127 | /** |
||
128 | * Create a Solr Service instance from the passed connection configuration. |
||
129 | * |
||
130 | * @param string $host |
||
131 | * @param int $port |
||
132 | * @param string $path |
||
133 | * @param string $scheme |
||
134 | * @return SolrService|object |
||
135 | */ |
||
136 | 89 | protected function buildSolrService($host, $port, $path, $scheme) |
|
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 SolrService |
||
146 | */ |
||
147 | 87 | 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 | 77 | 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 SolrService A solr connection. |
||
201 | * @throws NoSolrConnectionFoundException |
||
202 | */ |
||
203 | 77 | 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 | 78 | 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 SolrService A solr connection. |
||
248 | * @throws NoSolrConnectionFoundException |
||
249 | */ |
||
250 | 9 | 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 | 91 | public function getAllConfigurations() |
|
271 | |||
272 | /** |
||
273 | * Stores the connections in the registry. |
||
274 | * |
||
275 | * @param array $solrConfigurations |
||
276 | */ |
||
277 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
283 | |||
284 | /** |
||
285 | * Gets all connections found. |
||
286 | * |
||
287 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
288 | */ |
||
289 | 7 | 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 | 28 | 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 SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
326 | */ |
||
327 | 16 | 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 | 2 | public function updateConnections() |
|
374 | |||
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 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
401 | |||
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 | 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 | 3 | 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 | 3 | protected function buildConnectionLabel(array $connection) |
|
512 | |||
513 | /** |
||
514 | * Filters duplicate connections. When detecting the configured connections |
||
515 | * this is done with a little brute force by simply combining all root pages |
||
516 | * with all languages, this method filters out the duplicates. |
||
517 | * |
||
518 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
519 | * @return array An array with connections, no duplicates. |
||
520 | */ |
||
521 | 3 | protected function filterDuplicateConnections(array $connections) |
|
541 | } |
||
542 |