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\Logging\SolrLogManager |
||
59 | */ |
||
60 | protected $logger = null; |
||
61 | |||
62 | /** |
||
63 | * Gets a Solr connection. |
||
64 | * |
||
65 | * Instead of generating a new connection with each call, connections are |
||
66 | * kept and checked whether the requested connection already exists. If a |
||
67 | * connection already exists, it's reused. |
||
68 | * |
||
69 | * @param string $host Solr host (optional) |
||
70 | * @param int $port Solr port (optional) |
||
71 | * @param string $path Solr path (optional) |
||
72 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
73 | * @param string $username Solr user name (optional) |
||
74 | * @param string $password Solr password (optional) |
||
75 | * @return SolrService A solr connection. |
||
76 | */ |
||
77 | 75 | ||
78 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
||
108 | |||
109 | /** |
||
110 | * Create a Solr Service instance from the passed connection configuration. |
||
111 | * |
||
112 | * @param string $host |
||
113 | * @param int $port |
||
114 | * @param string $path |
||
115 | * @param string $scheme |
||
116 | * @return SolrService|object |
||
117 | 70 | */ |
|
118 | protected function buildSolrService($host, $port, $path, $scheme) |
||
122 | |||
123 | /** |
||
124 | * Creates a solr configuration from the configuration array and returns it. |
||
125 | * |
||
126 | * @param array $config The solr configuration array |
||
127 | * @return SolrService |
||
128 | 68 | */ |
|
129 | protected function getConnectionFromConfiguration(array $config) |
||
140 | |||
141 | /** |
||
142 | * Gets a Solr configuration for a page ID. |
||
143 | * |
||
144 | * @param int $pageId A page ID. |
||
145 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
146 | * @param string $mount Comma list of MountPoint parameters |
||
147 | * @return array A solr configuration. |
||
148 | * @throws NoSolrConnectionFoundException |
||
149 | 58 | */ |
|
150 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
||
175 | |||
176 | /** |
||
177 | * Gets a Solr connection for a page ID. |
||
178 | * |
||
179 | * @param int $pageId A page ID. |
||
180 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
181 | * @param string $mount Comma list of MountPoint parameters |
||
182 | * @return SolrService A solr connection. |
||
183 | * @throws NoSolrConnectionFoundException |
||
184 | 58 | */ |
|
185 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
||
191 | |||
192 | /** |
||
193 | * Gets a Solr configuration for a root page ID. |
||
194 | * |
||
195 | * @param int $pageId A root page ID. |
||
196 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
197 | * @return array A solr configuration. |
||
198 | * @throws NoSolrConnectionFoundException |
||
199 | 59 | */ |
|
200 | public function getConfigurationByRootPageId($pageId, $language = 0) |
||
223 | |||
224 | /** |
||
225 | * Gets a Solr connection for a root page ID. |
||
226 | * |
||
227 | * @param int $pageId A root page ID. |
||
228 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
229 | * @return SolrService A solr connection. |
||
230 | * @throws NoSolrConnectionFoundException |
||
231 | 6 | */ |
|
232 | public function getConnectionByRootPageId($pageId, $language = 0) |
||
239 | |||
240 | /** |
||
241 | * Gets all connection configurations found. |
||
242 | * |
||
243 | * @return array An array of connection configurations. |
||
244 | 72 | */ |
|
245 | public function getAllConfigurations() |
||
253 | |||
254 | /** |
||
255 | * Stores the connections in the registry. |
||
256 | * |
||
257 | * @param array $solrConfigurations |
||
258 | 3 | */ |
|
259 | protected function setAllConfigurations(array $solrConfigurations) |
||
265 | |||
266 | /** |
||
267 | * Gets all connections found. |
||
268 | * |
||
269 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
270 | 7 | */ |
|
271 | public function getAllConnections() |
||
282 | |||
283 | /** |
||
284 | * Gets all connection configurations for a given site. |
||
285 | * |
||
286 | * @param Site $site A TYPO3 site |
||
287 | * @return array An array of Solr connection configurations for a site |
||
288 | 22 | */ |
|
289 | public function getConfigurationsBySite(Site $site) |
||
302 | |||
303 | /** |
||
304 | * Gets all connections configured for a given site. |
||
305 | * |
||
306 | * @param Site $site A TYPO3 site |
||
307 | * @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
308 | 16 | */ |
|
309 | public function getConnectionsBySite(Site $site) |
||
320 | |||
321 | // updates |
||
322 | |||
323 | /** |
||
324 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
325 | * |
||
326 | * @param array $cacheActions Array of CacheMenuItems |
||
327 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
328 | */ |
||
329 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
356 | |||
357 | 2 | /** |
|
358 | * Updates the connections in the registry. |
||
359 | * |
||
360 | */ |
||
361 | public function updateConnections() |
||
370 | |||
371 | /** |
||
372 | 1 | * Entrypoint for the ajax request |
|
373 | */ |
||
374 | 1 | public function updateConnectionsInCacheMenu() |
|
378 | |||
379 | 1 | /** |
|
380 | 1 | * Updates the Solr connections for a specific root page ID / site. |
|
381 | 1 | * |
|
382 | * @param int $rootPageId A site root page id |
||
383 | 1 | */ |
|
384 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
405 | 2 | ||
406 | 2 | /** |
|
407 | * Finds the configured Solr connections. Also respects multi-site |
||
408 | * environments. |
||
409 | 2 | * |
|
410 | 2 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
|
411 | 2 | */ |
|
412 | protected function getConfiguredSolrConnections() |
||
434 | 3 | ||
435 | 3 | /** |
|
436 | 3 | * Gets the configured Solr connection for a specific root page and language ID. |
|
437 | * |
||
438 | 3 | * @param array $rootPage A root page record with at least title and uid |
|
439 | 3 | * @param int $languageId ID of a system language |
|
440 | * @return array A solr connection configuration. |
||
441 | 3 | */ |
|
442 | 3 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
494 | 3 | ||
495 | 3 | /** |
|
496 | 3 | * Gets the language name for a given language ID. |
|
497 | * |
||
498 | * @param int $languageId language ID |
||
499 | 3 | * @return string Language name |
|
500 | */ |
||
501 | 3 | protected function getLanguageName($languageId) |
|
519 | 3 | ||
520 | /** |
||
521 | 3 | * Creates a human readable label from the connections' configuration. |
|
522 | 3 | * |
|
523 | 3 | * @param array $connection Connection configuration |
|
524 | * @return string Connection label |
||
525 | 3 | */ |
|
526 | protected function buildConnectionLabel(array $connection) |
||
539 | 3 | ||
540 | /** |
||
541 | * Filters duplicate connections. When detecting the configured connections |
||
542 | 3 | * this is done with a little brute force by simply combining all root pages |
|
543 | 3 | * with all languages, this method filters out the duplicates. |
|
544 | 3 | * |
|
545 | 3 | * @param array $connections An array of unfiltered connections, containing duplicates |
|
546 | * @return array An array with connections, no duplicates. |
||
547 | */ |
||
548 | 3 | protected function filterDuplicateConnections(array $connections) |
|
568 | 3 | ||
569 | 3 | /** |
|
570 | * Finds the system's configured languages. |
||
571 | * |
||
572 | 3 | * @return array An array of language IDs |
|
573 | */ |
||
574 | protected function getSystemLanguages() |
||
593 | 2 | ||
594 | /** |
||
595 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
596 | 2 | * which usually is the case for pages with pid = 0. |
|
597 | * |
||
598 | * @return array An array of (partial) root page records, containing the uid and title fields |
||
599 | */ |
||
600 | protected function getRootPages() |
||
610 | } |
||
611 |