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\Records\SystemLanguage\SystemLanguageRepository |
||
59 | */ |
||
60 | protected $systemLanguageRepository; |
||
61 | |||
62 | /** |
||
63 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
64 | */ |
||
65 | protected $logger = null; |
||
66 | |||
67 | /** |
||
68 | * @var PagesRepositoryAtExtSolr |
||
69 | */ |
||
70 | protected $pagesRepositoryAtExtSolr; |
||
71 | |||
72 | /** |
||
73 | * @param SystemLanguageRepository $systemLanguageRepository |
||
74 | * @param PagesRepositoryAtExtSolr|null $pagesRepositoryAtExtSolr |
||
75 | * @param SolrLogManager $solrLogManager |
||
76 | */ |
||
77 | 115 | public function __construct(SystemLanguageRepository $systemLanguageRepository = null, PagesRepositoryAtExtSolr $pagesRepositoryAtExtSolr = null, SolrLogManager $solrLogManager = null) |
|
78 | { |
||
79 | 115 | $this->systemLanguageRepository = isset($systemLanguageRepository) ? $systemLanguageRepository : GeneralUtility::makeInstance(SystemLanguageRepository::class); |
|
80 | 115 | $this->pagesRepositoryAtExtSolr = isset($pagesRepositoryAtExtSolr) ? $pagesRepositoryAtExtSolr : GeneralUtility::makeInstance(PagesRepositoryAtExtSolr::class); |
|
81 | 115 | $this->logger = isset($solrLogManager) ? $solrLogManager : GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__); |
|
82 | 115 | } |
|
83 | |||
84 | /** |
||
85 | * Gets a Solr connection. |
||
86 | * |
||
87 | * Instead of generating a new connection with each call, connections are |
||
88 | * kept and checked whether the requested connection already exists. If a |
||
89 | * connection already exists, it's reused. |
||
90 | * |
||
91 | * @param string $host Solr host (optional) |
||
92 | * @param int $port Solr port (optional) |
||
93 | * @param string $path Solr path (optional) |
||
94 | * @param string $scheme Solr scheme, defaults to http, can be https (optional) |
||
95 | * @param string $username Solr user name (optional) |
||
96 | * @param string $password Solr password (optional) |
||
97 | * @return SolrConnection A solr connection. |
||
98 | */ |
||
99 | 108 | public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '') |
|
100 | { |
||
101 | 108 | if (empty($host)) { |
|
102 | 1 | $this->logger->log( |
|
103 | 1 | SolrLogManager::WARNING, |
|
104 | 1 | '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.' |
|
105 | ); |
||
106 | |||
107 | 1 | $configuration = Util::getSolrConfiguration(); |
|
108 | 1 | $host = $configuration->getSolrHost(); |
|
109 | 1 | $port = $configuration->getSolrPort(); |
|
110 | 1 | $path = $configuration->getSolrPath(); |
|
111 | 1 | $scheme = $configuration->getSolrScheme(); |
|
112 | 1 | $username = $configuration->getSolrUsername(); |
|
113 | 1 | $password = $configuration->getSolrPassword(); |
|
114 | } |
||
115 | |||
116 | 108 | $connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password); |
|
117 | 108 | if (!isset(self::$connections[$connectionHash])) { |
|
118 | 108 | $connection = $this->buildSolrConnection($host, $port, $path, $scheme, $username, $password); |
|
119 | 108 | self::$connections[$connectionHash] = $connection; |
|
120 | } |
||
121 | |||
122 | 108 | return self::$connections[$connectionHash]; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create a Solr Service instance from the passed connection configuration. |
||
127 | * |
||
128 | * @param string $host |
||
129 | * @param int $port |
||
130 | * @param string $path |
||
131 | * @param string $scheme |
||
132 | * @param string $username |
||
133 | * @param string $password |
||
134 | * @return SolrConnection|object |
||
135 | */ |
||
136 | 103 | protected function buildSolrConnection($host, $port, $path, $scheme, $username = '', $password = '') |
|
137 | { |
||
138 | 103 | return GeneralUtility::makeInstance(SolrConnection::class, $host, $port, $path, $scheme, $username, $password); |
|
139 | } |
||
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 SolrConnection |
||
146 | */ |
||
147 | 101 | 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 | 91 | 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 SolrConnection A solr connection. |
||
201 | * @throws NoSolrConnectionFoundException |
||
202 | */ |
||
203 | 91 | 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 | 92 | 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 SolrConnection A solr connection. |
||
248 | * @throws NoSolrConnectionFoundException |
||
249 | */ |
||
250 | 10 | 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 | 105 | 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 SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection 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 | 31 | 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 SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection) |
||
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() |
|
429 | |||
430 | /** |
||
431 | * Gets the configured Solr connection for a specific root page and language ID. |
||
432 | * |
||
433 | * @param array $rootPage A root page record with at least title and uid |
||
434 | * @param int $languageId ID of a system language |
||
435 | * @return array A solr connection configuration. |
||
436 | */ |
||
437 | 3 | protected function getConfiguredSolrConnectionByRootPage(array $rootPage, $languageId) |
|
489 | |||
490 | |||
491 | |||
492 | /** |
||
493 | * Creates a human readable label from the connections' configuration. |
||
494 | * |
||
495 | * @param array $connection Connection configuration |
||
496 | * @return string Connection label |
||
497 | */ |
||
498 | 3 | protected function buildConnectionLabel(array $connection) |
|
509 | |||
510 | /** |
||
511 | * Filters duplicate connections. When detecting the configured connections |
||
512 | * this is done with a little brute force by simply combining all root pages |
||
513 | * with all languages, this method filters out the duplicates. |
||
514 | * |
||
515 | * @param array $connections An array of unfiltered connections, containing duplicates |
||
516 | * @return array An array with connections, no duplicates. |
||
517 | */ |
||
518 | 3 | protected function filterDuplicateConnections(array $connections) |
|
538 | } |
||
539 |