Complex classes like DocumentManager 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 DocumentManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class DocumentManager implements ObjectManager |
||
43 | { |
||
44 | public const CLIENT_TYPEMAP = ['root' => 'array', 'document' => 'array']; |
||
45 | |||
46 | /** |
||
47 | * The Doctrine MongoDB connection instance. |
||
48 | * |
||
49 | * @var Client |
||
50 | */ |
||
51 | private $client; |
||
52 | |||
53 | /** |
||
54 | * The used Configuration. |
||
55 | * |
||
56 | * @var Configuration |
||
57 | */ |
||
58 | private $config; |
||
59 | |||
60 | /** |
||
61 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
62 | * |
||
63 | * @var ClassMetadataFactory |
||
64 | */ |
||
65 | private $metadataFactory; |
||
66 | |||
67 | /** |
||
68 | * The UnitOfWork used to coordinate object-level transactions. |
||
69 | * |
||
70 | * @var UnitOfWork |
||
71 | */ |
||
72 | private $unitOfWork; |
||
73 | |||
74 | /** |
||
75 | * The event manager that is the central point of the event system. |
||
76 | * |
||
77 | * @var EventManager |
||
78 | */ |
||
79 | private $eventManager; |
||
80 | |||
81 | /** |
||
82 | * The Hydrator factory instance. |
||
83 | * |
||
84 | * @var HydratorFactory |
||
85 | */ |
||
86 | private $hydratorFactory; |
||
87 | |||
88 | /** |
||
89 | * The Proxy factory instance. |
||
90 | * |
||
91 | * @var ProxyFactory |
||
92 | */ |
||
93 | private $proxyFactory; |
||
94 | |||
95 | /** |
||
96 | * The repository factory used to create dynamic repositories. |
||
97 | * |
||
98 | * @var RepositoryFactory |
||
99 | */ |
||
100 | private $repositoryFactory; |
||
101 | |||
102 | /** |
||
103 | * SchemaManager instance |
||
104 | * |
||
105 | * @var SchemaManager |
||
106 | */ |
||
107 | private $schemaManager; |
||
108 | |||
109 | /** |
||
110 | * Array of cached document database instances that are lazily loaded. |
||
111 | * |
||
112 | * @var Database[] |
||
113 | */ |
||
114 | private $documentDatabases = []; |
||
115 | |||
116 | /** |
||
117 | * Array of cached document collection instances that are lazily loaded. |
||
118 | * |
||
119 | * @var Collection[] |
||
120 | */ |
||
121 | private $documentCollections = []; |
||
122 | |||
123 | /** |
||
124 | * Array of cached document bucket instances that are lazily loaded. |
||
125 | * |
||
126 | * @var Bucket[] |
||
127 | */ |
||
128 | private $documentBuckets = []; |
||
129 | |||
130 | /** |
||
131 | * Whether the DocumentManager is closed or not. |
||
132 | * |
||
133 | * @var bool |
||
134 | */ |
||
135 | private $closed = false; |
||
136 | |||
137 | /** |
||
138 | * Collection of query filters. |
||
139 | * |
||
140 | * @var FilterCollection |
||
141 | */ |
||
142 | private $filterCollection; |
||
143 | |||
144 | /** |
||
145 | * Creates a new Document that operates on the given Mongo connection |
||
146 | * and uses the given Configuration. |
||
147 | */ |
||
148 | 1634 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
149 | { |
||
150 | 1634 | $this->config = $config ?: new Configuration(); |
|
151 | 1634 | $this->eventManager = $eventManager ?: new EventManager(); |
|
152 | 1634 | $this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => self::CLIENT_TYPEMAP]); |
|
153 | |||
154 | 1634 | $this->checkTypeMap(); |
|
155 | |||
156 | 1634 | $metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); |
|
157 | 1634 | $this->metadataFactory = new $metadataFactoryClassName(); |
|
158 | 1634 | $this->metadataFactory->setDocumentManager($this); |
|
159 | 1634 | $this->metadataFactory->setConfiguration($this->config); |
|
160 | |||
161 | 1634 | $cacheDriver = $this->config->getMetadataCacheImpl(); |
|
162 | 1634 | if ($cacheDriver) { |
|
163 | $this->metadataFactory->setCacheDriver($cacheDriver); |
||
164 | } |
||
165 | |||
166 | 1634 | $hydratorDir = $this->config->getHydratorDir(); |
|
167 | 1634 | $hydratorNs = $this->config->getHydratorNamespace(); |
|
168 | 1634 | $this->hydratorFactory = new HydratorFactory( |
|
169 | 1634 | $this, |
|
170 | 1634 | $this->eventManager, |
|
171 | 1634 | $hydratorDir, |
|
172 | 1634 | $hydratorNs, |
|
173 | 1634 | $this->config->getAutoGenerateHydratorClasses() |
|
174 | ); |
||
175 | |||
176 | 1634 | $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); |
|
177 | 1634 | $this->hydratorFactory->setUnitOfWork($this->unitOfWork); |
|
178 | 1634 | $this->schemaManager = new SchemaManager($this, $this->metadataFactory); |
|
179 | 1634 | $this->proxyFactory = new StaticProxyFactory($this); |
|
180 | 1634 | $this->repositoryFactory = $this->config->getRepositoryFactory(); |
|
181 | 1634 | } |
|
182 | |||
183 | /** |
||
184 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
185 | */ |
||
186 | 1 | public function getProxyFactory() : ProxyFactory |
|
187 | { |
||
188 | 1 | return $this->proxyFactory; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Creates a new Document that operates on the given Mongo connection |
||
193 | * and uses the given Configuration. |
||
194 | */ |
||
195 | 1634 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
|
196 | { |
||
197 | 1634 | return new static($client, $config, $eventManager); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Gets the EventManager used by the DocumentManager. |
||
202 | */ |
||
203 | 1697 | public function getEventManager() : EventManager |
|
204 | { |
||
205 | 1697 | return $this->eventManager; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
210 | */ |
||
211 | 1634 | public function getClient() : Client |
|
215 | |||
216 | /** |
||
217 | * Gets the metadata factory used to gather the metadata of classes. |
||
218 | * |
||
219 | * @return ClassMetadataFactory |
||
220 | */ |
||
221 | 5 | public function getMetadataFactory() |
|
225 | |||
226 | /** |
||
227 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
228 | * |
||
229 | * This method is a no-op for other objects. |
||
230 | * |
||
231 | * @param object $obj |
||
232 | */ |
||
233 | public function initializeObject($obj) |
||
237 | |||
238 | /** |
||
239 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
240 | */ |
||
241 | 1641 | public function getUnitOfWork() : UnitOfWork |
|
242 | { |
||
243 | 1641 | return $this->unitOfWork; |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
248 | * for each type of document. |
||
249 | */ |
||
250 | 70 | public function getHydratorFactory() : HydratorFactory |
|
251 | { |
||
252 | 70 | return $this->hydratorFactory; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
257 | */ |
||
258 | 28 | public function getSchemaManager() : SchemaManager |
|
259 | { |
||
260 | 28 | return $this->schemaManager; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns the metadata for a class. |
||
265 | * |
||
266 | * @internal Performance-sensitive method. |
||
267 | * |
||
268 | * @param string $className The class name. |
||
269 | */ |
||
270 | 1370 | public function getClassMetadata($className) : ClassMetadata |
|
271 | { |
||
272 | 1370 | return $this->metadataFactory->getMetadataFor(ClassNameResolver::getRealClass($className)); |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * Returns the MongoDB instance for a class. |
||
277 | */ |
||
278 | 1299 | public function getDocumentDatabase(string $className) : Database |
|
279 | { |
||
280 | 1299 | $className = ClassNameResolver::getRealClass($className); |
|
281 | |||
282 | 1299 | if (isset($this->documentDatabases[$className])) { |
|
283 | 46 | return $this->documentDatabases[$className]; |
|
284 | } |
||
285 | |||
286 | 1294 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
287 | 1294 | $db = $metadata->getDatabase(); |
|
288 | 1294 | $db = $db ?: $this->config->getDefaultDB(); |
|
289 | 1294 | $db = $db ?: 'doctrine'; |
|
290 | 1294 | $this->documentDatabases[$className] = $this->client->selectDatabase($db); |
|
291 | |||
292 | 1294 | return $this->documentDatabases[$className]; |
|
293 | } |
||
294 | |||
295 | /** |
||
296 | * Gets the array of instantiated document database instances. |
||
297 | * |
||
298 | * @return Database[] |
||
299 | */ |
||
300 | public function getDocumentDatabases() : array |
||
301 | { |
||
302 | return $this->documentDatabases; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Returns the collection instance for a class. |
||
307 | * |
||
308 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
309 | */ |
||
310 | 1302 | public function getDocumentCollection(string $className) : Collection |
|
311 | { |
||
312 | 1302 | $className = ClassNameResolver::getRealClass($className); |
|
313 | |||
314 | /** @var ClassMetadata $metadata */ |
||
315 | 1302 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
316 | 1302 | if ($metadata->isFile) { |
|
317 | 16 | return $this->getDocumentBucket($className)->getFilesCollection(); |
|
318 | } |
||
319 | |||
320 | 1291 | $collectionName = $metadata->getCollection(); |
|
321 | |||
322 | 1291 | if (! $collectionName) { |
|
323 | throw MongoDBException::documentNotMappedToCollection($className); |
||
324 | } |
||
325 | |||
326 | 1291 | if (! isset($this->documentCollections[$className])) { |
|
327 | 1281 | $db = $this->getDocumentDatabase($className); |
|
328 | |||
329 | 1281 | $options = []; |
|
330 | 1281 | if ($metadata->readPreference !== null) { |
|
331 | 3 | $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
|
332 | } |
||
333 | |||
334 | 1281 | $this->documentCollections[$className] = $db->selectCollection($collectionName, $options); |
|
335 | } |
||
336 | |||
337 | 1291 | return $this->documentCollections[$className]; |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * Returns the bucket instance for a class. |
||
342 | * |
||
343 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
344 | */ |
||
345 | 16 | public function getDocumentBucket(string $className) : Bucket |
|
346 | { |
||
347 | 16 | $className = ClassNameResolver::getRealClass($className); |
|
348 | |||
349 | /** @var ClassMetadata $metadata */ |
||
350 | 16 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
351 | 16 | if (! $metadata->isFile) { |
|
352 | throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className); |
||
353 | } |
||
354 | |||
355 | 16 | $bucketName = $metadata->getBucketName(); |
|
356 | |||
357 | 16 | if (! $bucketName) { |
|
|
|||
358 | throw MongoDBException::documentNotMappedToCollection($className); |
||
359 | } |
||
360 | |||
361 | 16 | if (! isset($this->documentBuckets[$className])) { |
|
362 | 11 | $db = $this->getDocumentDatabase($className); |
|
363 | |||
364 | 11 | $options = ['bucketName' => $bucketName]; |
|
365 | 11 | if ($metadata->readPreference !== null) { |
|
366 | $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
||
367 | } |
||
368 | |||
369 | 11 | $this->documentBuckets[$className] = $db->selectGridFSBucket($options); |
|
370 | } |
||
371 | |||
372 | 16 | return $this->documentBuckets[$className]; |
|
373 | } |
||
374 | |||
375 | /** |
||
376 | * Gets the array of instantiated document collection instances. |
||
377 | * |
||
378 | * @return Collection[] |
||
379 | */ |
||
380 | public function getDocumentCollections() : array |
||
381 | { |
||
382 | return $this->documentCollections; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Create a new Query instance for a class. |
||
387 | * |
||
388 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
389 | */ |
||
390 | 182 | public function createQueryBuilder($documentName = null) : Query\Builder |
|
391 | { |
||
392 | 182 | return new Query\Builder($this, $documentName); |
|
393 | } |
||
394 | |||
395 | /** |
||
396 | * Creates a new aggregation builder instance for a class. |
||
397 | */ |
||
398 | 41 | public function createAggregationBuilder(string $documentName) : Aggregation\Builder |
|
399 | { |
||
400 | 41 | return new Aggregation\Builder($this, $documentName); |
|
401 | } |
||
402 | |||
403 | /** |
||
404 | * Tells the DocumentManager to make an instance managed and persistent. |
||
405 | * |
||
406 | * The document will be entered into the database at or before transaction |
||
407 | * commit or as a result of the flush operation. |
||
408 | * |
||
409 | * NOTE: The persist operation always considers documents that are not yet known to |
||
410 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
411 | * |
||
412 | * @param object $document The instance to make managed and persistent. |
||
413 | * |
||
414 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
415 | */ |
||
416 | 614 | public function persist($document) |
|
424 | |||
425 | /** |
||
426 | * Removes a document instance. |
||
427 | * |
||
428 | * A removed document will be removed from the database at or before transaction commit |
||
429 | * or as a result of the flush operation. |
||
430 | * |
||
431 | * @param object $document The document instance to remove. |
||
432 | * |
||
433 | * @throws InvalidArgumentException When the $document param is not an object. |
||
434 | */ |
||
435 | 27 | public function remove($document) |
|
436 | { |
||
437 | 27 | if (! is_object($document)) { |
|
438 | 1 | throw new InvalidArgumentException(gettype($document)); |
|
439 | } |
||
443 | |||
444 | /** |
||
445 | * Refreshes the persistent state of a document from the database, |
||
446 | * overriding any local changes that have not yet been persisted. |
||
447 | * |
||
448 | * @param object $document The document to refresh. |
||
449 | * |
||
450 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
451 | */ |
||
452 | 26 | public function refresh($document) |
|
460 | |||
461 | /** |
||
462 | * Detaches a document from the DocumentManager, causing a managed document to |
||
463 | * become detached. Unflushed changes made to the document if any |
||
464 | * (including removal of the document), will not be synchronized to the database. |
||
465 | * Documents which previously referenced the detached document will continue to |
||
466 | * reference it. |
||
467 | * |
||
468 | * @param object $document The document to detach. |
||
469 | * |
||
470 | * @throws InvalidArgumentException When the $document param is not an object. |
||
471 | */ |
||
472 | 11 | public function detach($document) |
|
479 | |||
480 | /** |
||
481 | * Merges the state of a detached document into the persistence context |
||
482 | * of this DocumentManager and returns the managed copy of the document. |
||
483 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
484 | * |
||
485 | * @param object $document The detached document to merge into the persistence context. |
||
486 | * |
||
487 | * @return object The managed copy of the document. |
||
488 | * |
||
489 | * @throws LockException |
||
490 | * @throws InvalidArgumentException If the $document param is not an object. |
||
491 | */ |
||
492 | 13 | public function merge($document) |
|
500 | |||
501 | /** |
||
502 | * Acquire a lock on the given document. |
||
503 | * |
||
504 | * @throws InvalidArgumentException |
||
505 | * @throws LockException |
||
506 | */ |
||
507 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
511 | |||
512 | /** |
||
513 | * Releases a lock on the given document. |
||
514 | */ |
||
515 | 1 | public function unlock(object $document) : void |
|
519 | |||
520 | /** |
||
521 | * Gets the repository for a document class. |
||
522 | * |
||
523 | * @param string $documentName The name of the Document. |
||
524 | * |
||
525 | * @return ObjectRepository The repository. |
||
526 | */ |
||
527 | 357 | public function getRepository($documentName) |
|
531 | |||
532 | /** |
||
533 | * Flushes all changes to objects that have been queued up to now to the database. |
||
534 | * This effectively synchronizes the in-memory state of managed objects with the |
||
535 | * database. |
||
536 | * |
||
537 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
538 | * |
||
539 | * @throws MongoDBException |
||
540 | */ |
||
541 | 586 | public function flush(array $options = []) |
|
546 | |||
547 | /** |
||
548 | * Gets a reference to the document identified by the given type and identifier |
||
549 | * without actually loading it. |
||
550 | * |
||
551 | * If partial objects are allowed, this method will return a partial object that only |
||
552 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
553 | * loads itself on first access. |
||
554 | * |
||
555 | * @param string|object $identifier |
||
556 | */ |
||
557 | 133 | public function getReference(string $documentName, $identifier) : object |
|
573 | |||
574 | /** |
||
575 | * Gets a partial reference to the document identified by the given type and identifier |
||
576 | * without actually loading it, if the document is not yet loaded. |
||
577 | * |
||
578 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
579 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
580 | * Thus you can only ever safely access the identifier of a document obtained through |
||
581 | * this method. |
||
582 | * |
||
583 | * The use-cases for partial references involve maintaining bidirectional associations |
||
584 | * without loading one side of the association or to update a document without loading it. |
||
585 | * Note, however, that in the latter case the original (persistent) document data will |
||
586 | * never be visible to the application (especially not event listeners) as it will |
||
587 | * never be loaded in the first place. |
||
588 | * |
||
589 | * @param mixed $identifier The document identifier. |
||
590 | */ |
||
591 | 1 | public function getPartialReference(string $documentName, $identifier) : object |
|
606 | |||
607 | /** |
||
608 | * Finds a Document by its identifier. |
||
609 | * |
||
610 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
611 | * |
||
612 | * @param string $documentName |
||
613 | * @param mixed $identifier |
||
614 | * @param int $lockMode |
||
615 | * @param int $lockVersion |
||
616 | * |
||
617 | * @return object $document |
||
618 | */ |
||
619 | 186 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
623 | |||
624 | /** |
||
625 | * Clears the DocumentManager. |
||
626 | * |
||
627 | * All documents that are currently managed by this DocumentManager become |
||
628 | * detached. |
||
629 | * |
||
630 | * @param string|null $documentName if given, only documents of this type will get detached |
||
631 | */ |
||
632 | 389 | public function clear($documentName = null) |
|
636 | |||
637 | /** |
||
638 | * Closes the DocumentManager. All documents that are currently managed |
||
639 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
640 | * be used after it is closed. |
||
641 | */ |
||
642 | 6 | public function close() |
|
647 | |||
648 | /** |
||
649 | * Determines whether a document instance is managed in this DocumentManager. |
||
650 | * |
||
651 | * @param object $document |
||
652 | * |
||
653 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
654 | * |
||
655 | * @throws InvalidArgumentException When the $document param is not an object. |
||
656 | */ |
||
657 | 3 | public function contains($document) |
|
666 | |||
667 | /** |
||
668 | * Gets the Configuration used by the DocumentManager. |
||
669 | */ |
||
670 | 1697 | public function getConfiguration() : Configuration |
|
674 | |||
675 | /** |
||
676 | * Returns a reference to the supplied document. |
||
677 | * |
||
678 | * @return mixed The reference for the document in question, according to the desired mapping |
||
679 | * |
||
680 | * @throws MappingException |
||
681 | * @throws RuntimeException |
||
682 | */ |
||
683 | 225 | public function createReference(object $document, array $referenceMapping) |
|
760 | |||
761 | /** |
||
762 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
763 | * |
||
764 | * @throws MongoDBException If the DocumentManager is closed. |
||
765 | */ |
||
766 | 619 | private function errorIfClosed() : void |
|
772 | |||
773 | /** |
||
774 | * Check if the Document manager is open or closed. |
||
775 | */ |
||
776 | 1 | public function isOpen() : bool |
|
780 | |||
781 | /** |
||
782 | * Gets the filter collection. |
||
783 | */ |
||
784 | 536 | public function getFilterCollection() : FilterCollection |
|
792 | |||
793 | 1634 | private function checkTypeMap() : void |
|
803 | } |
||
804 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: