Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Client |
||
23 | { |
||
24 | const DEFAULT_DSN = 'mongodb://127.0.0.1'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $dsn = self::DEFAULT_DSN; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $connectOptions = array(); |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var \MongoClient |
||
39 | */ |
||
40 | private $mongoClient; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $databasePool = array(); |
||
46 | |||
47 | /** |
||
48 | * @var array Database to class mapping |
||
49 | */ |
||
50 | private $mapping = array(); |
||
51 | |||
52 | /** |
||
53 | * @var LoggerInterface|null |
||
54 | */ |
||
55 | private $logger; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $currentDatabaseName; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @var string version of MongoDb |
||
65 | */ |
||
66 | private $dbVersion; |
||
67 | |||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | private $debug = false; |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * @param string $dsn Data Source Name |
||
76 | * @param array $options |
||
77 | */ |
||
78 | public function __construct($dsn = null, array $options = null) |
||
88 | |||
89 | /** |
||
90 | * Check if client emulates ext-mongo driver by new ext-mongodb extension |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public static function isEmulationMode() |
||
98 | |||
99 | /** |
||
100 | * Set credentials to auth on db, specified in connect options or dsn. |
||
101 | * If not specified - auth on admin db |
||
102 | * |
||
103 | * @param string $username |
||
104 | * @param string $password |
||
105 | * @return \Sokil\Mongo\Client |
||
106 | */ |
||
107 | public function setCredentials($username, $password) |
||
114 | |||
115 | public function __get($name) |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | * @return string Version of PHP driver |
||
123 | */ |
||
124 | public function getVersion() |
||
128 | |||
129 | /** |
||
130 | * |
||
131 | * @return string version of mongo database |
||
132 | */ |
||
133 | public function getDbVersion() |
||
146 | |||
147 | public function setDsn($dsn) |
||
152 | |||
153 | public function getDsn() |
||
157 | |||
158 | /** |
||
159 | * Set connect options |
||
160 | * |
||
161 | * @link http://php.net/manual/en/mongoclient.construct.php connect options |
||
162 | * @param array $options |
||
163 | * @return \Sokil\Mongo\Client |
||
164 | */ |
||
165 | public function setConnectOptions(array $options) |
||
170 | |||
171 | public function getConnectOptions() |
||
175 | |||
176 | /** |
||
177 | * Set mongo's client |
||
178 | * |
||
179 | * @param \MongoClient $client |
||
180 | * @return \Sokil\Mongo\Client |
||
181 | */ |
||
182 | public function setMongoClient(\MongoClient $client) |
||
187 | |||
188 | /** |
||
189 | * Get mongo connection instance |
||
190 | * |
||
191 | * @return \MongoClient |
||
192 | * @throws Exception |
||
193 | */ |
||
194 | public function getMongoClient() |
||
207 | |||
208 | /** |
||
209 | * Get list of all active connections through this client |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function getConnections() |
||
217 | |||
218 | /** |
||
219 | * Map database and collection name to class. |
||
220 | * |
||
221 | * Collection name -> array definition: |
||
222 | * ['acmeDatabaseName' => ['acmeCollectionName' => ['class' => '\Acme\Collection\SomeCollectionClass']]] |
||
223 | * Collection name -> collection class name (deprecated: use definition array): |
||
224 | * ['acmeDatabaseName' => ['acmeCollectionName' => '\Acme\Collection\SomeCollectionClass']] |
||
225 | * Collection's class namespace (deprecated: use definition array): |
||
226 | * ['acmeDatabaseName' => '\Acme\Collection'] |
||
227 | * |
||
228 | * @param array $mapping classpath or class prefix |
||
229 | * @return Client |
||
230 | */ |
||
231 | public function map(array $mapping) |
||
237 | |||
238 | /** |
||
239 | * Get database instance |
||
240 | * |
||
241 | * @param string $name database name |
||
242 | * @return Database |
||
243 | */ |
||
244 | public function getDatabase($name = null) |
||
263 | |||
264 | /** |
||
265 | * Select database |
||
266 | * |
||
267 | * @param string $name |
||
268 | * @return \Sokil\Mongo\Client |
||
269 | */ |
||
270 | public function useDatabase($name) |
||
275 | |||
276 | /** |
||
277 | * Get name of current database |
||
278 | * |
||
279 | * @return string |
||
280 | * @throws Exception |
||
281 | */ |
||
282 | public function getCurrentDatabaseName() |
||
290 | |||
291 | /** |
||
292 | * Get collection from previously selected database by self::useDatabase() |
||
293 | * |
||
294 | * @param string $name |
||
295 | * @return \Sokil\Mongo\Collection |
||
296 | * @throws Exception |
||
297 | */ |
||
298 | public function getCollection($name) |
||
304 | |||
305 | public function readPrimaryOnly() |
||
310 | |||
311 | public function readPrimaryPreferred(array $tags = null) |
||
316 | |||
317 | public function readSecondaryOnly(array $tags = null) |
||
322 | |||
323 | public function readSecondaryPreferred(array $tags = null) |
||
328 | |||
329 | public function readNearest(array $tags = null) |
||
334 | |||
335 | public function getReadPreference() |
||
339 | |||
340 | public function setLogger(LoggerInterface $logger) |
||
345 | |||
346 | /** |
||
347 | * |
||
348 | * @return \Psr\Log\LoggerInterface |
||
349 | */ |
||
350 | public function getLogger() |
||
354 | |||
355 | /** |
||
356 | * Check if logger defined |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function hasLogger() |
||
364 | |||
365 | /** |
||
366 | * Remove logger |
||
367 | * |
||
368 | * @return \Sokil\Mongo\Client |
||
369 | */ |
||
370 | public function removeLogger() |
||
375 | |||
376 | /** |
||
377 | * Enable or disable debug mode |
||
378 | */ |
||
379 | public function debug($enabled = true) |
||
384 | |||
385 | /** |
||
386 | * Check state of debug mode |
||
387 | */ |
||
388 | public function isDebugEnabled() |
||
392 | |||
393 | /** |
||
394 | * Define write concern on whole requests |
||
395 | * |
||
396 | * @param string|integer $w write concern |
||
397 | * @param int $timeout timeout in milliseconds |
||
398 | * @return \Sokil\Mongo\Client |
||
399 | * |
||
400 | * @throws \Sokil\Mongo\Exception |
||
401 | */ |
||
402 | public function setWriteConcern($w, $timeout = 10000) |
||
410 | |||
411 | /** |
||
412 | * Define unacknowledged write concern on whole requests |
||
413 | * |
||
414 | * @param int $timeout timeout in milliseconds |
||
415 | * @return \Sokil\Mongo\Client |
||
416 | */ |
||
417 | public function setUnacknowledgedWriteConcern($timeout = 10000) |
||
422 | |||
423 | /** |
||
424 | * Define majority write concern on whole requests |
||
425 | * |
||
426 | * @param int $timeout timeout in milliseconds |
||
427 | * @return \Sokil\Mongo\Client |
||
428 | */ |
||
429 | public function setMajorityWriteConcern($timeout = 10000) |
||
434 | |||
435 | /** |
||
436 | * Get currently active write concern on connection level |
||
437 | * |
||
438 | * @return string|int |
||
439 | */ |
||
440 | public function getWriteConcern() |
||
444 | |||
445 | /** |
||
446 | * Create new persistence manager |
||
447 | * @return \Sokil\Mongo\Persistence |
||
448 | */ |
||
449 | public function createPersistence() |
||
459 | } |
||
460 |