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 |
||
| 70 | final class DocumentPersister |
||
| 71 | { |
||
| 72 | /** @var PersistenceBuilder */ |
||
| 73 | private $pb; |
||
| 74 | |||
| 75 | /** @var DocumentManager */ |
||
| 76 | private $dm; |
||
| 77 | |||
| 78 | /** @var UnitOfWork */ |
||
| 79 | private $uow; |
||
| 80 | |||
| 81 | /** @var ClassMetadata */ |
||
| 82 | private $class; |
||
| 83 | |||
| 84 | /** @var Collection|null */ |
||
| 85 | private $collection; |
||
| 86 | |||
| 87 | /** @var Bucket|null */ |
||
| 88 | private $bucket; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Array of queued inserts for the persister to insert. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $queuedInserts = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Array of queued inserts for the persister to insert. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private $queuedUpserts = []; |
||
| 103 | |||
| 104 | /** @var CriteriaMerger */ |
||
| 105 | private $cm; |
||
| 106 | |||
| 107 | /** @var CollectionPersister */ |
||
| 108 | private $cp; |
||
| 109 | |||
| 110 | /** @var HydratorFactory */ |
||
| 111 | private $hydratorFactory; |
||
| 112 | |||
| 113 | 1204 | public function __construct( |
|
| 114 | PersistenceBuilder $pb, |
||
| 115 | DocumentManager $dm, |
||
| 116 | UnitOfWork $uow, |
||
| 117 | HydratorFactory $hydratorFactory, |
||
| 118 | ClassMetadata $class, |
||
| 119 | ?CriteriaMerger $cm = null |
||
| 120 | ) { |
||
| 121 | 1204 | $this->pb = $pb; |
|
| 122 | 1204 | $this->dm = $dm; |
|
| 123 | 1204 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 124 | 1204 | $this->uow = $uow; |
|
| 125 | 1204 | $this->hydratorFactory = $hydratorFactory; |
|
| 126 | 1204 | $this->class = $class; |
|
| 127 | 1204 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 128 | |||
| 129 | 1204 | if ($class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 130 | 95 | return; |
|
| 131 | } |
||
| 132 | |||
| 133 | 1201 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 134 | |||
| 135 | 1201 | if (! $class->isFile) { |
|
| 136 | 1189 | return; |
|
| 137 | } |
||
| 138 | |||
| 139 | 20 | $this->bucket = $dm->getDocumentBucket($class->name); |
|
| 140 | 20 | } |
|
| 141 | |||
| 142 | public function getInserts() : array |
||
| 146 | |||
| 147 | public function isQueuedForInsert(object $document) : bool |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds a document to the queued insertions. |
||
| 154 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 155 | */ |
||
| 156 | 533 | public function addInsert(object $document) : void |
|
| 160 | |||
| 161 | public function getUpserts() : array |
||
| 165 | |||
| 166 | public function isQueuedForUpsert(object $document) : bool |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds a document to the queued upserts. |
||
| 173 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 174 | */ |
||
| 175 | 85 | public function addUpsert(object $document) : void |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Gets the ClassMetadata instance of the document class this persister is |
||
| 182 | * used for. |
||
| 183 | */ |
||
| 184 | public function getClassMetadata() : ClassMetadata |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Executes all queued document insertions. |
||
| 191 | * |
||
| 192 | * Queued documents without an ID will inserted in a batch and queued |
||
| 193 | * documents with an ID will be upserted individually. |
||
| 194 | * |
||
| 195 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 196 | * |
||
| 197 | * @throws DriverException |
||
| 198 | */ |
||
| 199 | 533 | public function executeInserts(array $options = []) : void |
|
| 200 | { |
||
| 201 | 533 | if (! $this->queuedInserts) { |
|
|
|
|||
| 202 | return; |
||
| 203 | } |
||
| 204 | |||
| 205 | 533 | $inserts = []; |
|
| 206 | 533 | $options = $this->getWriteOptions($options); |
|
| 207 | 533 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 208 | 533 | $data = $this->pb->prepareInsertData($document); |
|
| 209 | |||
| 210 | // Set the initial version for each insert |
||
| 211 | 522 | if ($this->class->isVersioned) { |
|
| 212 | 40 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 213 | 40 | $nextVersion = null; |
|
| 214 | 40 | if ($versionMapping['type'] === 'int') { |
|
| 215 | 38 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 216 | 38 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 217 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 218 | 2 | $nextVersionDateTime = new DateTime(); |
|
| 219 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 220 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 221 | } |
||
| 222 | 40 | $data[$versionMapping['name']] = $nextVersion; |
|
| 223 | } |
||
| 224 | |||
| 225 | 522 | $inserts[] = $data; |
|
| 226 | } |
||
| 227 | |||
| 228 | 522 | if ($inserts) { |
|
| 229 | try { |
||
| 230 | 522 | assert($this->collection instanceof Collection); |
|
| 231 | 522 | $this->collection->insertMany($inserts, $options); |
|
| 232 | 6 | } catch (DriverException $e) { |
|
| 233 | 6 | $this->queuedInserts = []; |
|
| 234 | 6 | throw $e; |
|
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /* All collections except for ones using addToSet have already been |
||
| 239 | * saved. We have left these to be handled separately to avoid checking |
||
| 240 | * collection for uniqueness on PHP side. |
||
| 241 | */ |
||
| 242 | 522 | foreach ($this->queuedInserts as $document) { |
|
| 243 | 522 | $this->handleCollections($document, $options); |
|
| 244 | } |
||
| 245 | |||
| 246 | 522 | $this->queuedInserts = []; |
|
| 247 | 522 | } |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Executes all queued document upserts. |
||
| 251 | * |
||
| 252 | * Queued documents with an ID are upserted individually. |
||
| 253 | * |
||
| 254 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 255 | */ |
||
| 256 | 85 | public function executeUpserts(array $options = []) : void |
|
| 257 | { |
||
| 258 | 85 | if (! $this->queuedUpserts) { |
|
| 259 | return; |
||
| 260 | } |
||
| 261 | |||
| 262 | 85 | $options = $this->getWriteOptions($options); |
|
| 263 | 85 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 264 | try { |
||
| 265 | 85 | $this->executeUpsert($document, $options); |
|
| 266 | 85 | $this->handleCollections($document, $options); |
|
| 267 | 85 | unset($this->queuedUpserts[$oid]); |
|
| 268 | } catch (WriteException $e) { |
||
| 269 | unset($this->queuedUpserts[$oid]); |
||
| 270 | throw $e; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | 85 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Executes a single upsert in {@link executeUpserts} |
||
| 277 | */ |
||
| 278 | 85 | private function executeUpsert(object $document, array $options) : void |
|
| 279 | { |
||
| 280 | 85 | $options['upsert'] = true; |
|
| 281 | 85 | $criteria = $this->getQueryForDocument($document); |
|
| 282 | |||
| 283 | 85 | $data = $this->pb->prepareUpsertData($document); |
|
| 284 | |||
| 285 | // Set the initial version for each upsert |
||
| 286 | 85 | if ($this->class->isVersioned) { |
|
| 287 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 288 | 3 | $nextVersion = null; |
|
| 289 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 290 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 291 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 292 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 293 | 1 | $nextVersionDateTime = new DateTime(); |
|
| 294 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 295 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 296 | } |
||
| 297 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 298 | } |
||
| 299 | |||
| 300 | 85 | foreach (array_keys($criteria) as $field) { |
|
| 301 | 85 | unset($data['$set'][$field]); |
|
| 302 | 85 | unset($data['$inc'][$field]); |
|
| 303 | 85 | unset($data['$setOnInsert'][$field]); |
|
| 304 | } |
||
| 305 | |||
| 306 | // Do not send empty update operators |
||
| 307 | 85 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 308 | 85 | if (! empty($data[$operator])) { |
|
| 309 | 70 | continue; |
|
| 310 | } |
||
| 311 | |||
| 312 | 85 | unset($data[$operator]); |
|
| 313 | } |
||
| 314 | |||
| 315 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 316 | * an identifier as its only field. Since a document with the identifier |
||
| 317 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 318 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 319 | * the identifier to the same value in our criteria. |
||
| 320 | * |
||
| 321 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 322 | * empty $set modifier. The best we can do (without attempting to check |
||
| 323 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 324 | * after the relevant exception. |
||
| 325 | * |
||
| 326 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 327 | */ |
||
| 328 | 85 | if (empty($data)) { |
|
| 329 | 16 | $retry = true; |
|
| 330 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 331 | } |
||
| 332 | |||
| 333 | try { |
||
| 334 | 85 | assert($this->collection instanceof Collection); |
|
| 335 | 85 | $this->collection->updateOne($criteria, $data, $options); |
|
| 336 | |||
| 337 | 85 | return; |
|
| 338 | } catch (WriteException $e) { |
||
| 339 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 340 | throw $e; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | assert($this->collection instanceof Collection); |
||
| 345 | $this->collection->updateOne($criteria, ['$set' => new stdClass()], $options); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Updates the already persisted document if it has any new changesets. |
||
| 350 | * |
||
| 351 | * @throws LockException |
||
| 352 | */ |
||
| 353 | 234 | public function update(object $document, array $options = []) : void |
|
| 354 | { |
||
| 355 | 234 | $update = $this->pb->prepareUpdateData($document); |
|
| 356 | |||
| 357 | 234 | $query = $this->getQueryForDocument($document); |
|
| 358 | |||
| 359 | 232 | foreach (array_keys($query) as $field) { |
|
| 360 | 232 | unset($update['$set'][$field]); |
|
| 361 | } |
||
| 362 | |||
| 363 | 232 | if (empty($update['$set'])) { |
|
| 364 | 101 | unset($update['$set']); |
|
| 365 | } |
||
| 366 | |||
| 367 | // Include versioning logic to set the new version value in the database |
||
| 368 | // and to ensure the version has not changed since this document object instance |
||
| 369 | // was fetched from the database |
||
| 370 | 232 | $nextVersion = null; |
|
| 371 | 232 | if ($this->class->isVersioned) { |
|
| 372 | 33 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 373 | 33 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 374 | 33 | if ($versionMapping['type'] === 'int') { |
|
| 375 | 30 | $nextVersion = $currentVersion + 1; |
|
| 376 | 30 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 377 | 30 | $query[$versionMapping['name']] = $currentVersion; |
|
| 378 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 379 | 3 | $nextVersion = new DateTime(); |
|
| 380 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 381 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | 232 | if (! empty($update)) { |
|
| 386 | // Include locking logic so that if the document object in memory is currently |
||
| 387 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 388 | 158 | if ($this->class->isLockable) { |
|
| 389 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 390 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 391 | 11 | if ($isLocked) { |
|
| 392 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 393 | } else { |
||
| 394 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | 158 | $options = $this->getWriteOptions($options); |
|
| 399 | |||
| 400 | 158 | assert($this->collection instanceof Collection); |
|
| 401 | 158 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 402 | |||
| 403 | 158 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 404 | 6 | throw LockException::lockFailed($document); |
|
| 405 | } |
||
| 406 | |||
| 407 | 153 | if ($this->class->isVersioned) { |
|
| 408 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | 227 | $this->handleCollections($document, $options); |
|
| 413 | 227 | } |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Removes document from mongo |
||
| 417 | * |
||
| 418 | * @throws LockException |
||
| 419 | */ |
||
| 420 | 36 | public function delete(object $document, array $options = []) : void |
|
| 421 | { |
||
| 422 | 36 | if ($this->bucket instanceof Bucket) { |
|
| 423 | 1 | $documentIdentifier = $this->uow->getDocumentIdentifier($document); |
|
| 424 | 1 | $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier); |
|
| 425 | |||
| 426 | 1 | $this->bucket->delete($databaseIdentifier); |
|
| 427 | |||
| 428 | 1 | return; |
|
| 429 | } |
||
| 430 | |||
| 431 | 35 | $query = $this->getQueryForDocument($document); |
|
| 432 | |||
| 433 | 35 | if ($this->class->isLockable) { |
|
| 434 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 435 | } |
||
| 436 | |||
| 437 | 35 | $options = $this->getWriteOptions($options); |
|
| 438 | |||
| 439 | 35 | assert($this->collection instanceof Collection); |
|
| 440 | 35 | $result = $this->collection->deleteOne($query, $options); |
|
| 441 | |||
| 442 | 35 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 443 | 2 | throw LockException::lockFailed($document); |
|
| 444 | } |
||
| 445 | 33 | } |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Refreshes a managed document. |
||
| 449 | */ |
||
| 450 | 23 | public function refresh(object $document) : void |
|
| 451 | { |
||
| 452 | 23 | assert($this->collection instanceof Collection); |
|
| 453 | 23 | $query = $this->getQueryForDocument($document); |
|
| 454 | 23 | $data = $this->collection->findOne($query); |
|
| 455 | 23 | if ($data === null) { |
|
| 456 | throw MongoDBException::cannotRefreshDocument(); |
||
| 457 | } |
||
| 458 | 23 | $data = $this->hydratorFactory->hydrate($document, (array) $data); |
|
| 459 | 23 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 460 | 23 | } |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Finds a document by a set of criteria. |
||
| 464 | * |
||
| 465 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 466 | * be used to match an _id value. |
||
| 467 | * |
||
| 468 | * @param mixed $criteria Query criteria |
||
| 469 | * |
||
| 470 | * @throws LockException |
||
| 471 | * |
||
| 472 | * @todo Check identity map? loadById method? Try to guess whether |
||
| 473 | * $criteria is the id? |
||
| 474 | */ |
||
| 475 | 366 | public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null) : ?object |
|
| 476 | { |
||
| 477 | // TODO: remove this |
||
| 478 | 366 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 479 | $criteria = ['_id' => $criteria]; |
||
| 480 | } |
||
| 481 | |||
| 482 | 366 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 483 | 366 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 484 | 366 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 485 | |||
| 486 | 366 | $options = []; |
|
| 487 | 366 | if ($sort !== null) { |
|
| 488 | 95 | $options['sort'] = $this->prepareSort($sort); |
|
| 489 | } |
||
| 490 | 366 | assert($this->collection instanceof Collection); |
|
| 491 | 366 | $result = $this->collection->findOne($criteria, $options); |
|
| 492 | 366 | $result = $result !== null ? (array) $result : null; |
|
| 493 | |||
| 494 | 366 | if ($this->class->isLockable) { |
|
| 495 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 496 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 497 | 1 | throw LockException::lockFailed($document); |
|
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | 365 | if ($result === null) { |
|
| 502 | 115 | return null; |
|
| 503 | } |
||
| 504 | |||
| 505 | 321 | return $this->createDocument($result, $document, $hints); |
|
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Finds documents by a set of criteria. |
||
| 510 | */ |
||
| 511 | 24 | public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null) : Iterator |
|
| 512 | { |
||
| 513 | 24 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 514 | 24 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 515 | 24 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 516 | |||
| 517 | 24 | $options = []; |
|
| 518 | 24 | if ($sort !== null) { |
|
| 519 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 520 | } |
||
| 521 | |||
| 522 | 24 | if ($limit !== null) { |
|
| 523 | 10 | $options['limit'] = $limit; |
|
| 524 | } |
||
| 525 | |||
| 526 | 24 | if ($skip !== null) { |
|
| 527 | 1 | $options['skip'] = $skip; |
|
| 528 | } |
||
| 529 | |||
| 530 | 24 | assert($this->collection instanceof Collection); |
|
| 531 | 24 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 532 | |||
| 533 | 24 | return $this->wrapCursor($baseCursor); |
|
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @throws MongoDBException |
||
| 538 | */ |
||
| 539 | 314 | private function getShardKeyQuery(object $document) : array |
|
| 540 | { |
||
| 541 | 314 | if (! $this->class->isSharded()) { |
|
| 542 | 304 | return []; |
|
| 543 | } |
||
| 544 | |||
| 545 | 10 | $shardKey = $this->class->getShardKey(); |
|
| 546 | 10 | $keys = array_keys($shardKey['keys']); |
|
| 547 | 10 | $data = $this->uow->getDocumentActualData($document); |
|
| 548 | |||
| 549 | 10 | $shardKeyQueryPart = []; |
|
| 550 | 10 | foreach ($keys as $key) { |
|
| 551 | 10 | assert(is_string($key)); |
|
| 552 | 10 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 553 | 10 | $this->guardMissingShardKey($document, $key, $data); |
|
| 554 | |||
| 555 | 8 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 556 | 1 | $reference = $this->prepareReference( |
|
| 557 | 1 | $key, |
|
| 558 | 1 | $data[$mapping['fieldName']], |
|
| 559 | 1 | $mapping, |
|
| 560 | 1 | false |
|
| 561 | ); |
||
| 562 | 1 | foreach ($reference as $keyValue) { |
|
| 563 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 564 | } |
||
| 565 | } else { |
||
| 566 | 7 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 567 | 7 | $shardKeyQueryPart[$key] = $value; |
|
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | 8 | return $shardKeyQueryPart; |
|
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 576 | */ |
||
| 577 | 24 | private function wrapCursor(Cursor $baseCursor) : Iterator |
|
| 578 | { |
||
| 579 | 24 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Checks whether the given managed document exists in the database. |
||
| 584 | */ |
||
| 585 | 3 | public function exists(object $document) : bool |
|
| 586 | { |
||
| 587 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 588 | 3 | assert($this->collection instanceof Collection); |
|
| 589 | |||
| 590 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 595 | */ |
||
| 596 | 5 | public function lock(object $document, int $lockMode) : void |
|
| 597 | { |
||
| 598 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 599 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 600 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 601 | 5 | assert($this->collection instanceof Collection); |
|
| 602 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 603 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 604 | 5 | } |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Releases any lock that exists on this document. |
||
| 608 | */ |
||
| 609 | 1 | public function unlock(object $document) : void |
|
| 610 | { |
||
| 611 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 612 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 613 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 614 | 1 | assert($this->collection instanceof Collection); |
|
| 615 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 616 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 617 | 1 | } |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Creates or fills a single document object from an query result. |
||
| 621 | * |
||
| 622 | * @param array $result The query result. |
||
| 623 | * @param object $document The document object to fill, if any. |
||
| 624 | * @param array $hints Hints for document creation. |
||
| 625 | * |
||
| 626 | * @return object|null The filled and managed document object or NULL, if the query result is empty. |
||
| 627 | */ |
||
| 628 | 321 | private function createDocument(array $result, ?object $document = null, array $hints = []) : ?object |
|
| 629 | { |
||
| 630 | 321 | if ($document !== null) { |
|
| 631 | 26 | $hints[Query::HINT_REFRESH] = true; |
|
| 632 | 26 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 633 | 26 | $this->uow->registerManaged($document, $id, $result); |
|
| 634 | } |
||
| 635 | |||
| 636 | 321 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 641 | */ |
||
| 642 | 180 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 663 | |||
| 664 | 127 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) : void |
|
| 665 | { |
||
| 666 | 127 | $embeddedDocuments = $collection->getMongoData(); |
|
| 667 | 127 | $mapping = $collection->getMapping(); |
|
| 668 | 127 | $owner = $collection->getOwner(); |
|
| 669 | |||
| 670 | 127 | if (! $embeddedDocuments) { |
|
| 671 | 75 | return; |
|
| 672 | } |
||
| 673 | |||
| 674 | 98 | if ($owner === null) { |
|
| 675 | throw PersistentCollectionException::ownerRequiredToLoadCollection(); |
||
| 676 | } |
||
| 677 | |||
| 678 | 98 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 702 | |||
| 703 | 60 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) : void |
|
| 786 | |||
| 787 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) : void |
|
| 797 | |||
| 798 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) : Query |
|
| 842 | |||
| 843 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) : void |
|
| 856 | |||
| 857 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) : Iterator |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Prepare a projection array by converting keys, which are PHP property |
||
| 883 | * names, to MongoDB field names. |
||
| 884 | */ |
||
| 885 | 15 | public function prepareProjection(array $fields) : array |
|
| 895 | |||
| 896 | /** |
||
| 897 | * @param int|string $sort |
||
| 898 | * |
||
| 899 | * @return int|string|null |
||
| 900 | */ |
||
| 901 | 26 | private function getSortDirection($sort) |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 915 | * names and changing direction strings to int. |
||
| 916 | */ |
||
| 917 | 142 | public function prepareSort(array $fields) : array |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Prepare a mongodb field name and convert the PHP property names to |
||
| 934 | * MongoDB field names. |
||
| 935 | */ |
||
| 936 | 462 | public function prepareFieldName(string $fieldName) : string |
|
| 942 | |||
| 943 | /** |
||
| 944 | * Adds discriminator criteria to an already-prepared query. |
||
| 945 | * |
||
| 946 | * If the class we're querying has a discriminator field set, we add all |
||
| 947 | * possible discriminator values to the query. The list of possible |
||
| 948 | * discriminator values is based on the discriminatorValue of the class |
||
| 949 | * itself as well as those of all its subclasses. |
||
| 950 | * |
||
| 951 | * This method should be used once for query criteria and not be used for |
||
| 952 | * nested expressions. It should be called before |
||
| 953 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 954 | */ |
||
| 955 | 523 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) : array |
|
| 975 | |||
| 976 | /** |
||
| 977 | * Adds filter criteria to an already-prepared query. |
||
| 978 | * |
||
| 979 | * This method should be used once for query criteria and not be used for |
||
| 980 | * nested expressions. It should be called after |
||
| 981 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 982 | */ |
||
| 983 | 524 | public function addFilterToPreparedQuery(array $preparedQuery) : array |
|
| 998 | |||
| 999 | /** |
||
| 1000 | * Prepares the query criteria or new document object. |
||
| 1001 | * |
||
| 1002 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 1003 | */ |
||
| 1004 | 599 | public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : array |
|
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Prepares a query value and converts the PHP value to the database value |
||
| 1035 | * if it is an identifier. |
||
| 1036 | * |
||
| 1037 | * It also handles converting $fieldName to the database name if they are |
||
| 1038 | * different. |
||
| 1039 | * |
||
| 1040 | * @param mixed $value |
||
| 1041 | */ |
||
| 1042 | 977 | private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false) : array |
|
| 1235 | |||
| 1236 | 77 | private function prepareQueryExpression(array $expression, ClassMetadata $class) : array |
|
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Checks whether the value has DBRef fields. |
||
| 1266 | * |
||
| 1267 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1268 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1269 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1270 | * $elemMatch criteria for matching a DBRef. |
||
| 1271 | * |
||
| 1272 | * @param mixed $value |
||
| 1273 | */ |
||
| 1274 | 78 | private function hasDBRefFields($value) : bool |
|
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Checks whether the value has query operators. |
||
| 1295 | * |
||
| 1296 | * @param mixed $value |
||
| 1297 | */ |
||
| 1298 | 82 | private function hasQueryOperators($value) : bool |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Returns the list of discriminator values for the given ClassMetadata |
||
| 1319 | */ |
||
| 1320 | 32 | private function getClassDiscriminatorValues(ClassMetadata $metadata) : array |
|
| 1344 | |||
| 1345 | 595 | private function handleCollections(object $document, array $options) : void |
|
| 1376 | |||
| 1377 | /** |
||
| 1378 | * If the document is new, ignore shard key field value, otherwise throw an |
||
| 1379 | * exception. Also, shard key field should be present in actual document |
||
| 1380 | * data. |
||
| 1381 | * |
||
| 1382 | * @throws MongoDBException |
||
| 1383 | */ |
||
| 1384 | 10 | private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData) : void |
|
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Get shard key aware query for single document. |
||
| 1403 | */ |
||
| 1404 | 310 | private function getQueryForDocument(object $document) : array |
|
| 1413 | |||
| 1414 | 606 | private function getWriteOptions(array $options = []) : array |
|
| 1424 | |||
| 1425 | 15 | private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj) : array |
|
| 1465 | } |
||
| 1466 |
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.