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 |
||
| 39 | class DocumentManager implements ObjectManager |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * The Doctrine MongoDB connection instance. |
||
| 43 | * |
||
| 44 | * @var Client |
||
| 45 | */ |
||
| 46 | private $client; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The used Configuration. |
||
| 50 | * |
||
| 51 | * @var Configuration |
||
| 52 | */ |
||
| 53 | private $config; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
| 57 | * |
||
| 58 | * @var ClassMetadataFactory |
||
| 59 | */ |
||
| 60 | private $metadataFactory; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 64 | * |
||
| 65 | * @var UnitOfWork |
||
| 66 | */ |
||
| 67 | private $unitOfWork; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The event manager that is the central point of the event system. |
||
| 71 | * |
||
| 72 | * @var EventManager |
||
| 73 | */ |
||
| 74 | private $eventManager; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Hydrator factory instance. |
||
| 78 | * |
||
| 79 | * @var HydratorFactory |
||
| 80 | */ |
||
| 81 | private $hydratorFactory; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The Proxy factory instance. |
||
| 85 | * |
||
| 86 | * @var ProxyFactory |
||
| 87 | */ |
||
| 88 | private $proxyFactory; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The repository factory used to create dynamic repositories. |
||
| 92 | * |
||
| 93 | * @var RepositoryFactory |
||
| 94 | */ |
||
| 95 | private $repositoryFactory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * SchemaManager instance |
||
| 99 | * |
||
| 100 | * @var SchemaManager |
||
| 101 | */ |
||
| 102 | private $schemaManager; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Array of cached document database instances that are lazily loaded. |
||
| 106 | * |
||
| 107 | * @var Database[] |
||
| 108 | */ |
||
| 109 | private $documentDatabases = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Array of cached document collection instances that are lazily loaded. |
||
| 113 | * |
||
| 114 | * @var Collection[] |
||
| 115 | */ |
||
| 116 | private $documentCollections = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Array of cached document bucket instances that are lazily loaded. |
||
| 120 | * |
||
| 121 | * @var Bucket[] |
||
| 122 | */ |
||
| 123 | private $documentBuckets = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Whether the DocumentManager is closed or not. |
||
| 127 | * |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $closed = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Collection of query filters. |
||
| 134 | * |
||
| 135 | * @var FilterCollection |
||
| 136 | */ |
||
| 137 | private $filterCollection; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Creates a new Document that operates on the given Mongo connection |
||
| 141 | * and uses the given Configuration. |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | 1596 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 145 | { |
||
| 146 | 1596 | $this->config = $config ?: new Configuration(); |
|
| 147 | 1596 | $this->eventManager = $eventManager ?: new EventManager(); |
|
| 148 | 1596 | $this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => ['root' => 'array', 'document' => 'array']]); |
|
| 149 | |||
| 150 | 1596 | $metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); |
|
| 151 | 1596 | $this->metadataFactory = new $metadataFactoryClassName(); |
|
| 152 | 1596 | $this->metadataFactory->setDocumentManager($this); |
|
| 153 | 1596 | $this->metadataFactory->setConfiguration($this->config); |
|
| 154 | |||
| 155 | 1596 | $cacheDriver = $this->config->getMetadataCacheImpl(); |
|
| 156 | 1596 | if ($cacheDriver) { |
|
| 157 | $this->metadataFactory->setCacheDriver($cacheDriver); |
||
| 158 | } |
||
| 159 | |||
| 160 | 1596 | $hydratorDir = $this->config->getHydratorDir(); |
|
| 161 | 1596 | $hydratorNs = $this->config->getHydratorNamespace(); |
|
| 162 | 1596 | $this->hydratorFactory = new HydratorFactory( |
|
| 163 | 1596 | $this, |
|
| 164 | 1596 | $this->eventManager, |
|
| 165 | 1596 | $hydratorDir, |
|
| 166 | 1596 | $hydratorNs, |
|
| 167 | 1596 | $this->config->getAutoGenerateHydratorClasses() |
|
| 168 | ); |
||
| 169 | |||
| 170 | 1596 | $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); |
|
| 171 | 1596 | $this->hydratorFactory->setUnitOfWork($this->unitOfWork); |
|
| 172 | 1596 | $this->schemaManager = new SchemaManager($this, $this->metadataFactory); |
|
| 173 | 1596 | $this->proxyFactory = new ProxyFactory( |
|
| 174 | 1596 | $this, |
|
| 175 | 1596 | $this->config->getProxyDir(), |
|
| 176 | 1596 | $this->config->getProxyNamespace(), |
|
| 177 | 1596 | $this->config->getAutoGenerateProxyClasses() |
|
| 178 | ); |
||
| 179 | 1596 | $this->repositoryFactory = $this->config->getRepositoryFactory(); |
|
| 180 | 1596 | } |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 184 | * |
||
| 185 | * @return ProxyFactory |
||
| 186 | */ |
||
| 187 | 1 | public function getProxyFactory() |
|
| 188 | { |
||
| 189 | 1 | return $this->proxyFactory; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a new Document that operates on the given Mongo connection |
||
| 194 | * and uses the given Configuration. |
||
| 195 | * |
||
| 196 | * @static |
||
| 197 | * @return DocumentManager |
||
| 198 | */ |
||
| 199 | 1596 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the EventManager used by the DocumentManager. |
||
| 206 | * |
||
| 207 | * @return EventManager |
||
| 208 | */ |
||
| 209 | 1642 | public function getEventManager() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 216 | * |
||
| 217 | * @return Client |
||
| 218 | */ |
||
| 219 | 1596 | public function getClient() |
|
| 220 | { |
||
| 221 | 1596 | return $this->client; |
|
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 226 | * |
||
| 227 | * @return ClassMetadataFactory |
||
| 228 | */ |
||
| 229 | 1596 | public function getMetadataFactory() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 236 | * |
||
| 237 | * This method is a no-op for other objects. |
||
| 238 | * |
||
| 239 | * @param object $obj |
||
| 240 | */ |
||
| 241 | public function initializeObject($obj) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 248 | * |
||
| 249 | * @return UnitOfWork |
||
| 250 | */ |
||
| 251 | 1603 | public function getUnitOfWork() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
| 258 | * for each type of document. |
||
| 259 | * |
||
| 260 | * @return HydratorFactory |
||
| 261 | */ |
||
| 262 | 68 | public function getHydratorFactory() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 269 | * |
||
| 270 | * @return SchemaManager |
||
| 271 | */ |
||
| 272 | 19 | public function getSchemaManager() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the metadata for a class. |
||
| 279 | * |
||
| 280 | * @param string $className The class name. |
||
| 281 | * @return ClassMetadata |
||
| 282 | * @internal Performance-sensitive method. |
||
| 283 | */ |
||
| 284 | 1335 | public function getClassMetadata($className) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Returns the MongoDB instance for a class. |
||
| 291 | * |
||
| 292 | * @param string $className The class name. |
||
| 293 | * @return Database |
||
| 294 | */ |
||
| 295 | 1264 | public function getDocumentDatabase($className) |
|
| 296 | { |
||
| 297 | 1264 | $className = ltrim($className, '\\'); |
|
| 298 | |||
| 299 | 1264 | if (isset($this->documentDatabases[$className])) { |
|
| 300 | 37 | return $this->documentDatabases[$className]; |
|
| 301 | } |
||
| 302 | |||
| 303 | 1259 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
| 304 | 1259 | $db = $metadata->getDatabase(); |
|
| 305 | 1259 | $db = $db ?: $this->config->getDefaultDB(); |
|
| 306 | 1259 | $db = $db ?: 'doctrine'; |
|
| 307 | 1259 | $this->documentDatabases[$className] = $this->client->selectDatabase($db); |
|
| 308 | |||
| 309 | 1259 | return $this->documentDatabases[$className]; |
|
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the array of instantiated document database instances. |
||
| 314 | * |
||
| 315 | * @return Database[] |
||
| 316 | */ |
||
| 317 | public function getDocumentDatabases() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the collection instance for a class. |
||
| 324 | * |
||
| 325 | * @param string $className The class name. |
||
| 326 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 327 | * @return Collection |
||
| 328 | */ |
||
| 329 | 1267 | public function getDocumentCollection($className) |
|
| 330 | { |
||
| 331 | 1267 | $className = ltrim($className, '\\'); |
|
| 332 | |||
| 333 | /** @var ClassMetadata $metadata */ |
||
| 334 | 1267 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
| 335 | 1267 | if ($metadata->isFile) { |
|
| 336 | 13 | return $this->getDocumentBucket($className)->getFilesCollection(); |
|
| 337 | } |
||
| 338 | |||
| 339 | 1259 | $collectionName = $metadata->getCollection(); |
|
| 340 | |||
| 341 | 1259 | if (! $collectionName) { |
|
| 342 | throw MongoDBException::documentNotMappedToCollection($className); |
||
| 343 | } |
||
| 344 | |||
| 345 | 1259 | if (! isset($this->documentCollections[$className])) { |
|
| 346 | 1249 | $db = $this->getDocumentDatabase($className); |
|
| 347 | |||
| 348 | 1249 | $options = []; |
|
| 349 | 1249 | if ($metadata->readPreference !== null) { |
|
| 350 | 3 | $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
|
| 351 | } |
||
| 352 | |||
| 353 | 1249 | $this->documentCollections[$className] = $db->selectCollection($collectionName, $options); |
|
| 354 | } |
||
| 355 | |||
| 356 | 1259 | return $this->documentCollections[$className]; |
|
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the bucket instance for a class. |
||
| 361 | * |
||
| 362 | * @param string $className The class name. |
||
| 363 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 364 | */ |
||
| 365 | 13 | public function getDocumentBucket(string $className): Bucket |
|
| 366 | { |
||
| 367 | 13 | $className = ltrim($className, '\\'); |
|
| 368 | |||
| 369 | /** @var ClassMetadata $metadata */ |
||
| 370 | 13 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
| 371 | 13 | if (! $metadata->isFile) { |
|
| 372 | throw MongoDBException::documentBucketOnlyAvailableForGridFSFiles($className); |
||
| 373 | } |
||
| 374 | |||
| 375 | 13 | $bucketName = $metadata->getBucketName(); |
|
| 376 | |||
| 377 | 13 | if (! $bucketName) { |
|
| 378 | throw MongoDBException::documentNotMappedToCollection($className); |
||
| 379 | } |
||
| 380 | |||
| 381 | 13 | if (! isset($this->documentBuckets[$className])) { |
|
| 382 | 8 | $db = $this->getDocumentDatabase($className); |
|
| 383 | |||
| 384 | 8 | $options = ['bucketName' => $bucketName]; |
|
| 385 | 8 | if ($metadata->readPreference !== null) { |
|
| 386 | $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
||
| 387 | } |
||
| 388 | |||
| 389 | 8 | $this->documentBuckets[$className] = $db->selectGridFSBucket($options); |
|
| 390 | } |
||
| 391 | |||
| 392 | 13 | return $this->documentBuckets[$className]; |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Gets the array of instantiated document collection instances. |
||
| 397 | * |
||
| 398 | * @return Collection[] |
||
| 399 | */ |
||
| 400 | public function getDocumentCollections() |
||
| 401 | { |
||
| 402 | return $this->documentCollections; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Create a new Query instance for a class. |
||
| 407 | * |
||
| 408 | * @param string $documentName The document class name. |
||
| 409 | * @return Query\Builder |
||
| 410 | */ |
||
| 411 | 180 | public function createQueryBuilder($documentName = null) |
|
| 412 | { |
||
| 413 | 180 | return new Query\Builder($this, $documentName); |
|
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Creates a new aggregation builder instance for a class. |
||
| 418 | * |
||
| 419 | * @param string $documentName The document class name. |
||
| 420 | * @return Aggregation\Builder |
||
| 421 | */ |
||
| 422 | 41 | public function createAggregationBuilder($documentName) |
|
| 423 | { |
||
| 424 | 41 | return new Aggregation\Builder($this, $documentName); |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 429 | * |
||
| 430 | * The document will be entered into the database at or before transaction |
||
| 431 | * commit or as a result of the flush operation. |
||
| 432 | * |
||
| 433 | * NOTE: The persist operation always considers documents that are not yet known to |
||
| 434 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
| 435 | * |
||
| 436 | * @param object $document The instance to make managed and persistent. |
||
| 437 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
| 438 | */ |
||
| 439 | 583 | public function persist($document) |
|
| 440 | { |
||
| 441 | 583 | if (! is_object($document)) { |
|
| 442 | 1 | throw new \InvalidArgumentException(gettype($document)); |
|
| 443 | } |
||
| 444 | 582 | $this->errorIfClosed(); |
|
| 445 | 581 | $this->unitOfWork->persist($document); |
|
| 446 | 577 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Removes a document instance. |
||
| 450 | * |
||
| 451 | * A removed document will be removed from the database at or before transaction commit |
||
| 452 | * or as a result of the flush operation. |
||
| 453 | * |
||
| 454 | * @param object $document The document instance to remove. |
||
| 455 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 456 | */ |
||
| 457 | 26 | public function remove($document) |
|
| 458 | { |
||
| 459 | 26 | if (! is_object($document)) { |
|
| 460 | 1 | throw new \InvalidArgumentException(gettype($document)); |
|
| 461 | } |
||
| 462 | 25 | $this->errorIfClosed(); |
|
| 463 | 24 | $this->unitOfWork->remove($document); |
|
| 464 | 24 | } |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Refreshes the persistent state of a document from the database, |
||
| 468 | * overriding any local changes that have not yet been persisted. |
||
| 469 | * |
||
| 470 | * @param object $document The document to refresh. |
||
| 471 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
| 472 | */ |
||
| 473 | 23 | public function refresh($document) |
|
| 474 | { |
||
| 475 | 23 | if (! is_object($document)) { |
|
| 476 | 1 | throw new \InvalidArgumentException(gettype($document)); |
|
| 477 | } |
||
| 478 | 22 | $this->errorIfClosed(); |
|
| 479 | 21 | $this->unitOfWork->refresh($document); |
|
| 480 | 20 | } |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Detaches a document from the DocumentManager, causing a managed document to |
||
| 484 | * become detached. Unflushed changes made to the document if any |
||
| 485 | * (including removal of the document), will not be synchronized to the database. |
||
| 486 | * Documents which previously referenced the detached document will continue to |
||
| 487 | * reference it. |
||
| 488 | * |
||
| 489 | * @param object $document The document to detach. |
||
| 490 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 491 | */ |
||
| 492 | 11 | public function detach($document) |
|
| 493 | { |
||
| 494 | 11 | if (! is_object($document)) { |
|
| 495 | 1 | throw new \InvalidArgumentException(gettype($document)); |
|
| 496 | } |
||
| 497 | 10 | $this->unitOfWork->detach($document); |
|
| 498 | 10 | } |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Merges the state of a detached document into the persistence context |
||
| 502 | * of this DocumentManager and returns the managed copy of the document. |
||
| 503 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
| 504 | * |
||
| 505 | * @param object $document The detached document to merge into the persistence context. |
||
| 506 | * @throws LockException |
||
| 507 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 508 | * @return object The managed copy of the document. |
||
| 509 | */ |
||
| 510 | 14 | public function merge($document) |
|
| 511 | { |
||
| 512 | 14 | if (! is_object($document)) { |
|
| 513 | 1 | throw new \InvalidArgumentException(gettype($document)); |
|
| 514 | } |
||
| 515 | 13 | $this->errorIfClosed(); |
|
| 516 | 12 | return $this->unitOfWork->merge($document); |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Acquire a lock on the given document. |
||
| 521 | * |
||
| 522 | * @param object $document |
||
| 523 | * @param int $lockMode |
||
| 524 | * @param int $lockVersion |
||
| 525 | * @throws \InvalidArgumentException |
||
| 526 | */ |
||
| 527 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 528 | { |
||
| 529 | 8 | if (! is_object($document)) { |
|
| 530 | throw new \InvalidArgumentException(gettype($document)); |
||
| 531 | } |
||
| 532 | 8 | $this->unitOfWork->lock($document, $lockMode, $lockVersion); |
|
| 533 | 5 | } |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Releases a lock on the given document. |
||
| 537 | * |
||
| 538 | * @param object $document |
||
| 539 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 540 | */ |
||
| 541 | 1 | public function unlock($document) |
|
| 542 | { |
||
| 543 | 1 | if (! is_object($document)) { |
|
| 544 | throw new \InvalidArgumentException(gettype($document)); |
||
| 545 | } |
||
| 546 | 1 | $this->unitOfWork->unlock($document); |
|
| 547 | 1 | } |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Gets the repository for a document class. |
||
| 551 | * |
||
| 552 | * @param string $documentName The name of the Document. |
||
| 553 | * @return ObjectRepository The repository. |
||
| 554 | */ |
||
| 555 | 334 | public function getRepository($documentName) |
|
| 556 | { |
||
| 557 | 334 | return $this->repositoryFactory->getRepository($this, $documentName); |
|
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 562 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 563 | * database. |
||
| 564 | * |
||
| 565 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 566 | * @throws \InvalidArgumentException |
||
| 567 | */ |
||
| 568 | 555 | public function flush(array $options = []) |
|
| 569 | { |
||
| 570 | 555 | $this->errorIfClosed(); |
|
| 571 | 554 | $this->unitOfWork->commit($options); |
|
| 572 | 551 | } |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Gets a reference to the document identified by the given type and identifier |
||
| 576 | * without actually loading it. |
||
| 577 | * |
||
| 578 | * If partial objects are allowed, this method will return a partial object that only |
||
| 579 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
| 580 | * loads itself on first access. |
||
| 581 | * |
||
| 582 | * @param string $documentName |
||
| 583 | * @param string|object $identifier |
||
| 584 | * @return mixed|object The document reference. |
||
| 585 | */ |
||
| 586 | 131 | public function getReference($documentName, $identifier) |
|
| 587 | { |
||
| 588 | /** @var ClassMetadata $class */ |
||
| 589 | 131 | $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\')); |
|
| 590 | 131 | $document = $this->unitOfWork->tryGetById($identifier, $class); |
|
| 591 | |||
| 592 | // Check identity map first, if its already in there just return it. |
||
| 593 | 131 | if ($document) { |
|
| 594 | 56 | return $document; |
|
| 595 | } |
||
| 596 | |||
| 597 | 102 | $document = $this->proxyFactory->getProxy($class->name, [$class->identifier => $identifier]); |
|
| 598 | 102 | $this->unitOfWork->registerManaged($document, $identifier, []); |
|
| 599 | |||
| 600 | 102 | return $document; |
|
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Gets a partial reference to the document identified by the given type and identifier |
||
| 605 | * without actually loading it, if the document is not yet loaded. |
||
| 606 | * |
||
| 607 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
| 608 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
| 609 | * Thus you can only ever safely access the identifier of a document obtained through |
||
| 610 | * this method. |
||
| 611 | * |
||
| 612 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 613 | * without loading one side of the association or to update a document without loading it. |
||
| 614 | * Note, however, that in the latter case the original (persistent) document data will |
||
| 615 | * never be visible to the application (especially not event listeners) as it will |
||
| 616 | * never be loaded in the first place. |
||
| 617 | * |
||
| 618 | * @param string $documentName The name of the document type. |
||
| 619 | * @param mixed $identifier The document identifier. |
||
| 620 | * @return object The (partial) document reference. |
||
| 621 | */ |
||
| 622 | 1 | public function getPartialReference($documentName, $identifier) |
|
| 623 | { |
||
| 624 | 1 | $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\')); |
|
| 625 | 1 | $document = $this->unitOfWork->tryGetById($identifier, $class); |
|
| 626 | |||
| 627 | // Check identity map first, if its already in there just return it. |
||
| 628 | 1 | if ($document) { |
|
| 629 | return $document; |
||
| 630 | } |
||
| 631 | 1 | $document = $class->newInstance(); |
|
| 632 | 1 | $class->setIdentifierValue($document, $identifier); |
|
| 633 | 1 | $this->unitOfWork->registerManaged($document, $identifier, []); |
|
| 634 | |||
| 635 | 1 | return $document; |
|
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Finds a Document by its identifier. |
||
| 640 | * |
||
| 641 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
| 642 | * |
||
| 643 | * @param string $documentName |
||
| 644 | * @param mixed $identifier |
||
| 645 | * @param int $lockMode |
||
| 646 | * @param int $lockVersion |
||
| 647 | * @return object $document |
||
| 648 | */ |
||
| 649 | 182 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
| 650 | { |
||
| 651 | 182 | return $this->getRepository($documentName)->find($identifier, $lockMode, $lockVersion); |
|
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Clears the DocumentManager. |
||
| 656 | * |
||
| 657 | * All documents that are currently managed by this DocumentManager become |
||
| 658 | * detached. |
||
| 659 | * |
||
| 660 | * @param string|null $documentName if given, only documents of this type will get detached |
||
| 661 | */ |
||
| 662 | 372 | public function clear($documentName = null) |
|
| 663 | { |
||
| 664 | 372 | $this->unitOfWork->clear($documentName); |
|
| 665 | 372 | } |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Closes the DocumentManager. All documents that are currently managed |
||
| 669 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
| 670 | * be used after it is closed. |
||
| 671 | */ |
||
| 672 | 6 | public function close() |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Determines whether a document instance is managed in this DocumentManager. |
||
| 680 | * |
||
| 681 | * @param object $document |
||
| 682 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 683 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
| 684 | */ |
||
| 685 | 3 | public function contains($document) |
|
| 686 | { |
||
| 687 | 3 | if (! is_object($document)) { |
|
| 688 | throw new \InvalidArgumentException(gettype($document)); |
||
| 689 | } |
||
| 690 | 3 | return $this->unitOfWork->isScheduledForInsert($document) || |
|
| 691 | 3 | $this->unitOfWork->isInIdentityMap($document) && |
|
| 692 | 3 | ! $this->unitOfWork->isScheduledForDelete($document); |
|
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Gets the Configuration used by the DocumentManager. |
||
| 697 | * |
||
| 698 | * @return Configuration |
||
| 699 | */ |
||
| 700 | 731 | public function getConfiguration() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Returns a reference to the supplied document. |
||
| 707 | * |
||
| 708 | * @param object $document A document object |
||
| 709 | * @param array $referenceMapping Mapping for the field that references the document |
||
| 710 | * |
||
| 711 | * @throws \InvalidArgumentException |
||
| 712 | * @throws MappingException |
||
| 713 | * @return mixed The reference for the document in question, according to the desired mapping |
||
| 714 | */ |
||
| 715 | 225 | public function createReference($document, array $referenceMapping) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
| 799 | * |
||
| 800 | * @throws MongoDBException If the DocumentManager is closed. |
||
| 801 | */ |
||
| 802 | 588 | private function errorIfClosed() |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Check if the Document manager is open or closed. |
||
| 811 | * |
||
| 812 | * @return bool |
||
| 813 | */ |
||
| 814 | 1 | public function isOpen() |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Gets the filter collection. |
||
| 821 | * |
||
| 822 | * @return FilterCollection The active filter collection. |
||
| 823 | */ |
||
| 824 | 513 | public function getFilterCollection() |
|
| 832 | } |
||
| 833 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.