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 |
||
| 68 | class DocumentPersister |
||
| 69 | { |
||
| 70 | /** @var PersistenceBuilder */ |
||
| 71 | private $pb; |
||
| 72 | |||
| 73 | /** @var DocumentManager */ |
||
| 74 | private $dm; |
||
| 75 | |||
| 76 | /** @var UnitOfWork */ |
||
| 77 | private $uow; |
||
| 78 | |||
| 79 | /** @var ClassMetadata */ |
||
| 80 | private $class; |
||
| 81 | |||
| 82 | /** @var Collection */ |
||
| 83 | private $collection; |
||
| 84 | |||
| 85 | /** @var Bucket|null */ |
||
| 86 | private $bucket; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Array of queued inserts for the persister to insert. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $queuedInserts = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Array of queued inserts for the persister to insert. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $queuedUpserts = []; |
||
| 101 | |||
| 102 | /** @var CriteriaMerger */ |
||
| 103 | private $cm; |
||
| 104 | |||
| 105 | /** @var CollectionPersister */ |
||
| 106 | private $cp; |
||
| 107 | |||
| 108 | /** @var HydratorFactory */ |
||
| 109 | private $hydratorFactory; |
||
| 110 | |||
| 111 | 1132 | public function __construct( |
|
| 112 | PersistenceBuilder $pb, |
||
| 113 | DocumentManager $dm, |
||
| 114 | UnitOfWork $uow, |
||
| 115 | HydratorFactory $hydratorFactory, |
||
| 116 | ClassMetadata $class, |
||
| 117 | ?CriteriaMerger $cm = null |
||
| 118 | ) { |
||
| 119 | 1132 | $this->pb = $pb; |
|
| 120 | 1132 | $this->dm = $dm; |
|
| 121 | 1132 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 122 | 1132 | $this->uow = $uow; |
|
| 123 | 1132 | $this->hydratorFactory = $hydratorFactory; |
|
| 124 | 1132 | $this->class = $class; |
|
| 125 | 1132 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 126 | 1132 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 127 | |||
| 128 | 1132 | if (! $class->isFile) { |
|
| 129 | 1124 | return; |
|
| 130 | } |
||
| 131 | |||
| 132 | 10 | $this->bucket = $dm->getDocumentBucket($class->name); |
|
| 133 | 10 | } |
|
| 134 | |||
| 135 | public function getInserts() : array |
||
| 139 | |||
| 140 | public function isQueuedForInsert(object $document) : bool |
||
| 141 | { |
||
| 142 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Adds a document to the queued insertions. |
||
| 147 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 148 | */ |
||
| 149 | 519 | public function addInsert(object $document) : void |
|
| 150 | { |
||
| 151 | 519 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 152 | 519 | } |
|
| 153 | |||
| 154 | public function getUpserts() : array |
||
| 158 | |||
| 159 | public function isQueuedForUpsert(object $document) : bool |
||
| 160 | { |
||
| 161 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Adds a document to the queued upserts. |
||
| 166 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 167 | */ |
||
| 168 | 85 | public function addUpsert(object $document) : void |
|
| 169 | { |
||
| 170 | 85 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 171 | 85 | } |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Gets the ClassMetadata instance of the document class this persister is |
||
| 175 | * used for. |
||
| 176 | */ |
||
| 177 | public function getClassMetadata() : ClassMetadata |
||
| 178 | { |
||
| 179 | return $this->class; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Executes all queued document insertions. |
||
| 184 | * |
||
| 185 | * Queued documents without an ID will inserted in a batch and queued |
||
| 186 | * documents with an ID will be upserted individually. |
||
| 187 | * |
||
| 188 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 189 | * |
||
| 190 | * @throws DriverException |
||
| 191 | */ |
||
| 192 | 519 | public function executeInserts(array $options = []) : void |
|
| 193 | { |
||
| 194 | 519 | if (! $this->queuedInserts) { |
|
|
|
|||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | 519 | $inserts = []; |
|
| 199 | 519 | $options = $this->getWriteOptions($options); |
|
| 200 | 519 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 201 | 519 | $data = $this->pb->prepareInsertData($document); |
|
| 202 | |||
| 203 | // Set the initial version for each insert |
||
| 204 | 508 | if ($this->class->isVersioned) { |
|
| 205 | 40 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 206 | 40 | $nextVersion = null; |
|
| 207 | 40 | if ($versionMapping['type'] === 'int') { |
|
| 208 | 38 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 209 | 38 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 210 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 211 | 2 | $nextVersionDateTime = new DateTime(); |
|
| 212 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 213 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 214 | } |
||
| 215 | 40 | $data[$versionMapping['name']] = $nextVersion; |
|
| 216 | } |
||
| 217 | |||
| 218 | 508 | $inserts[] = $data; |
|
| 219 | } |
||
| 220 | |||
| 221 | 508 | if ($inserts) { |
|
| 222 | try { |
||
| 223 | 508 | $this->collection->insertMany($inserts, $options); |
|
| 224 | 6 | } catch (DriverException $e) { |
|
| 225 | 6 | $this->queuedInserts = []; |
|
| 226 | 6 | throw $e; |
|
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /* All collections except for ones using addToSet have already been |
||
| 231 | * saved. We have left these to be handled separately to avoid checking |
||
| 232 | * collection for uniqueness on PHP side. |
||
| 233 | */ |
||
| 234 | 508 | foreach ($this->queuedInserts as $document) { |
|
| 235 | 508 | $this->handleCollections($document, $options); |
|
| 236 | } |
||
| 237 | |||
| 238 | 508 | $this->queuedInserts = []; |
|
| 239 | 508 | } |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Executes all queued document upserts. |
||
| 243 | * |
||
| 244 | * Queued documents with an ID are upserted individually. |
||
| 245 | * |
||
| 246 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 247 | */ |
||
| 248 | 85 | public function executeUpserts(array $options = []) : void |
|
| 249 | { |
||
| 250 | 85 | if (! $this->queuedUpserts) { |
|
| 251 | return; |
||
| 252 | } |
||
| 253 | |||
| 254 | 85 | $options = $this->getWriteOptions($options); |
|
| 255 | 85 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 256 | try { |
||
| 257 | 85 | $this->executeUpsert($document, $options); |
|
| 258 | 85 | $this->handleCollections($document, $options); |
|
| 259 | 85 | unset($this->queuedUpserts[$oid]); |
|
| 260 | } catch (WriteException $e) { |
||
| 261 | unset($this->queuedUpserts[$oid]); |
||
| 262 | throw $e; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | 85 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Executes a single upsert in {@link executeUpserts} |
||
| 269 | */ |
||
| 270 | 85 | private function executeUpsert(object $document, array $options) : void |
|
| 271 | { |
||
| 272 | 85 | $options['upsert'] = true; |
|
| 273 | 85 | $criteria = $this->getQueryForDocument($document); |
|
| 274 | |||
| 275 | 85 | $data = $this->pb->prepareUpsertData($document); |
|
| 276 | |||
| 277 | // Set the initial version for each upsert |
||
| 278 | 85 | if ($this->class->isVersioned) { |
|
| 279 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 280 | 3 | $nextVersion = null; |
|
| 281 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 282 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 283 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 284 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 285 | 1 | $nextVersionDateTime = new DateTime(); |
|
| 286 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 287 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 288 | } |
||
| 289 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 290 | } |
||
| 291 | |||
| 292 | 85 | foreach (array_keys($criteria) as $field) { |
|
| 293 | 85 | unset($data['$set'][$field]); |
|
| 294 | 85 | unset($data['$inc'][$field]); |
|
| 295 | 85 | unset($data['$setOnInsert'][$field]); |
|
| 296 | } |
||
| 297 | |||
| 298 | // Do not send empty update operators |
||
| 299 | 85 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 300 | 85 | if (! empty($data[$operator])) { |
|
| 301 | 70 | continue; |
|
| 302 | } |
||
| 303 | |||
| 304 | 85 | unset($data[$operator]); |
|
| 305 | } |
||
| 306 | |||
| 307 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 308 | * an identifier as its only field. Since a document with the identifier |
||
| 309 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 310 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 311 | * the identifier to the same value in our criteria. |
||
| 312 | * |
||
| 313 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 314 | * empty $set modifier. The best we can do (without attempting to check |
||
| 315 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 316 | * after the relevant exception. |
||
| 317 | * |
||
| 318 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 319 | */ |
||
| 320 | 85 | if (empty($data)) { |
|
| 321 | 16 | $retry = true; |
|
| 322 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 323 | } |
||
| 324 | |||
| 325 | try { |
||
| 326 | 85 | $this->collection->updateOne($criteria, $data, $options); |
|
| 327 | 85 | return; |
|
| 328 | } catch (WriteException $e) { |
||
| 329 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 330 | throw $e; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | $this->collection->updateOne($criteria, ['$set' => new stdClass()], $options); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Updates the already persisted document if it has any new changesets. |
||
| 339 | * |
||
| 340 | * @throws LockException |
||
| 341 | */ |
||
| 342 | 227 | public function update(object $document, array $options = []) : void |
|
| 343 | { |
||
| 344 | 227 | $update = $this->pb->prepareUpdateData($document); |
|
| 345 | |||
| 346 | 227 | $query = $this->getQueryForDocument($document); |
|
| 347 | |||
| 348 | 225 | foreach (array_keys($query) as $field) { |
|
| 349 | 225 | unset($update['$set'][$field]); |
|
| 350 | } |
||
| 351 | |||
| 352 | 225 | if (empty($update['$set'])) { |
|
| 353 | 100 | unset($update['$set']); |
|
| 354 | } |
||
| 355 | |||
| 356 | // Include versioning logic to set the new version value in the database |
||
| 357 | // and to ensure the version has not changed since this document object instance |
||
| 358 | // was fetched from the database |
||
| 359 | 225 | $nextVersion = null; |
|
| 360 | 225 | if ($this->class->isVersioned) { |
|
| 361 | 33 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 362 | 33 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 363 | 33 | if ($versionMapping['type'] === 'int') { |
|
| 364 | 30 | $nextVersion = $currentVersion + 1; |
|
| 365 | 30 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 366 | 30 | $query[$versionMapping['name']] = $currentVersion; |
|
| 367 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 368 | 3 | $nextVersion = new DateTime(); |
|
| 369 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 370 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | 225 | if (! empty($update)) { |
|
| 375 | // Include locking logic so that if the document object in memory is currently |
||
| 376 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 377 | 151 | if ($this->class->isLockable) { |
|
| 378 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 379 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 380 | 11 | if ($isLocked) { |
|
| 381 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 382 | } else { |
||
| 383 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | 151 | $options = $this->getWriteOptions($options); |
|
| 388 | |||
| 389 | 151 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 390 | |||
| 391 | 151 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 392 | 6 | throw LockException::lockFailed($document); |
|
| 393 | 146 | } elseif ($this->class->isVersioned) { |
|
| 394 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | 220 | $this->handleCollections($document, $options); |
|
| 399 | 220 | } |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Removes document from mongo |
||
| 403 | * |
||
| 404 | * @throws LockException |
||
| 405 | */ |
||
| 406 | 36 | public function delete(object $document, array $options = []) : void |
|
| 407 | { |
||
| 408 | 36 | if ($this->bucket instanceof Bucket) { |
|
| 409 | 1 | $documentIdentifier = $this->uow->getDocumentIdentifier($document); |
|
| 410 | 1 | $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier); |
|
| 411 | |||
| 412 | 1 | $this->bucket->delete($databaseIdentifier); |
|
| 413 | |||
| 414 | 1 | return; |
|
| 415 | } |
||
| 416 | |||
| 417 | 35 | $query = $this->getQueryForDocument($document); |
|
| 418 | |||
| 419 | 35 | if ($this->class->isLockable) { |
|
| 420 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 421 | } |
||
| 422 | |||
| 423 | 35 | $options = $this->getWriteOptions($options); |
|
| 424 | |||
| 425 | 35 | $result = $this->collection->deleteOne($query, $options); |
|
| 426 | |||
| 427 | 35 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 428 | 2 | throw LockException::lockFailed($document); |
|
| 429 | } |
||
| 430 | 33 | } |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Refreshes a managed document. |
||
| 434 | */ |
||
| 435 | 23 | public function refresh(object $document) : void |
|
| 436 | { |
||
| 437 | 23 | $query = $this->getQueryForDocument($document); |
|
| 438 | 23 | $data = $this->collection->findOne($query); |
|
| 439 | 23 | if ($data === null) { |
|
| 440 | throw MongoDBException::cannotRefreshDocument(); |
||
| 441 | } |
||
| 442 | 23 | $data = $this->hydratorFactory->hydrate($document, (array) $data); |
|
| 443 | 23 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 444 | 23 | } |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Finds a document by a set of criteria. |
||
| 448 | * |
||
| 449 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 450 | * be used to match an _id value. |
||
| 451 | * |
||
| 452 | * @param mixed $criteria Query criteria |
||
| 453 | * |
||
| 454 | * @throws LockException |
||
| 455 | * |
||
| 456 | * @todo Check identity map? loadById method? Try to guess whether |
||
| 457 | * $criteria is the id? |
||
| 458 | */ |
||
| 459 | 365 | public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null) : ?object |
|
| 460 | { |
||
| 461 | // TODO: remove this |
||
| 462 | 365 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 463 | $criteria = ['_id' => $criteria]; |
||
| 464 | } |
||
| 465 | |||
| 466 | 365 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 467 | 365 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 468 | 365 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 469 | |||
| 470 | 365 | $options = []; |
|
| 471 | 365 | if ($sort !== null) { |
|
| 472 | 95 | $options['sort'] = $this->prepareSort($sort); |
|
| 473 | } |
||
| 474 | 365 | $result = $this->collection->findOne($criteria, $options); |
|
| 475 | 365 | $result = $result !== null ? (array) $result : null; |
|
| 476 | |||
| 477 | 365 | if ($this->class->isLockable) { |
|
| 478 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 479 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 480 | 1 | throw LockException::lockFailed($document); |
|
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | 364 | if ($result === null) { |
|
| 485 | 115 | return null; |
|
| 486 | } |
||
| 487 | |||
| 488 | 320 | return $this->createDocument($result, $document, $hints); |
|
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Finds documents by a set of criteria. |
||
| 493 | */ |
||
| 494 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null) : Iterator |
|
| 495 | { |
||
| 496 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 497 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 498 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 499 | |||
| 500 | 22 | $options = []; |
|
| 501 | 22 | if ($sort !== null) { |
|
| 502 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 503 | } |
||
| 504 | |||
| 505 | 22 | if ($limit !== null) { |
|
| 506 | 10 | $options['limit'] = $limit; |
|
| 507 | } |
||
| 508 | |||
| 509 | 22 | if ($skip !== null) { |
|
| 510 | 1 | $options['skip'] = $skip; |
|
| 511 | } |
||
| 512 | |||
| 513 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 514 | 22 | return $this->wrapCursor($baseCursor); |
|
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @throws MongoDBException |
||
| 519 | */ |
||
| 520 | 307 | private function getShardKeyQuery(object $document) : array |
|
| 521 | { |
||
| 522 | 307 | if (! $this->class->isSharded()) { |
|
| 523 | 297 | return []; |
|
| 524 | } |
||
| 525 | |||
| 526 | 10 | $shardKey = $this->class->getShardKey(); |
|
| 527 | 10 | $keys = array_keys($shardKey['keys']); |
|
| 528 | 10 | $data = $this->uow->getDocumentActualData($document); |
|
| 529 | |||
| 530 | 10 | $shardKeyQueryPart = []; |
|
| 531 | 10 | foreach ($keys as $key) { |
|
| 532 | 10 | assert(is_string($key)); |
|
| 533 | 10 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 534 | 10 | $this->guardMissingShardKey($document, $key, $data); |
|
| 535 | |||
| 536 | 8 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 537 | 1 | $reference = $this->prepareReference( |
|
| 538 | 1 | $key, |
|
| 539 | 1 | $data[$mapping['fieldName']], |
|
| 540 | 1 | $mapping, |
|
| 541 | 1 | false |
|
| 542 | ); |
||
| 543 | 1 | foreach ($reference as $keyValue) { |
|
| 544 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 545 | } |
||
| 546 | } else { |
||
| 547 | 7 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 548 | 7 | $shardKeyQueryPart[$key] = $value; |
|
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | 8 | return $shardKeyQueryPart; |
|
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 557 | */ |
||
| 558 | 22 | private function wrapCursor(Cursor $baseCursor) : Iterator |
|
| 559 | { |
||
| 560 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Checks whether the given managed document exists in the database. |
||
| 565 | */ |
||
| 566 | 3 | public function exists(object $document) : bool |
|
| 567 | { |
||
| 568 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 569 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 574 | */ |
||
| 575 | 5 | public function lock(object $document, int $lockMode) : void |
|
| 576 | { |
||
| 577 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 578 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 579 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 580 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 581 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 582 | 5 | } |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Releases any lock that exists on this document. |
||
| 586 | */ |
||
| 587 | 1 | public function unlock(object $document) : void |
|
| 588 | { |
||
| 589 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 590 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 591 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 592 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 593 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 594 | 1 | } |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Creates or fills a single document object from an query result. |
||
| 598 | * |
||
| 599 | * @param array $result The query result. |
||
| 600 | * @param object $document The document object to fill, if any. |
||
| 601 | * @param array $hints Hints for document creation. |
||
| 602 | * |
||
| 603 | * @return object|null The filled and managed document object or NULL, if the query result is empty. |
||
| 604 | */ |
||
| 605 | 320 | private function createDocument(array $result, ?object $document = null, array $hints = []) : ?object |
|
| 606 | { |
||
| 607 | 320 | if ($document !== null) { |
|
| 608 | 26 | $hints[Query::HINT_REFRESH] = true; |
|
| 609 | 26 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 610 | 26 | $this->uow->registerManaged($document, $id, $result); |
|
| 611 | } |
||
| 612 | |||
| 613 | 320 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 618 | */ |
||
| 619 | 178 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 640 | |||
| 641 | 126 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) : void |
|
| 670 | |||
| 671 | 59 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) : void |
|
| 744 | |||
| 745 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) : void |
|
| 755 | |||
| 756 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) : Query |
|
| 800 | |||
| 801 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) : void |
|
| 814 | |||
| 815 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) : Iterator |
|
| 816 | { |
||
| 817 | 5 | $mapping = $collection->getMapping(); |
|
| 818 | 5 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 819 | 5 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 820 | 5 | ->$repositoryMethod($collection->getOwner()); |
|
| 821 | |||
| 822 | 5 | if (! $cursor instanceof Iterator) { |
|
| 823 | throw new BadMethodCallException(sprintf('Expected repository method %s to return an iterable object', $repositoryMethod)); |
||
| 824 | } |
||
| 825 | |||
| 826 | 5 | if (! empty($mapping['prime'])) { |
|
| 827 | 1 | $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); |
|
| 828 | 1 | $primers = array_combine($mapping['prime'], array_fill(0, count($mapping['prime']), true)); |
|
| 829 | 1 | $class = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 830 | |||
| 831 | 1 | assert(is_array($primers)); |
|
| 832 | |||
| 833 | 1 | $cursor = new PrimingIterator($cursor, $class, $referencePrimer, $primers, $collection->getHints()); |
|
| 834 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Prepare a projection array by converting keys, which are PHP property |
||
| 841 | * names, to MongoDB field names. |
||
| 842 | */ |
||
| 843 | 15 | public function prepareProjection(array $fields) : array |
|
| 853 | |||
| 854 | /** |
||
| 855 | * @param int|string $sort |
||
| 856 | * |
||
| 857 | * @return int|string|null |
||
| 858 | */ |
||
| 859 | 26 | private function getSortDirection($sort) |
|
| 871 | |||
| 872 | /** |
||
| 873 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 874 | * names and changing direction strings to int. |
||
| 875 | */ |
||
| 876 | 142 | public function prepareSort(array $fields) : array |
|
| 890 | |||
| 891 | /** |
||
| 892 | * Prepare a mongodb field name and convert the PHP property names to |
||
| 893 | * MongoDB field names. |
||
| 894 | */ |
||
| 895 | 439 | public function prepareFieldName(string $fieldName) : string |
|
| 901 | |||
| 902 | /** |
||
| 903 | * Adds discriminator criteria to an already-prepared query. |
||
| 904 | * |
||
| 905 | * This method should be used once for query criteria and not be used for |
||
| 906 | * nested expressions. It should be called before |
||
| 907 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 908 | */ |
||
| 909 | 520 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) : array |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Adds filter criteria to an already-prepared query. |
||
| 928 | * |
||
| 929 | * This method should be used once for query criteria and not be used for |
||
| 930 | * nested expressions. It should be called after |
||
| 931 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 932 | */ |
||
| 933 | 521 | public function addFilterToPreparedQuery(array $preparedQuery) : array |
|
| 948 | |||
| 949 | /** |
||
| 950 | * Prepares the query criteria or new document object. |
||
| 951 | * |
||
| 952 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 953 | */ |
||
| 954 | 553 | public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : array |
|
| 982 | |||
| 983 | /** |
||
| 984 | * Prepares a query value and converts the PHP value to the database value |
||
| 985 | * if it is an identifier. |
||
| 986 | * |
||
| 987 | * It also handles converting $fieldName to the database name if they are |
||
| 988 | * different. |
||
| 989 | * |
||
| 990 | * @param mixed $value |
||
| 991 | */ |
||
| 992 | 910 | private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false) : array |
|
| 1185 | |||
| 1186 | 77 | private function prepareQueryExpression(array $expression, ClassMetadata $class) : array |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Checks whether the value has DBRef fields. |
||
| 1216 | * |
||
| 1217 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1218 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1219 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1220 | * $elemMatch criteria for matching a DBRef. |
||
| 1221 | * |
||
| 1222 | * @param mixed $value |
||
| 1223 | */ |
||
| 1224 | 78 | private function hasDBRefFields($value) : bool |
|
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Checks whether the value has query operators. |
||
| 1245 | * |
||
| 1246 | * @param mixed $value |
||
| 1247 | */ |
||
| 1248 | 82 | private function hasQueryOperators($value) : bool |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1269 | */ |
||
| 1270 | 27 | private function getClassDiscriminatorValues(ClassMetadata $metadata) : array |
|
| 1289 | |||
| 1290 | 581 | private function handleCollections(object $document, array $options) : void |
|
| 1321 | |||
| 1322 | /** |
||
| 1323 | * If the document is new, ignore shard key field value, otherwise throw an |
||
| 1324 | * exception. Also, shard key field should be present in actual document |
||
| 1325 | * data. |
||
| 1326 | * |
||
| 1327 | * @throws MongoDBException |
||
| 1328 | */ |
||
| 1329 | 10 | private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData) : void |
|
| 1345 | |||
| 1346 | /** |
||
| 1347 | * Get shard key aware query for single document. |
||
| 1348 | */ |
||
| 1349 | 303 | private function getQueryForDocument(object $document) : array |
|
| 1357 | |||
| 1358 | 592 | private function getWriteOptions(array $options = []) : array |
|
| 1368 | |||
| 1369 | 15 | private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj) : array |
|
| 1409 | } |
||
| 1410 |
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.