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\Logging\SolrLogManager |
||
58 | */ |
||
59 | protected $logger = null; |
||
60 | |||
61 | /** |
||
62 | * Gets a Solr connection. |
||
63 | * |
||
64 | * Instead of generating a new connection with each call, connections are |
||
65 | * kept and checked whether the requested connection already exists. If a |
||
66 | * connection already exists, it's reused. |
||
67 | * |
||
68 | * @param string $host Solr host (optional) |
||
69 | * @param int $port Solr port (optional) |
||
70 | * @param string $path Solr path (optional) |
||
71 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
72 | * @param string $username Solr user name (optional) |
||
73 | * @param string $password Solr password (optional) |
||
74 | * @return SolrService A solr connection. |
||
75 | */ |
||
76 | |||
77 | 75 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
78 | { |
||
79 | 75 | if (empty($host)) { |
|
80 | 1 | $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
81 | 1 | $this->logger->log( |
|
82 | 1 | SolrLogManager::WARNING, |
|
83 | '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.' |
||
84 | 1 | ); |
|
85 | |||
86 | 1 | $configuration = Util::getSolrConfiguration(); |
|
87 | 1 | $host = $configuration->getSolrHost(); |
|
88 | 1 | $port = $configuration->getSolrPort(); |
|
89 | 1 | $path = $configuration->getSolrPath(); |
|
90 | 1 | $scheme = $configuration->getSolrScheme(); |
|
91 | 1 | $username = $configuration->getSolrUsername(); |
|
92 | 1 | $password = $configuration->getSolrPassword(); |
|
93 | 1 | } |
|
94 | |||
95 | 75 | $connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password); |
|
96 | 75 | if (!isset(self::$connections[$connectionHash])) { |
|
97 | 75 | $connection = $this->buildSolrService($host, $port, $path, $scheme); |
|
98 | 74 | if (trim($username) !== '') { |
|
99 | 1 | $connection->setAuthenticationCredentials($username, $password); |
|
100 | 1 | } |
|
101 | |||
102 | 74 | self::$connections[$connectionHash] = $connection; |
|
103 | 74 | } |
|
104 | |||
105 | 74 | return self::$connections[$connectionHash]; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Create a Solr Service instance from the passed connection configuration. |
||
110 | * |
||
111 | * @param string $host |
||
112 | * @param int $port |
||
113 | * @param string $path |
||
114 | * @param string $scheme |
||
115 | * @return SolrService|object |
||
116 | */ |
||
117 | 70 | protected function buildSolrService($host, $port, $path, $scheme) |
|
121 | |||
122 | /** |
||
123 | * Creates a solr configuration from the configuration array and returns it. |
||
124 | * |
||
125 | * @param array $config The solr configuration array |
||
126 | * @return SolrService |
||
127 | */ |
||
128 | 68 | protected function getConnectionFromConfiguration(array $config) |
|
129 | { |
||
130 | 68 | return $this->getConnection( |
|
131 | 68 | $config['solrHost'], |
|
132 | 68 | $config['solrPort'], |
|
133 | 68 | $config['solrPath'], |
|
134 | 68 | $config['solrScheme'], |
|
135 | 68 | $config['solrUsername'], |
|
136 | 68 | $config['solrPassword'] |
|
137 | 68 | ); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Gets a Solr configuration for a page ID. |
||
142 | * |
||
143 | * @param int $pageId A page ID. |
||
144 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
145 | * @param string $mount Comma list of MountPoint parameters |
||
146 | * @return array A solr configuration. |
||
147 | * @throws NoSolrConnectionFoundException |
||
148 | */ |
||
149 | 58 | public function getConfigurationByPageId($pageId, $language = 0, $mount = '') |
|
174 | |||
175 | /** |
||
176 | * Gets a Solr connection for a page ID. |
||
177 | * |
||
178 | * @param int $pageId A page ID. |
||
179 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
180 | * @param string $mount Comma list of MountPoint parameters |
||
181 | * @return SolrService A solr connection. |
||
182 | * @throws NoSolrConnectionFoundException |
||
183 | */ |
||
184 | 58 | public function getConnectionByPageId($pageId, $language = 0, $mount = '') |
|
190 | |||
191 | /** |
||
192 | * Gets a Solr configuration for a root page ID. |
||
193 | * |
||
194 | * @param int $pageId A root page ID. |
||
195 | * @param int $language The language ID to get the configuration for as the path may differ. Optional, defaults to 0. |
||
196 | * @return array A solr configuration. |
||
197 | * @throws NoSolrConnectionFoundException |
||
198 | */ |
||
199 | 59 | public function getConfigurationByRootPageId($pageId, $language = 0) |
|
200 | { |
||
201 | 59 | $connectionKey = $pageId . '|' . $language; |
|
202 | 59 | $solrServers = $this->getAllConfigurations(); |
|
203 | |||
204 | 59 | if (isset($solrServers[$connectionKey])) { |
|
205 | 57 | $solrConfiguration = $solrServers[$connectionKey]; |
|
206 | 57 | } else { |
|
207 | /* @var $noSolrConnectionException NoSolrConnectionFoundException */ |
||
208 | 3 | $noSolrConnectionException = GeneralUtility::makeInstance( |
|
209 | 3 | NoSolrConnectionFoundException::class, |
|
210 | 'Could not find a Solr connection for root page [' |
||
211 | 3 | . $pageId . '] and language [' . $language . '].', |
|
212 | 1275396474 |
||
213 | 3 | ); |
|
214 | 3 | $noSolrConnectionException->setRootPageId($pageId); |
|
215 | 3 | $noSolrConnectionException->setLanguageId($language); |
|
216 | |||
217 | 3 | throw $noSolrConnectionException; |
|
218 | } |
||
219 | |||
220 | 57 | return $solrConfiguration; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Gets a Solr connection for a root page ID. |
||
225 | * |
||
226 | * @param int $pageId A root page ID. |
||
227 | * @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0. |
||
228 | * @return SolrService A solr connection. |
||
229 | * @throws NoSolrConnectionFoundException |
||
230 | */ |
||
231 | 6 | public function getConnectionByRootPageId($pageId, $language = 0) |
|
238 | |||
239 | /** |
||
240 | * Gets all connection configurations found. |
||
241 | * |
||
242 | * @return array An array of connection configurations. |
||
243 | */ |
||
244 | 72 | public function getAllConfigurations() |
|
252 | |||
253 | /** |
||
254 | * Stores the connections in the registry. |
||
255 | * |
||
256 | * @param array $solrConfigurations |
||
257 | */ |
||
258 | 3 | protected function setAllConfigurations(array $solrConfigurations) |
|
264 | |||
265 | /** |
||
266 | * Gets all connections found. |
||
267 | * |
||
268 | * @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections |
||
269 | */ |
||
270 | 7 | public function getAllConnections() |
|
271 | { |
||
272 | 7 | $connections = []; |
|
273 | |||
274 | 7 | $solrConfigurations = $this->getAllConfigurations(); |
|
275 | 7 | foreach ($solrConfigurations as $solrConfiguration) { |
|
276 | 7 | $connections[] = $this->getConnectionFromConfiguration($solrConfiguration); |
|
277 | 7 | } |
|
278 | |||
279 | 7 | return $connections; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Gets all connection configurations for a given site. |
||
284 | * |
||
285 | * @param Site $site A TYPO3 site |
||
286 | * @return array An array of Solr connection configurations for a site |
||
287 | */ |
||
288 | 22 | public function getConfigurationsBySite(Site $site) |
|
289 | { |
||
290 | 22 | $solrConfigurations = []; |
|
291 | |||
292 | 22 | $allConfigurations = $this->getAllConfigurations(); |
|
293 | 22 | foreach ($allConfigurations as $configuration) { |
|
294 | 22 | if ($configuration['rootPageUid'] == $site->getRootPageId()) { |
|
295 | 22 | $solrConfigurations[] = $configuration; |
|
296 | 22 | } |
|
297 | 22 | } |
|
298 | |||
299 | 22 | return $solrConfigurations; |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Gets all connections configured for a given site. |
||
304 | * |
||
305 | * @param Site $site A TYPO3 site |
||
306 | * @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService) |
||
307 | */ |
||
308 | 16 | public function getConnectionsBySite(Site $site) |
|
309 | { |
||
310 | 16 | $connections = []; |
|
311 | |||
312 | 16 | $solrServers = $this->getConfigurationsBySite($site); |
|
313 | 16 | foreach ($solrServers as $solrServer) { |
|
314 | 16 | $connections[] = $this->getConnectionFromConfiguration($solrServer); |
|
315 | 16 | } |
|
316 | |||
317 | 16 | return $connections; |
|
318 | } |
||
319 | |||
320 | // updates |
||
321 | |||
322 | /** |
||
323 | * Adds a menu entry to the clear cache menu to detect Solr connections. |
||
324 | * |
||
325 | * @param array $cacheActions Array of CacheMenuItems |
||
326 | * @param array $optionValues Array of AccessConfigurations-identifiers (typically used by userTS with options.clearCache.identifier) |
||
327 | */ |
||
328 | public function manipulateCacheActions(&$cacheActions, &$optionValues) |
||
329 | { |
||
330 | if ($GLOBALS['BE_USER']->isAdmin()) { |
||
331 | $title = 'Initialize Solr connections'; |
||
332 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
333 | |||
334 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
335 | $cacheActions[] = [ |
||
336 | 'id' => 'clearSolrConnectionCache', |
||
337 | 'title' => $title, |
||
338 | 'href' => $uriBuilder->buildUriFromRoute('ajax_solr_updateConnections'), |
||
339 | 'icon' => $iconFactory->getIcon('extensions-solr-module-initsolrconnections', Icon::SIZE_SMALL) |
||
340 | ]; |
||
341 | $optionValues[] = 'clearSolrConnectionCache'; |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Updates the connections in the registry. |
||
347 | * |
||
348 | */ |
||
349 | 2 | public function updateConnections() |
|
350 | { |
||
351 | 2 | $solrConnections = $this->getConfiguredSolrConnections(); |
|
352 | 2 | $solrConnections = $this->filterDuplicateConnections($solrConnections); |
|
353 | |||
354 | 2 | if (!empty($solrConnections)) { |
|
355 | 2 | $this->setAllConfigurations($solrConnections); |
|
356 | 2 | } |
|
357 | 2 | } |
|
358 | |||
359 | /** |
||
360 | * Entrypoint for the ajax request |
||
361 | */ |
||
362 | public function updateConnectionsInCacheMenu() |
||
366 | |||
367 | /** |
||
368 | * Updates the Solr connections for a specific root page ID / site. |
||
369 | * |
||
370 | * @param int $rootPageId A site root page id |
||
371 | */ |
||
372 | 1 | public function updateConnectionByRootPageId($rootPageId) |
|
373 | { |
||
374 | 1 | $systemLanguages = $this->getSystemLanguages(); |
|
375 | 1 | $siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
|
376 | 1 | $site = $siteRepository->getSiteByRootPageId($rootPageId); |
|
377 | 1 | $rootPage = $site->getRootPage(); |
|
378 | |||
379 | 1 | $updatedSolrConnections = []; |
|
380 | 1 | foreach ($systemLanguages as $languageId) { |
|
381 | 1 | $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, $languageId); |
|
382 | |||
383 | 1 | if (!empty($connection)) { |
|
384 | 1 | $updatedSolrConnections[$connection['connectionKey']] = $connection; |
|
385 | 1 | } |
|
386 | 1 | } |
|
387 | |||
388 | 1 | $solrConnections = $this->getAllConfigurations(); |
|
389 | 1 | $solrConnections = array_merge($solrConnections, $updatedSolrConnections); |
|
390 | 1 | $solrConnections = $this->filterDuplicateConnections($solrConnections); |
|
391 | 1 | $this->setAllConfigurations($solrConnections); |
|
392 | 1 | } |
|
393 | |||
394 | /** |
||
395 | * Finds the configured Solr connections. Also respects multi-site |
||
396 | * environments. |
||
397 | * |
||
398 | * @return array An array with connections, each connection with keys rootPageTitle, rootPageUid, solrHost, solrPort, solrPath |
||
399 | */ |
||
400 | 2 | protected function getConfiguredSolrConnections() |
|
401 | { |
||
402 | 2 | $configuredSolrConnections = []; |
|
403 | |||
404 | // find website roots and languages for this installation |
||
405 | 2 | $rootPages = $this->getRootPages(); |
|
406 | 2 | $languages = $this->getSystemLanguages(); |
|
407 | |||
408 | // find solr configurations and add them as function menu entries |
||
409 | 2 | foreach ($rootPages as $rootPage) { |
|
410 | 2 | foreach ($languages as $languageId) { |
|
411 | 2 | $connection = $this->getConfiguredSolrConnectionByRootPage($rootPage, |
|
412 | 2 | $languageId); |
|
413 | |||
414 | 2 | if (!empty($connection)) { |
|
415 | 2 | $configuredSolrConnections[$connection['connectionKey']] = $connection; |
|
416 | 2 | } |
|
417 | 2 | } |
|
418 | 2 | } |
|
419 | |||
420 | 2 | return $configuredSolrConnections; |
|
421 | } |
||
422 | |||
423 | /** |
||
424 | * Gets the configured Solr connection for a specific root page and language ID. |
||
425 | * |
||
426 | * @param array $rootPage A root page record with at least title and uid |
||
427 | * @param int $languageId ID of a system language |
||
428 | * @return array A solr connection configuration. |
||
429 | */ |
||
430 | 3 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
482 | |||
483 | /** |
||
484 | * Gets the language name for a given language ID. |
||
485 | * |
||
486 | * @param int $languageId language ID |
||
487 | * @return string Language name |
||
488 | */ |
||
489 | 3 | protected function getLanguageName($languageId) |
|
490 | { |
||
491 | 3 | $languageName = ''; |
|
492 | |||
493 | 3 | $language = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
494 | 3 | 'uid, title', |
|
495 | 3 | 'sys_language', |
|
496 | 'uid = ' . (integer)$languageId |
||
497 | 3 | ); |
|
498 | |||
499 | 3 | if (count($language)) { |
|
500 | $languageName = $language[0]['title']; |
||
501 | 3 | } elseif ($languageId == 0) { |
|
502 | 3 | $languageName = 'default'; |
|
503 | 3 | } |
|
504 | |||
505 | 3 | return $languageName; |
|
506 | } |
||
507 | |||
508 | /** |
||
509 | * Creates a human readable label from the connections' configuration. |
||
510 | * |
||
511 | * @param array $connection Connection configuration |
||
512 | * @return string Connection label |
||
513 | */ |
||
514 | 3 | protected function buildConnectionLabel(array $connection) |
|
527 | |||
528 | /** |
||
529 | * Filters duplicate connections. When detecting the configured connections |
||
530 | * this is done with a little brute force by simply combining all root pages |
||
531 | * with all languages, this method filters out the duplicates. |
||
532 | * |
||
533 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
534 | * @return array An array with connections, no duplicates. |
||
535 | */ |
||
536 | 3 | protected function filterDuplicateConnections(array $connections) |
|
537 | { |
||
538 | 3 | $hashedConnections = []; |
|
539 | 3 | $filteredConnections = []; |
|
540 | |||
541 | // array_unique() doesn't work on multi dimensional arrays, so we need to flatten it first |
||
542 | 3 | foreach ($connections as $key => $connection) { |
|
543 | 3 | unset($connection['language']); |
|
544 | 3 | $connectionHash = md5(implode('|', $connection)); |
|
545 | 3 | $hashedConnections[$key] = $connectionHash; |
|
546 | 3 | } |
|
547 | |||
548 | 3 | $hashedConnections = array_unique($hashedConnections); |
|
549 | |||
550 | 3 | foreach ($hashedConnections as $key => $hash) { |
|
551 | 3 | $filteredConnections[$key] = $connections[$key]; |
|
552 | 3 | } |
|
553 | |||
554 | 3 | return $filteredConnections; |
|
555 | } |
||
556 | |||
557 | /** |
||
558 | * Finds the system's configured languages. |
||
559 | * |
||
560 | * @return array An array of language IDs |
||
561 | */ |
||
562 | 3 | protected function getSystemLanguages() |
|
563 | { |
||
564 | 3 | $languages = [0]; |
|
565 | |||
566 | 3 | $languageRecords = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
|
567 | 3 | 'uid', |
|
568 | 3 | 'sys_language', |
|
569 | 'hidden = 0' |
||
570 | 3 | ); |
|
571 | |||
572 | 3 | if (!is_array($languageRecords)) { |
|
573 | return $languages; |
||
574 | } |
||
575 | |||
576 | 3 | foreach ($languageRecords as $languageRecord) { |
|
577 | $languages[] = $languageRecord['uid']; |
||
578 | 3 | } |
|
579 | 3 | return $languages; |
|
580 | } |
||
581 | |||
582 | /** |
||
583 | * Gets the site's root pages. The "Is root of website" flag must be set, |
||
584 | * which usually is the case for pages with pid = 0. |
||
585 | * |
||
586 | * @return array An array of (partial) root page records, containing the uid and title fields |
||
587 | */ |
||
588 | 2 | protected function getRootPages() |
|
598 | } |
||
599 |