Complex classes like DocumentPersister 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 DocumentPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | class DocumentPersister |
||
| 64 | { |
||
| 65 | /** @var PersistenceBuilder */ |
||
| 66 | private $pb; |
||
| 67 | |||
| 68 | /** @var DocumentManager */ |
||
| 69 | private $dm; |
||
| 70 | |||
| 71 | /** @var UnitOfWork */ |
||
| 72 | private $uow; |
||
| 73 | |||
| 74 | /** @var ClassMetadata */ |
||
| 75 | private $class; |
||
| 76 | |||
| 77 | /** @var Collection */ |
||
| 78 | private $collection; |
||
| 79 | |||
| 80 | /** @var Bucket|null */ |
||
| 81 | private $bucket; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Array of queued inserts for the persister to insert. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $queuedInserts = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Array of queued inserts for the persister to insert. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $queuedUpserts = []; |
||
| 96 | |||
| 97 | /** @var CriteriaMerger */ |
||
| 98 | private $cm; |
||
| 99 | |||
| 100 | /** @var CollectionPersister */ |
||
| 101 | private $cp; |
||
| 102 | |||
| 103 | /** @var HydratorFactory */ |
||
| 104 | private $hydratorFactory; |
||
| 105 | |||
| 106 | 1093 | public function __construct( |
|
| 107 | PersistenceBuilder $pb, |
||
| 108 | DocumentManager $dm, |
||
| 109 | UnitOfWork $uow, |
||
| 110 | HydratorFactory $hydratorFactory, |
||
| 111 | ClassMetadata $class, |
||
| 112 | ?CriteriaMerger $cm = null |
||
| 113 | ) { |
||
| 114 | 1093 | $this->pb = $pb; |
|
| 115 | 1093 | $this->dm = $dm; |
|
| 116 | 1093 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 117 | 1093 | $this->uow = $uow; |
|
| 118 | 1093 | $this->hydratorFactory = $hydratorFactory; |
|
| 119 | 1093 | $this->class = $class; |
|
| 120 | 1093 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 121 | 1093 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 122 | |||
| 123 | 1093 | if (! $class->isFile) { |
|
| 124 | 1085 | return; |
|
| 125 | } |
||
| 126 | |||
| 127 | 10 | $this->bucket = $dm->getDocumentBucket($class->name); |
|
| 128 | 10 | } |
|
| 129 | |||
| 130 | public function getInserts() : array |
||
| 131 | { |
||
| 132 | return $this->queuedInserts; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function isQueuedForInsert(object $document) : bool |
||
| 136 | { |
||
| 137 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Adds a document to the queued insertions. |
||
| 142 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 143 | */ |
||
| 144 | 485 | public function addInsert(object $document) : void |
|
| 145 | { |
||
| 146 | 485 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 147 | 485 | } |
|
| 148 | |||
| 149 | public function getUpserts() : array |
||
| 150 | { |
||
| 151 | return $this->queuedUpserts; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function isQueuedForUpsert(object $document) : bool |
||
| 155 | { |
||
| 156 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Adds a document to the queued upserts. |
||
| 161 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 162 | */ |
||
| 163 | 83 | public function addUpsert(object $document) : void |
|
| 164 | { |
||
| 165 | 83 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 166 | 83 | } |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 170 | */ |
||
| 171 | public function getClassMetadata() : ClassMetadata |
||
| 172 | { |
||
| 173 | return $this->class; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Executes all queued document insertions. |
||
| 178 | * |
||
| 179 | * Queued documents without an ID will inserted in a batch and queued |
||
| 180 | * documents with an ID will be upserted individually. |
||
| 181 | * |
||
| 182 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 183 | * |
||
| 184 | * @throws DriverException |
||
| 185 | */ |
||
| 186 | 485 | public function executeInserts(array $options = []) : void |
|
| 187 | { |
||
| 188 | 485 | if (! $this->queuedInserts) { |
|
|
|
|||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | 485 | $inserts = []; |
|
| 193 | 485 | $options = $this->getWriteOptions($options); |
|
| 194 | 485 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 195 | 485 | $data = $this->pb->prepareInsertData($document); |
|
| 196 | |||
| 197 | // Set the initial version for each insert |
||
| 198 | 484 | if ($this->class->isVersioned) { |
|
| 199 | 20 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 200 | 20 | $nextVersion = null; |
|
| 201 | 20 | if ($versionMapping['type'] === 'int') { |
|
| 202 | 18 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 203 | 18 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 204 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 205 | 2 | $nextVersionDateTime = new DateTime(); |
|
| 206 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 207 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 208 | } |
||
| 209 | 20 | $data[$versionMapping['name']] = $nextVersion; |
|
| 210 | } |
||
| 211 | |||
| 212 | 484 | $inserts[] = $data; |
|
| 213 | } |
||
| 214 | |||
| 215 | 484 | if ($inserts) { |
|
| 216 | try { |
||
| 217 | 484 | $this->collection->insertMany($inserts, $options); |
|
| 218 | 6 | } catch (DriverException $e) { |
|
| 219 | 6 | $this->queuedInserts = []; |
|
| 220 | 6 | throw $e; |
|
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /* All collections except for ones using addToSet have already been |
||
| 225 | * saved. We have left these to be handled separately to avoid checking |
||
| 226 | * collection for uniqueness on PHP side. |
||
| 227 | */ |
||
| 228 | 484 | foreach ($this->queuedInserts as $document) { |
|
| 229 | 484 | $this->handleCollections($document, $options); |
|
| 230 | } |
||
| 231 | |||
| 232 | 484 | $this->queuedInserts = []; |
|
| 233 | 484 | } |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Executes all queued document upserts. |
||
| 237 | * |
||
| 238 | * Queued documents with an ID are upserted individually. |
||
| 239 | * |
||
| 240 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 241 | */ |
||
| 242 | 83 | public function executeUpserts(array $options = []) : void |
|
| 243 | { |
||
| 244 | 83 | if (! $this->queuedUpserts) { |
|
| 245 | return; |
||
| 246 | } |
||
| 247 | |||
| 248 | 83 | $options = $this->getWriteOptions($options); |
|
| 249 | 83 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 250 | try { |
||
| 251 | 83 | $this->executeUpsert($document, $options); |
|
| 252 | 83 | $this->handleCollections($document, $options); |
|
| 253 | 83 | unset($this->queuedUpserts[$oid]); |
|
| 254 | } catch (WriteException $e) { |
||
| 255 | unset($this->queuedUpserts[$oid]); |
||
| 256 | 83 | throw $e; |
|
| 257 | } |
||
| 258 | } |
||
| 259 | 83 | } |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Executes a single upsert in {@link executeUpserts} |
||
| 263 | */ |
||
| 264 | 83 | private function executeUpsert(object $document, array $options) : void |
|
| 265 | { |
||
| 266 | 83 | $options['upsert'] = true; |
|
| 267 | 83 | $criteria = $this->getQueryForDocument($document); |
|
| 268 | |||
| 269 | 83 | $data = $this->pb->prepareUpsertData($document); |
|
| 270 | |||
| 271 | // Set the initial version for each upsert |
||
| 272 | 83 | if ($this->class->isVersioned) { |
|
| 273 | 2 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 274 | 2 | $nextVersion = null; |
|
| 275 | 2 | if ($versionMapping['type'] === 'int') { |
|
| 276 | 1 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 277 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 278 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 279 | 1 | $nextVersionDateTime = new DateTime(); |
|
| 280 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 281 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 282 | } |
||
| 283 | 2 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 284 | } |
||
| 285 | |||
| 286 | 83 | foreach (array_keys($criteria) as $field) { |
|
| 287 | 83 | unset($data['$set'][$field]); |
|
| 288 | 83 | unset($data['$inc'][$field]); |
|
| 289 | 83 | unset($data['$setOnInsert'][$field]); |
|
| 290 | } |
||
| 291 | |||
| 292 | // Do not send empty update operators |
||
| 293 | 83 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 294 | 83 | if (! empty($data[$operator])) { |
|
| 295 | 68 | continue; |
|
| 296 | } |
||
| 297 | |||
| 298 | 83 | unset($data[$operator]); |
|
| 299 | } |
||
| 300 | |||
| 301 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 302 | * an identifier as its only field. Since a document with the identifier |
||
| 303 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 304 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 305 | * the identifier to the same value in our criteria. |
||
| 306 | * |
||
| 307 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 308 | * empty $set modifier. The best we can do (without attempting to check |
||
| 309 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 310 | * after the relevant exception. |
||
| 311 | * |
||
| 312 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 313 | */ |
||
| 314 | 83 | if (empty($data)) { |
|
| 315 | 16 | $retry = true; |
|
| 316 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 317 | } |
||
| 318 | |||
| 319 | try { |
||
| 320 | 83 | $this->collection->updateOne($criteria, $data, $options); |
|
| 321 | 83 | return; |
|
| 322 | } catch (WriteException $e) { |
||
| 323 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 324 | throw $e; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->collection->updateOne($criteria, ['$set' => new stdClass()], $options); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Updates the already persisted document if it has any new changesets. |
||
| 333 | * |
||
| 334 | * @throws LockException |
||
| 335 | */ |
||
| 336 | 197 | public function update(object $document, array $options = []) : void |
|
| 337 | { |
||
| 338 | 197 | $update = $this->pb->prepareUpdateData($document); |
|
| 339 | |||
| 340 | 197 | $query = $this->getQueryForDocument($document); |
|
| 341 | |||
| 342 | 197 | foreach (array_keys($query) as $field) { |
|
| 343 | 197 | unset($update['$set'][$field]); |
|
| 344 | } |
||
| 345 | |||
| 346 | 197 | if (empty($update['$set'])) { |
|
| 347 | 90 | unset($update['$set']); |
|
| 348 | } |
||
| 349 | |||
| 350 | // Include versioning logic to set the new version value in the database |
||
| 351 | // and to ensure the version has not changed since this document object instance |
||
| 352 | // was fetched from the database |
||
| 353 | 197 | $nextVersion = null; |
|
| 354 | 197 | if ($this->class->isVersioned) { |
|
| 355 | 13 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 356 | 13 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 357 | 13 | if ($versionMapping['type'] === 'int') { |
|
| 358 | 10 | $nextVersion = $currentVersion + 1; |
|
| 359 | 10 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 360 | 10 | $query[$versionMapping['name']] = $currentVersion; |
|
| 361 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 362 | 3 | $nextVersion = new DateTime(); |
|
| 363 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 364 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | 197 | if (! empty($update)) { |
|
| 369 | // Include locking logic so that if the document object in memory is currently |
||
| 370 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 371 | 130 | if ($this->class->isLockable) { |
|
| 372 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 373 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 374 | 11 | if ($isLocked) { |
|
| 375 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 376 | } else { |
||
| 377 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | 130 | $options = $this->getWriteOptions($options); |
|
| 382 | |||
| 383 | 130 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 384 | |||
| 385 | 130 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 386 | 4 | throw LockException::lockFailed($document); |
|
| 387 | 126 | } elseif ($this->class->isVersioned) { |
|
| 388 | 9 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | 193 | $this->handleCollections($document, $options); |
|
| 393 | 193 | } |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Removes document from mongo |
||
| 397 | * |
||
| 398 | * @throws LockException |
||
| 399 | */ |
||
| 400 | 35 | public function delete(object $document, array $options = []) : void |
|
| 401 | { |
||
| 402 | 35 | if ($this->bucket instanceof Bucket) { |
|
| 403 | 1 | $documentIdentifier = $this->uow->getDocumentIdentifier($document); |
|
| 404 | 1 | $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier); |
|
| 405 | |||
| 406 | 1 | $this->bucket->delete($databaseIdentifier); |
|
| 407 | |||
| 408 | 1 | return; |
|
| 409 | } |
||
| 410 | |||
| 411 | 34 | $query = $this->getQueryForDocument($document); |
|
| 412 | |||
| 413 | 34 | if ($this->class->isLockable) { |
|
| 414 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 415 | } |
||
| 416 | |||
| 417 | 34 | $options = $this->getWriteOptions($options); |
|
| 418 | |||
| 419 | 34 | $result = $this->collection->deleteOne($query, $options); |
|
| 420 | |||
| 421 | 34 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 422 | 2 | throw LockException::lockFailed($document); |
|
| 423 | } |
||
| 424 | 32 | } |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Refreshes a managed document. |
||
| 428 | */ |
||
| 429 | 22 | public function refresh(object $document) : void |
|
| 430 | { |
||
| 431 | 22 | $query = $this->getQueryForDocument($document); |
|
| 432 | 22 | $data = $this->collection->findOne($query); |
|
| 433 | 22 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 434 | 22 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 435 | 22 | } |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Finds a document by a set of criteria. |
||
| 439 | * |
||
| 440 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 441 | * be used to match an _id value. |
||
| 442 | * |
||
| 443 | * @param mixed $criteria Query criteria |
||
| 444 | * |
||
| 445 | * @throws LockException |
||
| 446 | * |
||
| 447 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 448 | */ |
||
| 449 | 352 | public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null) : ?object |
|
| 450 | { |
||
| 451 | // TODO: remove this |
||
| 452 | 352 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 453 | $criteria = ['_id' => $criteria]; |
||
| 454 | } |
||
| 455 | |||
| 456 | 352 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 457 | 352 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 458 | 352 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 459 | |||
| 460 | 352 | $options = []; |
|
| 461 | 352 | if ($sort !== null) { |
|
| 462 | 92 | $options['sort'] = $this->prepareSort($sort); |
|
| 463 | } |
||
| 464 | 352 | $result = $this->collection->findOne($criteria, $options); |
|
| 465 | |||
| 466 | 352 | if ($this->class->isLockable) { |
|
| 467 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 468 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 469 | 1 | throw LockException::lockFailed($document); |
|
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | 351 | return $this->createDocument($result, $document, $hints); |
|
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Finds documents by a set of criteria. |
||
| 478 | */ |
||
| 479 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null) : Iterator |
|
| 480 | { |
||
| 481 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 482 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 483 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 484 | |||
| 485 | 22 | $options = []; |
|
| 486 | 22 | if ($sort !== null) { |
|
| 487 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 488 | } |
||
| 489 | |||
| 490 | 22 | if ($limit !== null) { |
|
| 491 | 10 | $options['limit'] = $limit; |
|
| 492 | } |
||
| 493 | |||
| 494 | 22 | if ($skip !== null) { |
|
| 495 | 1 | $options['skip'] = $skip; |
|
| 496 | } |
||
| 497 | |||
| 498 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 499 | 22 | return $this->wrapCursor($baseCursor); |
|
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @throws MongoDBException |
||
| 504 | */ |
||
| 505 | 273 | private function getShardKeyQuery(object $document) : array |
|
| 506 | { |
||
| 507 | 273 | if (! $this->class->isSharded()) { |
|
| 508 | 269 | return []; |
|
| 509 | } |
||
| 510 | |||
| 511 | 4 | $shardKey = $this->class->getShardKey(); |
|
| 512 | 4 | $keys = array_keys($shardKey['keys']); |
|
| 513 | 4 | $data = $this->uow->getDocumentActualData($document); |
|
| 514 | |||
| 515 | 4 | $shardKeyQueryPart = []; |
|
| 516 | 4 | foreach ($keys as $key) { |
|
| 517 | 4 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 518 | 4 | $this->guardMissingShardKey($document, $key, $data); |
|
| 519 | |||
| 520 | 4 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 521 | 1 | $reference = $this->prepareReference( |
|
| 522 | 1 | $key, |
|
| 523 | 1 | $data[$mapping['fieldName']], |
|
| 524 | 1 | $mapping, |
|
| 525 | 1 | false |
|
| 526 | ); |
||
| 527 | 1 | foreach ($reference as $keyValue) { |
|
| 528 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 529 | } |
||
| 530 | } else { |
||
| 531 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 532 | 4 | $shardKeyQueryPart[$key] = $value; |
|
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | 4 | return $shardKeyQueryPart; |
|
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 541 | */ |
||
| 542 | 22 | private function wrapCursor(Cursor $baseCursor) : Iterator |
|
| 543 | { |
||
| 544 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Checks whether the given managed document exists in the database. |
||
| 549 | */ |
||
| 550 | 3 | public function exists(object $document) : bool |
|
| 551 | { |
||
| 552 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 553 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 558 | */ |
||
| 559 | 5 | public function lock(object $document, int $lockMode) : void |
|
| 560 | { |
||
| 561 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 562 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 563 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 564 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 565 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 566 | 5 | } |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Releases any lock that exists on this document. |
||
| 570 | */ |
||
| 571 | 1 | public function unlock(object $document) : void |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Creates or fills a single document object from an query result. |
||
| 582 | * |
||
| 583 | * @param object $result The query result. |
||
| 584 | * @param object $document The document object to fill, if any. |
||
| 585 | * @param array $hints Hints for document creation. |
||
| 586 | * |
||
| 587 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 588 | */ |
||
| 589 | 351 | private function createDocument($result, ?object $document = null, array $hints = []) : ?object |
|
| 590 | { |
||
| 591 | 351 | if ($result === null) { |
|
| 592 | 113 | return null; |
|
| 593 | } |
||
| 594 | |||
| 595 | 307 | if ($document !== null) { |
|
| 596 | 28 | $hints[Query::HINT_REFRESH] = true; |
|
| 597 | 28 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 598 | 28 | $this->uow->registerManaged($document, $id, $result); |
|
| 599 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 606 | */ |
||
| 607 | 164 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 628 | |||
| 629 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) : void |
|
| 658 | |||
| 659 | 61 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) : void |
|
| 732 | |||
| 733 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) : void |
|
| 741 | |||
| 742 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) : Query |
|
| 781 | |||
| 782 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) : void |
|
| 795 | |||
| 796 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) : Iterator |
|
| 817 | |||
| 818 | /** |
||
| 819 | * Prepare a projection array by converting keys, which are PHP property |
||
| 820 | * names, to MongoDB field names. |
||
| 821 | */ |
||
| 822 | 14 | public function prepareProjection(array $fields) : array |
|
| 832 | |||
| 833 | /** |
||
| 834 | * @param int|string|null $sort |
||
| 835 | * |
||
| 836 | * @return int|string|null |
||
| 837 | */ |
||
| 838 | 25 | private function getSortDirection($sort) |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 853 | * names and changing direction strings to int. |
||
| 854 | */ |
||
| 855 | 142 | public function prepareSort(array $fields) : array |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 868 | */ |
||
| 869 | 436 | public function prepareFieldName(string $fieldName) : string |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Adds discriminator criteria to an already-prepared query. |
||
| 878 | * |
||
| 879 | * This method should be used once for query criteria and not be used for |
||
| 880 | * nested expressions. It should be called before |
||
| 881 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 882 | */ |
||
| 883 | 507 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) : array |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Adds filter criteria to an already-prepared query. |
||
| 902 | * |
||
| 903 | * This method should be used once for query criteria and not be used for |
||
| 904 | * nested expressions. It should be called after |
||
| 905 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 906 | */ |
||
| 907 | 508 | public function addFilterToPreparedQuery(array $preparedQuery) : array |
|
| 922 | |||
| 923 | /** |
||
| 924 | * Prepares the query criteria or new document object. |
||
| 925 | * |
||
| 926 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 927 | */ |
||
| 928 | 540 | public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : array |
|
| 956 | |||
| 957 | /** |
||
| 958 | * Prepares a query value and converts the PHP value to the database value |
||
| 959 | * if it is an identifier. |
||
| 960 | * |
||
| 961 | * It also handles converting $fieldName to the database name if they are different. |
||
| 962 | * |
||
| 963 | * @param mixed $value |
||
| 964 | */ |
||
| 965 | 894 | private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false) : array |
|
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Prepares a query expression. |
||
| 1163 | * |
||
| 1164 | * @param array|object $expression |
||
| 1165 | */ |
||
| 1166 | 79 | private function prepareQueryExpression($expression, ClassMetadata $class) : array |
|
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Checks whether the value has DBRef fields. |
||
| 1196 | * |
||
| 1197 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1198 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1199 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1200 | * $elemMatch criteria for matching a DBRef. |
||
| 1201 | * |
||
| 1202 | * @param mixed $value |
||
| 1203 | */ |
||
| 1204 | 80 | private function hasDBRefFields($value) : bool |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Checks whether the value has query operators. |
||
| 1225 | * |
||
| 1226 | * @param mixed $value |
||
| 1227 | */ |
||
| 1228 | 84 | private function hasQueryOperators($value) : bool |
|
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1249 | */ |
||
| 1250 | 27 | private function getClassDiscriminatorValues(ClassMetadata $metadata) : array |
|
| 1269 | |||
| 1270 | 555 | private function handleCollections(object $document, array $options) : void |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1296 | * Also, shard key field should be present in actual document data. |
||
| 1297 | * |
||
| 1298 | * @throws MongoDBException |
||
| 1299 | */ |
||
| 1300 | 4 | private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData) : void |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Get shard key aware query for single document. |
||
| 1319 | */ |
||
| 1320 | 269 | private function getQueryForDocument(object $document) : array |
|
| 1328 | |||
| 1329 | 556 | private function getWriteOptions(array $options = []) : array |
|
| 1339 | |||
| 1340 | 15 | private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj) : array |
|
| 1380 | } |
||
| 1381 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.