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 | |||
78 | 76 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
79 | { |
||
80 | 76 | if (empty($host)) { |
|
81 | 1 | $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
82 | 1 | $this->logger->log( |
|
83 | 1 | SolrLogManager::WARNING, |
|
84 | 'ApacheSolrForTypo3\Solr\ConnectionManager::getConnection() called with empty host parameter. Using configuration from TSFE, might be inaccurate. Always provide a host or use the getConnectionBy* methods.' |
||
85 | 1 | ); |
|
86 | |||
87 | 1 | $configuration = Util::getSolrConfiguration(); |
|
88 | 1 | $host = $configuration->getSolrHost(); |
|
89 | 1 | $port = $configuration->getSolrPort(); |
|
90 | 1 | $path = $configuration->getSolrPath(); |
|
91 | 1 | $scheme = $configuration->getSolrScheme(); |
|
92 | 1 | $username = $configuration->getSolrUsername(); |
|
93 | 1 | $password = $configuration->getSolrPassword(); |
|
94 | 47 | } |
|
95 | |||
96 | 76 | $connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password); |
|
97 | 76 | if (!isset(self::$connections[$connectionHash])) { |
|
98 | 76 | $connection = $this->buildSolrService($host, $port, $path, $scheme); |
|
99 | 75 | if (trim($username) !== '') { |
|
100 | 1 | $connection->setAuthenticationCredentials($username, $password); |
|
101 | 1 | } |
|
102 | |||
103 | 75 | self::$connections[$connectionHash] = $connection; |
|
104 | 75 | } |
|
105 | |||
106 | 75 | return self::$connections[$connectionHash]; |
|
107 | } |
||
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 | */ |
||
118 | 71 | 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 | */ |
||
129 | 69 | protected function getConnectionFromConfiguration(array $config) |
|
130 | { |
||
131 | 69 | return $this->getConnection( |
|
132 | 69 | $config['solrHost'], |
|
133 | 69 | $config['solrPort'], |
|
134 | 69 | $config['solrPath'], |
|
135 | 69 | $config['solrScheme'], |
|
136 | 69 | $config['solrUsername'], |
|
137 | 69 | $config['solrPassword'] |
|
138 | 69 | ); |
|
139 | } |
||
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 | */ |
||
150 | 59 | 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 | */ |
||
185 | 59 | 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 | */ |
||
200 | 60 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
201 | { |
||
202 | 60 | $connectionKey = $pageId . '|' . $language; |
|
203 | 60 | $solrServers = $this->getAllConfigurations(); |
|
204 | |||
205 | 60 | if (isset($solrServers[$connectionKey])) { |
|
206 | 58 | $solrConfiguration = $solrServers[$connectionKey]; |
|
207 | 58 | } else { |
|
208 | /* @var $noSolrConnectionException NoSolrConnectionFoundException */ |
||
209 | 3 | $noSolrConnectionException = GeneralUtility::makeInstance( |
|
210 | 3 | NoSolrConnectionFoundException::class, |
|
211 | 'Could not find a Solr connection for root page [' |
||
212 | 3 | . $pageId . '] and language [' . $language . '].', |
|
213 | 1275396474 |
||
214 | 3 | ); |
|
215 | 3 | $noSolrConnectionException->setRootPageId($pageId); |
|
216 | 3 | $noSolrConnectionException->setLanguageId($language); |
|
217 | |||
218 | 3 | throw $noSolrConnectionException; |
|
219 | } |
||
220 | |||
221 | 58 | return $solrConfiguration; |
|
222 | } |
||
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 | */ |
||
232 | 6 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
239 | |||
240 | /** |
||
241 | * Gets all connection configurations found. |
||
242 | * |
||
243 | * @return array An array of connection configurations. |
||
244 | */ |
||
245 | 73 | public function getAllConfigurations() |
|
253 | |||
254 | /** |
||
255 | * Stores the connections in the registry. |
||
256 | * |
||
257 | * @param array $solrConfigurations |
||
258 | */ |
||
259 | 3 | 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 | */ |
||
271 | 7 | 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 | */ |
||
289 | 22 | 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 | */ |
||
309 | 16 | 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) |
||
342 | |||
343 | /** |
||
344 | * Updates the connections in the registry. |
||
345 | * |
||
346 | */ |
||
347 | public function updateConnections() |
||
356 | |||
357 | /** |
||
358 | * Entrypoint for the ajax request |
||
359 | */ |
||
360 | public function updateConnectionsInCacheMenu() |
||
364 | 2 | ||
365 | /** |
||
366 | 2 | * Updates the Solr connections for a specific root page ID / site. |
|
367 | 2 | * |
|
368 | 2 | * @param int $rootPageId A site root page id |
|
369 | 2 | */ |
|
370 | public function updateConnectionByRootPageId($rootPageId) |
||
391 | 1 | ||
392 | 1 | /** |
|
393 | 1 | * Finds the configured Solr connections. Also respects multi-site |
|
394 | * environments. |
||
395 | 1 | * |
|
396 | 1 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
|
397 | 1 | */ |
|
398 | 1 | protected function getConfiguredSolrConnections() |
|
420 | |||
421 | 2 | /** |
|
422 | 2 | * Gets the configured Solr connection for a specific root page and language ID. |
|
423 | 2 | * |
|
424 | 2 | * @param array $rootPage A root page record with at least title and uid |
|
425 | * @param int $languageId ID of a system language |
||
426 | 2 | * @return array A solr connection configuration. |
|
427 | 2 | */ |
|
428 | 2 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
480 | 3 | ||
481 | 3 | /** |
|
482 | 3 | * Gets the language name for a given language ID. |
|
483 | 3 | * |
|
484 | 3 | * @param int $languageId language ID |
|
485 | 3 | * @return string Language name |
|
486 | 3 | */ |
|
487 | protected function getLanguageName($languageId) |
||
505 | 3 | ||
506 | 3 | /** |
|
507 | 3 | * Creates a human readable label from the connections' configuration. |
|
508 | * |
||
509 | 3 | * @param array $connection Connection configuration |
|
510 | * @return string Connection label |
||
511 | 3 | */ |
|
512 | protected function buildConnectionLabel(array $connection) |
||
525 | |||
526 | 3 | /** |
|
527 | * Filters duplicate connections. When detecting the configured connections |
||
528 | 3 | * this is done with a little brute force by simply combining all root pages |
|
529 | 3 | * with all languages, this method filters out the duplicates. |
|
530 | 3 | * |
|
531 | 3 | * @param array $connections An array of unfiltered connections, containing duplicates |
|
532 | * @return array An array with connections, no duplicates. |
||
533 | 3 | */ |
|
534 | 3 | protected function filterDuplicateConnections(array $connections) |
|
554 | 3 | ||
555 | 3 | /** |
|
556 | 3 | * Finds the system's configured languages. |
|
557 | 3 | * |
|
558 | 3 | * @return array An array of language IDs |
|
559 | */ |
||
560 | 3 | protected function getSystemLanguages() |
|
579 | 3 | ||
580 | 3 | /** |
|
581 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
582 | 3 | * which usually is the case for pages with pid = 0. |
|
583 | * |
||
584 | 3 | * @return array An array of (partial) root page records, containing the uid and title fields |
|
585 | */ |
||
586 | protected function getRootPages() |
||
596 | } |
||
597 |