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 |
||
| 59 | class DocumentPersister |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * The PersistenceBuilder instance. |
||
| 63 | * |
||
| 64 | * @var PersistenceBuilder |
||
| 65 | */ |
||
| 66 | private $pb; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The DocumentManager instance. |
||
| 70 | * |
||
| 71 | * @var DocumentManager |
||
| 72 | */ |
||
| 73 | private $dm; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The UnitOfWork instance. |
||
| 77 | * |
||
| 78 | * @var UnitOfWork |
||
| 79 | */ |
||
| 80 | private $uow; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The ClassMetadata instance for the document type being persisted. |
||
| 84 | * |
||
| 85 | * @var ClassMetadata |
||
| 86 | */ |
||
| 87 | private $class; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The MongoCollection instance for this document. |
||
| 91 | * |
||
| 92 | * @var Collection |
||
| 93 | */ |
||
| 94 | private $collection; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Array of queued inserts for the persister to insert. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private $queuedInserts = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Array of queued inserts for the persister to insert. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private $queuedUpserts = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The CriteriaMerger instance. |
||
| 112 | * |
||
| 113 | * @var CriteriaMerger |
||
| 114 | */ |
||
| 115 | private $cm; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The CollectionPersister instance. |
||
| 119 | * |
||
| 120 | * @var CollectionPersister |
||
| 121 | */ |
||
| 122 | private $cp; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The HydratorFactory instance. |
||
| 126 | * |
||
| 127 | * @var HydratorFactory |
||
| 128 | */ |
||
| 129 | private $hydratorFactory; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Initializes this instance. |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | 1073 | public function __construct( |
|
| 136 | PersistenceBuilder $pb, |
||
| 137 | DocumentManager $dm, |
||
| 138 | UnitOfWork $uow, |
||
| 139 | HydratorFactory $hydratorFactory, |
||
| 140 | ClassMetadata $class, |
||
| 141 | ?CriteriaMerger $cm = null |
||
| 142 | ) { |
||
| 143 | 1073 | $this->pb = $pb; |
|
| 144 | 1073 | $this->dm = $dm; |
|
| 145 | 1073 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 146 | 1073 | $this->uow = $uow; |
|
| 147 | 1073 | $this->hydratorFactory = $hydratorFactory; |
|
| 148 | 1073 | $this->class = $class; |
|
| 149 | 1073 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 150 | 1073 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 151 | 1073 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getInserts() |
||
| 157 | { |
||
| 158 | return $this->queuedInserts; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param object $document |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function isQueuedForInsert($document) |
||
| 166 | { |
||
| 167 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Adds a document to the queued insertions. |
||
| 172 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 173 | * |
||
| 174 | * @param object $document The document to queue for insertion. |
||
| 175 | */ |
||
| 176 | 476 | public function addInsert($document) |
|
| 177 | { |
||
| 178 | 476 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 179 | 476 | } |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getUpserts() |
||
| 185 | { |
||
| 186 | return $this->queuedUpserts; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param object $document |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isQueuedForUpsert($document) |
||
| 194 | { |
||
| 195 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds a document to the queued upserts. |
||
| 200 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 201 | * |
||
| 202 | * @param object $document The document to queue for insertion. |
||
| 203 | */ |
||
| 204 | 83 | public function addUpsert($document) |
|
| 205 | { |
||
| 206 | 83 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 207 | 83 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 211 | * |
||
| 212 | * @return ClassMetadata |
||
| 213 | */ |
||
| 214 | public function getClassMetadata() |
||
| 215 | { |
||
| 216 | return $this->class; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Executes all queued document insertions. |
||
| 221 | * |
||
| 222 | * Queued documents without an ID will inserted in a batch and queued |
||
| 223 | * documents with an ID will be upserted individually. |
||
| 224 | * |
||
| 225 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 226 | * |
||
| 227 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 228 | */ |
||
| 229 | 476 | public function executeInserts(array $options = []) |
|
| 230 | { |
||
| 231 | 476 | if (! $this->queuedInserts) { |
|
|
|
|||
| 232 | return; |
||
| 233 | } |
||
| 234 | |||
| 235 | 476 | $inserts = []; |
|
| 236 | 476 | $options = $this->getWriteOptions($options); |
|
| 237 | 476 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 238 | 476 | $data = $this->pb->prepareInsertData($document); |
|
| 239 | |||
| 240 | // Set the initial version for each insert |
||
| 241 | 475 | if ($this->class->isVersioned) { |
|
| 242 | 20 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 243 | 20 | $nextVersion = null; |
|
| 244 | 20 | if ($versionMapping['type'] === 'int') { |
|
| 245 | 18 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 246 | 18 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 247 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 248 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 249 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 250 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 251 | } |
||
| 252 | 20 | $data[$versionMapping['name']] = $nextVersion; |
|
| 253 | } |
||
| 254 | |||
| 255 | 475 | $inserts[] = $data; |
|
| 256 | } |
||
| 257 | |||
| 258 | 475 | if ($inserts) { |
|
| 259 | try { |
||
| 260 | 475 | $this->collection->insertMany($inserts, $options); |
|
| 261 | 6 | } catch (DriverException $e) { |
|
| 262 | 6 | $this->queuedInserts = []; |
|
| 263 | 6 | throw $e; |
|
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /* All collections except for ones using addToSet have already been |
||
| 268 | * saved. We have left these to be handled separately to avoid checking |
||
| 269 | * collection for uniqueness on PHP side. |
||
| 270 | */ |
||
| 271 | 475 | foreach ($this->queuedInserts as $document) { |
|
| 272 | 475 | $this->handleCollections($document, $options); |
|
| 273 | } |
||
| 274 | |||
| 275 | 475 | $this->queuedInserts = []; |
|
| 276 | 475 | } |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Executes all queued document upserts. |
||
| 280 | * |
||
| 281 | * Queued documents with an ID are upserted individually. |
||
| 282 | * |
||
| 283 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 284 | * |
||
| 285 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 286 | */ |
||
| 287 | 83 | public function executeUpserts(array $options = []) |
|
| 288 | { |
||
| 289 | 83 | if (! $this->queuedUpserts) { |
|
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | 83 | $options = $this->getWriteOptions($options); |
|
| 294 | 83 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 295 | try { |
||
| 296 | 83 | $this->executeUpsert($document, $options); |
|
| 297 | 83 | $this->handleCollections($document, $options); |
|
| 298 | 83 | unset($this->queuedUpserts[$oid]); |
|
| 299 | } catch (WriteException $e) { |
||
| 300 | unset($this->queuedUpserts[$oid]); |
||
| 301 | 83 | throw $e; |
|
| 302 | } |
||
| 303 | } |
||
| 304 | 83 | } |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Executes a single upsert in {@link executeUpserts} |
||
| 308 | * |
||
| 309 | * @param object $document |
||
| 310 | * @param array $options |
||
| 311 | */ |
||
| 312 | 83 | private function executeUpsert($document, array $options) |
|
| 313 | { |
||
| 314 | 83 | $options['upsert'] = true; |
|
| 315 | 83 | $criteria = $this->getQueryForDocument($document); |
|
| 316 | |||
| 317 | 83 | $data = $this->pb->prepareUpsertData($document); |
|
| 318 | |||
| 319 | // Set the initial version for each upsert |
||
| 320 | 83 | if ($this->class->isVersioned) { |
|
| 321 | 2 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 322 | 2 | $nextVersion = null; |
|
| 323 | 2 | if ($versionMapping['type'] === 'int') { |
|
| 324 | 1 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 325 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 326 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 327 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 328 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 329 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 330 | } |
||
| 331 | 2 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 332 | } |
||
| 333 | |||
| 334 | 83 | foreach (array_keys($criteria) as $field) { |
|
| 335 | 83 | unset($data['$set'][$field]); |
|
| 336 | 83 | unset($data['$inc'][$field]); |
|
| 337 | 83 | unset($data['$setOnInsert'][$field]); |
|
| 338 | } |
||
| 339 | |||
| 340 | // Do not send empty update operators |
||
| 341 | 83 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 342 | 83 | if (empty($data[$operator])) { |
|
| 343 | 83 | unset($data[$operator]); |
|
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 348 | * an identifier as its only field. Since a document with the identifier |
||
| 349 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 350 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 351 | * the identifier to the same value in our criteria. |
||
| 352 | * |
||
| 353 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 354 | * empty $set modifier. The best we can do (without attempting to check |
||
| 355 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 356 | * after the relevant exception. |
||
| 357 | * |
||
| 358 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 359 | */ |
||
| 360 | 83 | if (empty($data)) { |
|
| 361 | 16 | $retry = true; |
|
| 362 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 363 | } |
||
| 364 | |||
| 365 | try { |
||
| 366 | 83 | $this->collection->updateOne($criteria, $data, $options); |
|
| 367 | 83 | return; |
|
| 368 | } catch (WriteException $e) { |
||
| 369 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 370 | throw $e; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | $this->collection->updateOne($criteria, ['$set' => new \stdClass()], $options); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Updates the already persisted document if it has any new changesets. |
||
| 379 | * |
||
| 380 | * @param object $document |
||
| 381 | * @param array $options Array of options to be used with update() |
||
| 382 | * @throws LockException |
||
| 383 | */ |
||
| 384 | 195 | public function update($document, array $options = []) |
|
| 385 | { |
||
| 386 | 195 | $update = $this->pb->prepareUpdateData($document); |
|
| 387 | |||
| 388 | 195 | $query = $this->getQueryForDocument($document); |
|
| 389 | |||
| 390 | 195 | foreach (array_keys($query) as $field) { |
|
| 391 | 195 | unset($update['$set'][$field]); |
|
| 392 | } |
||
| 393 | |||
| 394 | 195 | if (empty($update['$set'])) { |
|
| 395 | 89 | unset($update['$set']); |
|
| 396 | } |
||
| 397 | |||
| 398 | // Include versioning logic to set the new version value in the database |
||
| 399 | // and to ensure the version has not changed since this document object instance |
||
| 400 | // was fetched from the database |
||
| 401 | 195 | $nextVersion = null; |
|
| 402 | 195 | if ($this->class->isVersioned) { |
|
| 403 | 13 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 404 | 13 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 405 | 13 | if ($versionMapping['type'] === 'int') { |
|
| 406 | 10 | $nextVersion = $currentVersion + 1; |
|
| 407 | 10 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 408 | 10 | $query[$versionMapping['name']] = $currentVersion; |
|
| 409 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 410 | 3 | $nextVersion = new \DateTime(); |
|
| 411 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 412 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | 195 | if (! empty($update)) { |
|
| 417 | // Include locking logic so that if the document object in memory is currently |
||
| 418 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 419 | 129 | if ($this->class->isLockable) { |
|
| 420 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 421 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 422 | 11 | if ($isLocked) { |
|
| 423 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 424 | } else { |
||
| 425 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | 129 | $options = $this->getWriteOptions($options); |
|
| 430 | |||
| 431 | 129 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 432 | |||
| 433 | 129 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 434 | 4 | throw LockException::lockFailed($document); |
|
| 435 | 125 | } elseif ($this->class->isVersioned) { |
|
| 436 | 9 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | 191 | $this->handleCollections($document, $options); |
|
| 441 | 191 | } |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Removes document from mongo |
||
| 445 | * |
||
| 446 | * @param mixed $document |
||
| 447 | * @param array $options Array of options to be used with remove() |
||
| 448 | * @throws LockException |
||
| 449 | */ |
||
| 450 | 32 | public function delete($document, array $options = []) |
|
| 451 | { |
||
| 452 | 32 | $query = $this->getQueryForDocument($document); |
|
| 453 | |||
| 454 | 32 | if ($this->class->isLockable) { |
|
| 455 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 456 | } |
||
| 457 | |||
| 458 | 32 | $options = $this->getWriteOptions($options); |
|
| 459 | |||
| 460 | 32 | $result = $this->collection->deleteOne($query, $options); |
|
| 461 | |||
| 462 | 32 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 463 | 2 | throw LockException::lockFailed($document); |
|
| 464 | } |
||
| 465 | 30 | } |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Refreshes a managed document. |
||
| 469 | * |
||
| 470 | * @param object $document The document to refresh. |
||
| 471 | */ |
||
| 472 | 20 | public function refresh($document) |
|
| 473 | { |
||
| 474 | 20 | $query = $this->getQueryForDocument($document); |
|
| 475 | 20 | $data = $this->collection->findOne($query); |
|
| 476 | 20 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 477 | 20 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 478 | 20 | } |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Finds a document by a set of criteria. |
||
| 482 | * |
||
| 483 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 484 | * be used to match an _id value. |
||
| 485 | * |
||
| 486 | * @param mixed $criteria Query criteria |
||
| 487 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 488 | * @param array $hints Hints for document creation |
||
| 489 | * @param int $lockMode |
||
| 490 | * @param array $sort Sort array for Cursor::sort() |
||
| 491 | * @throws LockException |
||
| 492 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 493 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 494 | */ |
||
| 495 | 342 | public function load($criteria, $document = null, array $hints = [], $lockMode = 0, ?array $sort = null) |
|
| 496 | { |
||
| 497 | // TODO: remove this |
||
| 498 | 342 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 499 | $criteria = ['_id' => $criteria]; |
||
| 500 | } |
||
| 501 | |||
| 502 | 342 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 503 | 342 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 504 | 342 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 505 | |||
| 506 | 342 | $options = []; |
|
| 507 | 342 | if ($sort !== null) { |
|
| 508 | 92 | $options['sort'] = $this->prepareSort($sort); |
|
| 509 | } |
||
| 510 | 342 | $result = $this->collection->findOne($criteria, $options); |
|
| 511 | |||
| 512 | 342 | if ($this->class->isLockable) { |
|
| 513 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 514 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 515 | 1 | throw LockException::lockFailed($result); |
|
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | 341 | return $this->createDocument($result, $document, $hints); |
|
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Finds documents by a set of criteria. |
||
| 524 | * |
||
| 525 | * @param array $criteria Query criteria |
||
| 526 | * @param array $sort Sort array for Cursor::sort() |
||
| 527 | * @param int|null $limit Limit for Cursor::limit() |
||
| 528 | * @param int|null $skip Skip for Cursor::skip() |
||
| 529 | * @return Iterator |
||
| 530 | */ |
||
| 531 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, $limit = null, $skip = null) |
|
| 532 | { |
||
| 533 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 534 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 535 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 536 | |||
| 537 | 22 | $options = []; |
|
| 538 | 22 | if ($sort !== null) { |
|
| 539 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 540 | } |
||
| 541 | |||
| 542 | 22 | if ($limit !== null) { |
|
| 543 | 10 | $options['limit'] = $limit; |
|
| 544 | } |
||
| 545 | |||
| 546 | 22 | if ($skip !== null) { |
|
| 547 | 1 | $options['skip'] = $skip; |
|
| 548 | } |
||
| 549 | |||
| 550 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 551 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 552 | |||
| 553 | 22 | return $cursor; |
|
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param object $document |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | * @throws MongoDBException |
||
| 561 | */ |
||
| 562 | 267 | private function getShardKeyQuery($document) |
|
| 563 | { |
||
| 564 | 267 | if (! $this->class->isSharded()) { |
|
| 565 | 263 | return []; |
|
| 566 | } |
||
| 567 | |||
| 568 | 4 | $shardKey = $this->class->getShardKey(); |
|
| 569 | 4 | $keys = array_keys($shardKey['keys']); |
|
| 570 | 4 | $data = $this->uow->getDocumentActualData($document); |
|
| 571 | |||
| 572 | 4 | $shardKeyQueryPart = []; |
|
| 573 | 4 | foreach ($keys as $key) { |
|
| 574 | 4 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 575 | 4 | $this->guardMissingShardKey($document, $key, $data); |
|
| 576 | |||
| 577 | 4 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 578 | 1 | $reference = $this->prepareReference( |
|
| 579 | 1 | $key, |
|
| 580 | 1 | $data[$mapping['fieldName']], |
|
| 581 | 1 | $mapping, |
|
| 582 | 1 | false |
|
| 583 | ); |
||
| 584 | 1 | foreach ($reference as $keyValue) { |
|
| 585 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 586 | } |
||
| 587 | } else { |
||
| 588 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 589 | 4 | $shardKeyQueryPart[$key] = $value; |
|
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | 4 | return $shardKeyQueryPart; |
|
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 598 | * |
||
| 599 | */ |
||
| 600 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 601 | { |
||
| 602 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Checks whether the given managed document exists in the database. |
||
| 607 | * |
||
| 608 | * @param object $document |
||
| 609 | * @return bool TRUE if the document exists in the database, FALSE otherwise. |
||
| 610 | */ |
||
| 611 | 3 | public function exists($document) |
|
| 612 | { |
||
| 613 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 614 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 619 | * |
||
| 620 | * @param object $document |
||
| 621 | * @param int $lockMode |
||
| 622 | */ |
||
| 623 | 5 | public function lock($document, $lockMode) |
|
| 624 | { |
||
| 625 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 626 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 627 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 628 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 629 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 630 | 5 | } |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Releases any lock that exists on this document. |
||
| 634 | * |
||
| 635 | * @param object $document |
||
| 636 | */ |
||
| 637 | 1 | public function unlock($document) |
|
| 638 | { |
||
| 639 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 640 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 641 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 642 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 643 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 644 | 1 | } |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Creates or fills a single document object from an query result. |
||
| 648 | * |
||
| 649 | * @param object $result The query result. |
||
| 650 | * @param object $document The document object to fill, if any. |
||
| 651 | * @param array $hints Hints for document creation. |
||
| 652 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 653 | */ |
||
| 654 | 341 | private function createDocument($result, $document = null, array $hints = []) |
|
| 655 | { |
||
| 656 | 341 | if ($result === null) { |
|
| 657 | 111 | return null; |
|
| 658 | } |
||
| 659 | |||
| 660 | 300 | if ($document !== null) { |
|
| 661 | 38 | $hints[Query::HINT_REFRESH] = true; |
|
| 662 | 38 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 663 | 38 | $this->uow->registerManaged($document, $id, $result); |
|
| 664 | } |
||
| 665 | |||
| 666 | 300 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 671 | * |
||
| 672 | */ |
||
| 673 | 163 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 694 | |||
| 695 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 696 | { |
||
| 697 | 109 | $embeddedDocuments = $collection->getMongoData(); |
|
| 698 | 109 | $mapping = $collection->getMapping(); |
|
| 699 | 109 | $owner = $collection->getOwner(); |
|
| 700 | 109 | if (! $embeddedDocuments) { |
|
| 701 | 58 | return; |
|
| 702 | } |
||
| 703 | |||
| 704 | 82 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 705 | 82 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
| 706 | 82 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
| 707 | 82 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
| 708 | |||
| 709 | 82 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
|
| 710 | |||
| 711 | 82 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints()); |
|
| 712 | 82 | $id = $data[$embeddedMetadata->identifier] ?? null; |
|
| 713 | |||
| 714 | 82 | if (empty($collection->getHints()[Query::HINT_READ_ONLY])) { |
|
| 715 | 81 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
|
| 716 | } |
||
| 717 | 82 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 718 | 10 | $collection->set($key, $embeddedDocumentObject); |
|
| 719 | } else { |
||
| 720 | 82 | $collection->add($embeddedDocumentObject); |
|
| 721 | } |
||
| 722 | } |
||
| 723 | 82 | } |
|
| 724 | |||
| 725 | 60 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 726 | { |
||
| 727 | 60 | $hints = $collection->getHints(); |
|
| 728 | 60 | $mapping = $collection->getMapping(); |
|
| 729 | 60 | $groupedIds = []; |
|
| 730 | |||
| 731 | 60 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 732 | |||
| 733 | 60 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 734 | 54 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 735 | 54 | $identifier = ClassMetadata::getReferenceId($reference, $mapping['storeAs']); |
|
| 736 | 54 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier); |
|
| 737 | |||
| 738 | // create a reference to the class and id |
||
| 739 | 54 | $reference = $this->dm->getReference($className, $id); |
|
| 740 | |||
| 741 | // no custom sort so add the references right now in the order they are embedded |
||
| 742 | 54 | if (! $sorted) { |
|
| 743 | 53 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 744 | 2 | $collection->set($key, $reference); |
|
| 745 | } else { |
||
| 746 | 51 | $collection->add($reference); |
|
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 751 | 54 | if (! (($reference instanceof Proxy && ! $reference->__isInitialized__)) && ! $sorted) { |
|
| 752 | 21 | continue; |
|
| 753 | } |
||
| 754 | |||
| 755 | 39 | $groupedIds[$className][] = $identifier; |
|
| 756 | } |
||
| 757 | 60 | foreach ($groupedIds as $className => $ids) { |
|
| 758 | 39 | $class = $this->dm->getClassMetadata($className); |
|
| 759 | 39 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 760 | 39 | $criteria = $this->cm->merge( |
|
| 761 | 39 | ['_id' => ['$in' => array_values($ids)]], |
|
| 762 | 39 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 763 | 39 | $mapping['criteria'] ?? [] |
|
| 764 | ); |
||
| 765 | 39 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 766 | |||
| 767 | 39 | $options = []; |
|
| 768 | 39 | if (isset($mapping['sort'])) { |
|
| 769 | 39 | $options['sort'] = $this->prepareSort($mapping['sort']); |
|
| 770 | } |
||
| 771 | 39 | if (isset($mapping['limit'])) { |
|
| 772 | $options['limit'] = $mapping['limit']; |
||
| 773 | } |
||
| 774 | 39 | if (isset($mapping['skip'])) { |
|
| 775 | $options['skip'] = $mapping['skip']; |
||
| 776 | } |
||
| 777 | 39 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 778 | $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE]; |
||
| 779 | } |
||
| 780 | |||
| 781 | 39 | $cursor = $mongoCollection->find($criteria, $options); |
|
| 782 | 39 | $documents = $cursor->toArray(); |
|
| 783 | 39 | foreach ($documents as $documentData) { |
|
| 784 | 38 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 785 | 38 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 786 | 38 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 787 | 38 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 788 | 38 | $document->__isInitialized__ = true; |
|
| 789 | } |
||
| 790 | 38 | if (! $sorted) { |
|
| 791 | 37 | continue; |
|
| 792 | } |
||
| 793 | |||
| 794 | 39 | $collection->add($document); |
|
| 795 | } |
||
| 796 | } |
||
| 797 | 60 | } |
|
| 798 | |||
| 799 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 800 | { |
||
| 801 | 17 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 802 | 17 | $documents = $query->execute()->toArray(); |
|
| 803 | 17 | foreach ($documents as $key => $document) { |
|
| 804 | 16 | $collection->add($document); |
|
| 805 | } |
||
| 806 | 17 | } |
|
| 807 | |||
| 808 | /** |
||
| 809 | * |
||
| 810 | * @return Query |
||
| 811 | */ |
||
| 812 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 813 | { |
||
| 814 | 17 | $hints = $collection->getHints(); |
|
| 815 | 17 | $mapping = $collection->getMapping(); |
|
| 816 | 17 | $owner = $collection->getOwner(); |
|
| 817 | 17 | $ownerClass = $this->dm->getClassMetadata(get_class($owner)); |
|
| 818 | 17 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 819 | 17 | $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']] ?? []; |
|
| 820 | 17 | $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']); |
|
| 821 | |||
| 822 | 17 | $criteria = $this->cm->merge( |
|
| 823 | 17 | [$mappedByFieldName => $ownerClass->getIdentifierObject($owner)], |
|
| 824 | 17 | $this->dm->getFilterCollection()->getFilterCriteria($targetClass), |
|
| 825 | 17 | $mapping['criteria'] ?? [] |
|
| 826 | ); |
||
| 827 | 17 | $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); |
|
| 828 | 17 | $qb = $this->dm->createQueryBuilder($mapping['targetDocument']) |
|
| 829 | 17 | ->setQueryArray($criteria); |
|
| 830 | |||
| 831 | 17 | if (isset($mapping['sort'])) { |
|
| 832 | 17 | $qb->sort($mapping['sort']); |
|
| 833 | } |
||
| 834 | 17 | if (isset($mapping['limit'])) { |
|
| 835 | 2 | $qb->limit($mapping['limit']); |
|
| 836 | } |
||
| 837 | 17 | if (isset($mapping['skip'])) { |
|
| 838 | $qb->skip($mapping['skip']); |
||
| 839 | } |
||
| 840 | |||
| 841 | 17 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 842 | $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE]); |
||
| 843 | } |
||
| 844 | |||
| 845 | 17 | foreach ($mapping['prime'] as $field) { |
|
| 846 | 4 | $qb->field($field)->prime(true); |
|
| 847 | } |
||
| 848 | |||
| 849 | 17 | return $qb->getQuery(); |
|
| 850 | } |
||
| 851 | |||
| 852 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 853 | { |
||
| 854 | 5 | $cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection); |
|
| 855 | 5 | $mapping = $collection->getMapping(); |
|
| 856 | 5 | $documents = $cursor->toArray(); |
|
| 857 | 5 | foreach ($documents as $key => $obj) { |
|
| 858 | 5 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 859 | 1 | $collection->set($key, $obj); |
|
| 860 | } else { |
||
| 861 | 5 | $collection->add($obj); |
|
| 862 | } |
||
| 863 | } |
||
| 864 | 5 | } |
|
| 865 | |||
| 866 | /** |
||
| 867 | * |
||
| 868 | * @return \Iterator |
||
| 869 | */ |
||
| 870 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 871 | { |
||
| 872 | 5 | $mapping = $collection->getMapping(); |
|
| 873 | 5 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 874 | 5 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 875 | 5 | ->$repositoryMethod($collection->getOwner()); |
|
| 876 | |||
| 877 | 5 | if (! $cursor instanceof Iterator) { |
|
| 878 | throw new \BadMethodCallException(sprintf('Expected repository method %s to return an iterable object', $repositoryMethod)); |
||
| 879 | } |
||
| 880 | |||
| 881 | 5 | if (! empty($mapping['prime'])) { |
|
| 882 | 1 | $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); |
|
| 883 | 1 | $primers = array_combine($mapping['prime'], array_fill(0, count($mapping['prime']), true)); |
|
| 884 | 1 | $class = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 885 | |||
| 886 | 1 | $cursor = new PrimingIterator($cursor, $class, $referencePrimer, $primers, $collection->getHints()); |
|
| 887 | } |
||
| 888 | |||
| 889 | 5 | return $cursor; |
|
| 890 | } |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Prepare a projection array by converting keys, which are PHP property |
||
| 894 | * names, to MongoDB field names. |
||
| 895 | * |
||
| 896 | * @param array $fields |
||
| 897 | * @return array |
||
| 898 | */ |
||
| 899 | 14 | public function prepareProjection(array $fields) |
|
| 900 | { |
||
| 901 | 14 | $preparedFields = []; |
|
| 902 | |||
| 903 | 14 | foreach ($fields as $key => $value) { |
|
| 904 | 14 | $preparedFields[$this->prepareFieldName($key)] = $value; |
|
| 905 | } |
||
| 906 | |||
| 907 | 14 | return $preparedFields; |
|
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @param string $sort |
||
| 912 | * @return int |
||
| 913 | */ |
||
| 914 | 25 | private function getSortDirection($sort) |
|
| 915 | { |
||
| 916 | 25 | switch (strtolower((string) $sort)) { |
|
| 917 | 25 | case 'desc': |
|
| 918 | 15 | return -1; |
|
| 919 | |||
| 920 | 22 | case 'asc': |
|
| 921 | 13 | return 1; |
|
| 922 | } |
||
| 923 | |||
| 924 | 12 | return $sort; |
|
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 929 | * names and changing direction strings to int. |
||
| 930 | * |
||
| 931 | * @param array $fields |
||
| 932 | * @return array |
||
| 933 | */ |
||
| 934 | 141 | public function prepareSort(array $fields) |
|
| 935 | { |
||
| 936 | 141 | $sortFields = []; |
|
| 937 | |||
| 938 | 141 | foreach ($fields as $key => $value) { |
|
| 939 | 25 | $sortFields[$this->prepareFieldName($key)] = $this->getSortDirection($value); |
|
| 940 | } |
||
| 941 | |||
| 942 | 141 | return $sortFields; |
|
| 943 | } |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 947 | * |
||
| 948 | * @param string $fieldName |
||
| 949 | * @return string |
||
| 950 | */ |
||
| 951 | 433 | public function prepareFieldName($fieldName) |
|
| 952 | { |
||
| 953 | 433 | $fieldNames = $this->prepareQueryElement($fieldName, null, null, false); |
|
| 954 | |||
| 955 | 433 | return $fieldNames[0][0]; |
|
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Adds discriminator criteria to an already-prepared query. |
||
| 960 | * |
||
| 961 | * This method should be used once for query criteria and not be used for |
||
| 962 | * nested expressions. It should be called before |
||
| 963 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 964 | * |
||
| 965 | * @param array $preparedQuery |
||
| 966 | * @return array |
||
| 967 | */ |
||
| 968 | 492 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 969 | { |
||
| 970 | /* If the class has a discriminator field, which is not already in the |
||
| 971 | * criteria, inject it now. The field/values need no preparation. |
||
| 972 | */ |
||
| 973 | 492 | if ($this->class->hasDiscriminator() && ! isset($preparedQuery[$this->class->discriminatorField])) { |
|
| 974 | 29 | $discriminatorValues = $this->getClassDiscriminatorValues($this->class); |
|
| 975 | 29 | if (count($discriminatorValues) === 1) { |
|
| 976 | 21 | $preparedQuery[$this->class->discriminatorField] = $discriminatorValues[0]; |
|
| 977 | } else { |
||
| 978 | 10 | $preparedQuery[$this->class->discriminatorField] = ['$in' => $discriminatorValues]; |
|
| 979 | } |
||
| 980 | } |
||
| 981 | |||
| 982 | 492 | return $preparedQuery; |
|
| 983 | } |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Adds filter criteria to an already-prepared query. |
||
| 987 | * |
||
| 988 | * This method should be used once for query criteria and not be used for |
||
| 989 | * nested expressions. It should be called after |
||
| 990 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 991 | * |
||
| 992 | * @param array $preparedQuery |
||
| 993 | * @return array |
||
| 994 | */ |
||
| 995 | 493 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 996 | { |
||
| 997 | /* If filter criteria exists for this class, prepare it and merge |
||
| 998 | * over the existing query. |
||
| 999 | * |
||
| 1000 | * @todo Consider recursive merging in case the filter criteria and |
||
| 1001 | * prepared query both contain top-level $and/$or operators. |
||
| 1002 | */ |
||
| 1003 | 493 | $filterCriteria = $this->dm->getFilterCollection()->getFilterCriteria($this->class); |
|
| 1004 | 493 | if ($filterCriteria) { |
|
| 1005 | 18 | $preparedQuery = $this->cm->merge($preparedQuery, $this->prepareQueryOrNewObj($filterCriteria)); |
|
| 1006 | } |
||
| 1007 | |||
| 1008 | 493 | return $preparedQuery; |
|
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Prepares the query criteria or new document object. |
||
| 1013 | * |
||
| 1014 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 1015 | * |
||
| 1016 | * @param array $query |
||
| 1017 | * @param bool $isNewObj |
||
| 1018 | * @return array |
||
| 1019 | */ |
||
| 1020 | 525 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Prepares a query value and converts the PHP value to the database value |
||
| 1051 | * if it is an identifier. |
||
| 1052 | * |
||
| 1053 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1054 | * |
||
| 1055 | * @param string $fieldName |
||
| 1056 | * @param mixed $value |
||
| 1057 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1058 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1059 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1060 | * @return array An array of tuples containing prepared field names and values |
||
| 1061 | */ |
||
| 1062 | 876 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Prepares a query expression. |
||
| 1252 | * |
||
| 1253 | * @param array|object $expression |
||
| 1254 | * @param ClassMetadata $class |
||
| 1255 | * @return array |
||
| 1256 | */ |
||
| 1257 | 78 | private function prepareQueryExpression($expression, $class) |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Checks whether the value has DBRef fields. |
||
| 1287 | * |
||
| 1288 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1289 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1290 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1291 | * $elemMatch criteria for matching a DBRef. |
||
| 1292 | * |
||
| 1293 | * @param mixed $value |
||
| 1294 | * @return bool |
||
| 1295 | */ |
||
| 1296 | 79 | private function hasDBRefFields($value) |
|
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Checks whether the value has query operators. |
||
| 1317 | * |
||
| 1318 | * @param mixed $value |
||
| 1319 | * @return bool |
||
| 1320 | */ |
||
| 1321 | 83 | private function hasQueryOperators($value) |
|
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1342 | * |
||
| 1343 | * @return array |
||
| 1344 | */ |
||
| 1345 | 29 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1346 | { |
||
| 1347 | 29 | $discriminatorValues = [$metadata->discriminatorValue]; |
|
| 1348 | 29 | foreach ($metadata->subClasses as $className) { |
|
| 1349 | 8 | $key = array_search($className, $metadata->discriminatorMap); |
|
| 1350 | 8 | if (! $key) { |
|
| 1351 | continue; |
||
| 1352 | } |
||
| 1353 | |||
| 1354 | 8 | $discriminatorValues[] = $key; |
|
| 1355 | } |
||
| 1356 | |||
| 1357 | // If a defaultDiscriminatorValue is set and it is among the discriminators being queries, add NULL to the list |
||
| 1358 | 29 | if ($metadata->defaultDiscriminatorValue && array_search($metadata->defaultDiscriminatorValue, $discriminatorValues) !== false) { |
|
| 1359 | 2 | $discriminatorValues[] = null; |
|
| 1360 | } |
||
| 1361 | |||
| 1362 | 29 | return $discriminatorValues; |
|
| 1363 | } |
||
| 1364 | |||
| 1365 | 546 | private function handleCollections($document, $options) |
|
| 1366 | { |
||
| 1367 | // Collection deletions (deletions of complete collections) |
||
| 1368 | 546 | foreach ($this->uow->getScheduledCollections($document) as $coll) { |
|
| 1369 | 103 | if (! $this->uow->isCollectionScheduledForDeletion($coll)) { |
|
| 1370 | 93 | continue; |
|
| 1371 | } |
||
| 1372 | |||
| 1373 | 30 | $this->cp->delete($coll, $options); |
|
| 1374 | } |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1391 | * Also, shard key field should be present in actual document data. |
||
| 1392 | * |
||
| 1393 | * @param object $document |
||
| 1394 | * @param string $shardKeyField |
||
| 1395 | * @param array $actualDocumentData |
||
| 1396 | * |
||
| 1397 | * @throws MongoDBException |
||
| 1398 | */ |
||
| 1399 | 4 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Get shard key aware query for single document. |
||
| 1418 | * |
||
| 1419 | * @param object $document |
||
| 1420 | * |
||
| 1421 | * @return array |
||
| 1422 | */ |
||
| 1423 | 263 | private function getQueryForDocument($document) |
|
| 1433 | |||
| 1434 | /** |
||
| 1435 | * @param array $options |
||
| 1436 | * |
||
| 1437 | * @return array |
||
| 1438 | */ |
||
| 1439 | 547 | private function getWriteOptions(array $options = []) |
|
| 1449 | |||
| 1450 | /** |
||
| 1451 | * @param string $fieldName |
||
| 1452 | * @param mixed $value |
||
| 1453 | * @param array $mapping |
||
| 1454 | * @param bool $inNewObj |
||
| 1455 | * @return array |
||
| 1456 | */ |
||
| 1457 | 15 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1497 | } |
||
| 1498 |
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.