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 EventManager instance |
||
| 77 | * |
||
| 78 | * @var EventManager |
||
| 79 | */ |
||
| 80 | private $evm; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The UnitOfWork instance. |
||
| 84 | * |
||
| 85 | * @var UnitOfWork |
||
| 86 | */ |
||
| 87 | private $uow; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The ClassMetadata instance for the document type being persisted. |
||
| 91 | * |
||
| 92 | * @var ClassMetadata |
||
| 93 | */ |
||
| 94 | private $class; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The MongoCollection instance for this document. |
||
| 98 | * |
||
| 99 | * @var Collection |
||
| 100 | */ |
||
| 101 | private $collection; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Array of queued inserts for the persister to insert. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private $queuedInserts = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Array of queued inserts for the persister to insert. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private $queuedUpserts = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The CriteriaMerger instance. |
||
| 119 | * |
||
| 120 | * @var CriteriaMerger |
||
| 121 | */ |
||
| 122 | private $cm; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The CollectionPersister instance. |
||
| 126 | * |
||
| 127 | * @var CollectionPersister |
||
| 128 | */ |
||
| 129 | private $cp; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The HydratorFactory instance. |
||
| 133 | * |
||
| 134 | * @var HydratorFactory |
||
| 135 | */ |
||
| 136 | private $hydratorFactory; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Initializes this instance. |
||
| 140 | * |
||
| 141 | */ |
||
| 142 | 1073 | public function __construct( |
|
| 143 | PersistenceBuilder $pb, |
||
| 144 | DocumentManager $dm, |
||
| 145 | EventManager $evm, |
||
| 146 | UnitOfWork $uow, |
||
| 147 | HydratorFactory $hydratorFactory, |
||
| 148 | ClassMetadata $class, |
||
| 149 | ?CriteriaMerger $cm = null |
||
| 150 | ) { |
||
| 151 | 1073 | $this->pb = $pb; |
|
| 152 | 1073 | $this->dm = $dm; |
|
| 153 | 1073 | $this->evm = $evm; |
|
| 154 | 1073 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 155 | 1073 | $this->uow = $uow; |
|
| 156 | 1073 | $this->hydratorFactory = $hydratorFactory; |
|
| 157 | 1073 | $this->class = $class; |
|
| 158 | 1073 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 159 | 1073 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 160 | 1073 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function getInserts() |
||
| 166 | { |
||
| 167 | return $this->queuedInserts; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param object $document |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function isQueuedForInsert($document) |
||
| 175 | { |
||
| 176 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Adds a document to the queued insertions. |
||
| 181 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 182 | * |
||
| 183 | * @param object $document The document to queue for insertion. |
||
| 184 | */ |
||
| 185 | 476 | public function addInsert($document) |
|
| 186 | { |
||
| 187 | 476 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 188 | 476 | } |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getUpserts() |
||
| 194 | { |
||
| 195 | return $this->queuedUpserts; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param object $document |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function isQueuedForUpsert($document) |
||
| 203 | { |
||
| 204 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Adds a document to the queued upserts. |
||
| 209 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 210 | * |
||
| 211 | * @param object $document The document to queue for insertion. |
||
| 212 | */ |
||
| 213 | 83 | public function addUpsert($document) |
|
| 214 | { |
||
| 215 | 83 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 216 | 83 | } |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 220 | * |
||
| 221 | * @return ClassMetadata |
||
| 222 | */ |
||
| 223 | public function getClassMetadata() |
||
| 224 | { |
||
| 225 | return $this->class; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Executes all queued document insertions. |
||
| 230 | * |
||
| 231 | * Queued documents without an ID will inserted in a batch and queued |
||
| 232 | * documents with an ID will be upserted individually. |
||
| 233 | * |
||
| 234 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 235 | * |
||
| 236 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 237 | */ |
||
| 238 | 476 | public function executeInserts(array $options = []) |
|
| 239 | { |
||
| 240 | 476 | if (! $this->queuedInserts) { |
|
|
|
|||
| 241 | return; |
||
| 242 | } |
||
| 243 | |||
| 244 | 476 | $inserts = []; |
|
| 245 | 476 | $options = $this->getWriteOptions($options); |
|
| 246 | 476 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 247 | 476 | $data = $this->pb->prepareInsertData($document); |
|
| 248 | |||
| 249 | // Set the initial version for each insert |
||
| 250 | 475 | if ($this->class->isVersioned) { |
|
| 251 | 20 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 252 | 20 | $nextVersion = null; |
|
| 253 | 20 | if ($versionMapping['type'] === 'int') { |
|
| 254 | 18 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 255 | 18 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 256 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 257 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 258 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 259 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 260 | } |
||
| 261 | 20 | $data[$versionMapping['name']] = $nextVersion; |
|
| 262 | } |
||
| 263 | |||
| 264 | 475 | $inserts[] = $data; |
|
| 265 | } |
||
| 266 | |||
| 267 | 475 | if ($inserts) { |
|
| 268 | try { |
||
| 269 | 475 | $this->collection->insertMany($inserts, $options); |
|
| 270 | 6 | } catch (DriverException $e) { |
|
| 271 | 6 | $this->queuedInserts = []; |
|
| 272 | 6 | throw $e; |
|
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /* All collections except for ones using addToSet have already been |
||
| 277 | * saved. We have left these to be handled separately to avoid checking |
||
| 278 | * collection for uniqueness on PHP side. |
||
| 279 | */ |
||
| 280 | 475 | foreach ($this->queuedInserts as $document) { |
|
| 281 | 475 | $this->handleCollections($document, $options); |
|
| 282 | } |
||
| 283 | |||
| 284 | 475 | $this->queuedInserts = []; |
|
| 285 | 475 | } |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Executes all queued document upserts. |
||
| 289 | * |
||
| 290 | * Queued documents with an ID are upserted individually. |
||
| 291 | * |
||
| 292 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 293 | * |
||
| 294 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 295 | */ |
||
| 296 | 83 | public function executeUpserts(array $options = []) |
|
| 297 | { |
||
| 298 | 83 | if (! $this->queuedUpserts) { |
|
| 299 | return; |
||
| 300 | } |
||
| 301 | |||
| 302 | 83 | $options = $this->getWriteOptions($options); |
|
| 303 | 83 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 304 | try { |
||
| 305 | 83 | $this->executeUpsert($document, $options); |
|
| 306 | 83 | $this->handleCollections($document, $options); |
|
| 307 | 83 | unset($this->queuedUpserts[$oid]); |
|
| 308 | } catch (WriteException $e) { |
||
| 309 | unset($this->queuedUpserts[$oid]); |
||
| 310 | 83 | throw $e; |
|
| 311 | } |
||
| 312 | } |
||
| 313 | 83 | } |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Executes a single upsert in {@link executeUpserts} |
||
| 317 | * |
||
| 318 | * @param object $document |
||
| 319 | * @param array $options |
||
| 320 | */ |
||
| 321 | 83 | private function executeUpsert($document, array $options) |
|
| 322 | { |
||
| 323 | 83 | $options['upsert'] = true; |
|
| 324 | 83 | $criteria = $this->getQueryForDocument($document); |
|
| 325 | |||
| 326 | 83 | $data = $this->pb->prepareUpsertData($document); |
|
| 327 | |||
| 328 | // Set the initial version for each upsert |
||
| 329 | 83 | if ($this->class->isVersioned) { |
|
| 330 | 2 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 331 | 2 | $nextVersion = null; |
|
| 332 | 2 | if ($versionMapping['type'] === 'int') { |
|
| 333 | 1 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 334 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 335 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 336 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 337 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 338 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 339 | } |
||
| 340 | 2 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 341 | } |
||
| 342 | |||
| 343 | 83 | foreach (array_keys($criteria) as $field) { |
|
| 344 | 83 | unset($data['$set'][$field]); |
|
| 345 | } |
||
| 346 | |||
| 347 | // Do not send an empty $set modifier |
||
| 348 | 83 | if (empty($data['$set'])) { |
|
| 349 | 16 | unset($data['$set']); |
|
| 350 | } |
||
| 351 | |||
| 352 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 353 | * an identifier as its only field. Since a document with the identifier |
||
| 354 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 355 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 356 | * the identifier to the same value in our criteria. |
||
| 357 | * |
||
| 358 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 359 | * empty $set modifier. The best we can do (without attempting to check |
||
| 360 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 361 | * after the relevant exception. |
||
| 362 | * |
||
| 363 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 364 | */ |
||
| 365 | 83 | if (empty($data)) { |
|
| 366 | 16 | $retry = true; |
|
| 367 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 368 | } |
||
| 369 | |||
| 370 | try { |
||
| 371 | 83 | $this->collection->updateOne($criteria, $data, $options); |
|
| 372 | 83 | return; |
|
| 373 | } catch (WriteException $e) { |
||
| 374 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 375 | throw $e; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | $this->collection->updateOne($criteria, ['$set' => new \stdClass()], $options); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Updates the already persisted document if it has any new changesets. |
||
| 384 | * |
||
| 385 | * @param object $document |
||
| 386 | * @param array $options Array of options to be used with update() |
||
| 387 | * @throws LockException |
||
| 388 | */ |
||
| 389 | 195 | public function update($document, array $options = []) |
|
| 390 | { |
||
| 391 | 195 | $update = $this->pb->prepareUpdateData($document); |
|
| 392 | |||
| 393 | 195 | $query = $this->getQueryForDocument($document); |
|
| 394 | |||
| 395 | 195 | foreach (array_keys($query) as $field) { |
|
| 396 | 195 | unset($update['$set'][$field]); |
|
| 397 | } |
||
| 398 | |||
| 399 | 195 | if (empty($update['$set'])) { |
|
| 400 | 89 | unset($update['$set']); |
|
| 401 | } |
||
| 402 | |||
| 403 | // Include versioning logic to set the new version value in the database |
||
| 404 | // and to ensure the version has not changed since this document object instance |
||
| 405 | // was fetched from the database |
||
| 406 | 195 | $nextVersion = null; |
|
| 407 | 195 | if ($this->class->isVersioned) { |
|
| 408 | 13 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 409 | 13 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 410 | 13 | if ($versionMapping['type'] === 'int') { |
|
| 411 | 10 | $nextVersion = $currentVersion + 1; |
|
| 412 | 10 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 413 | 10 | $query[$versionMapping['name']] = $currentVersion; |
|
| 414 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 415 | 3 | $nextVersion = new \DateTime(); |
|
| 416 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 417 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | 195 | if (! empty($update)) { |
|
| 422 | // Include locking logic so that if the document object in memory is currently |
||
| 423 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 424 | 129 | if ($this->class->isLockable) { |
|
| 425 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 426 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 427 | 11 | if ($isLocked) { |
|
| 428 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 429 | } else { |
||
| 430 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | 129 | $options = $this->getWriteOptions($options); |
|
| 435 | |||
| 436 | 129 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 437 | |||
| 438 | 129 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 439 | 4 | throw LockException::lockFailed($document); |
|
| 440 | 125 | } elseif ($this->class->isVersioned) { |
|
| 441 | 9 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | 191 | $this->handleCollections($document, $options); |
|
| 446 | 191 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Removes document from mongo |
||
| 450 | * |
||
| 451 | * @param mixed $document |
||
| 452 | * @param array $options Array of options to be used with remove() |
||
| 453 | * @throws LockException |
||
| 454 | */ |
||
| 455 | 32 | public function delete($document, array $options = []) |
|
| 456 | { |
||
| 457 | 32 | $query = $this->getQueryForDocument($document); |
|
| 458 | |||
| 459 | 32 | if ($this->class->isLockable) { |
|
| 460 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 461 | } |
||
| 462 | |||
| 463 | 32 | $options = $this->getWriteOptions($options); |
|
| 464 | |||
| 465 | 32 | $result = $this->collection->deleteOne($query, $options); |
|
| 466 | |||
| 467 | 32 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 468 | 2 | throw LockException::lockFailed($document); |
|
| 469 | } |
||
| 470 | 30 | } |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Refreshes a managed document. |
||
| 474 | * |
||
| 475 | * @param object $document The document to refresh. |
||
| 476 | */ |
||
| 477 | 20 | public function refresh($document) |
|
| 478 | { |
||
| 479 | 20 | $query = $this->getQueryForDocument($document); |
|
| 480 | 20 | $data = $this->collection->findOne($query); |
|
| 481 | 20 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 482 | 20 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 483 | 20 | } |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Finds a document by a set of criteria. |
||
| 487 | * |
||
| 488 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 489 | * be used to match an _id value. |
||
| 490 | * |
||
| 491 | * @param mixed $criteria Query criteria |
||
| 492 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 493 | * @param array $hints Hints for document creation |
||
| 494 | * @param int $lockMode |
||
| 495 | * @param array $sort Sort array for Cursor::sort() |
||
| 496 | * @throws LockException |
||
| 497 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 498 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 499 | */ |
||
| 500 | 342 | public function load($criteria, $document = null, array $hints = [], $lockMode = 0, ?array $sort = null) |
|
| 501 | { |
||
| 502 | // TODO: remove this |
||
| 503 | 342 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 504 | $criteria = ['_id' => $criteria]; |
||
| 505 | } |
||
| 506 | |||
| 507 | 342 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 508 | 342 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 509 | 342 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 510 | |||
| 511 | 342 | $options = []; |
|
| 512 | 342 | if ($sort !== null) { |
|
| 513 | 92 | $options['sort'] = $this->prepareSort($sort); |
|
| 514 | } |
||
| 515 | 342 | $result = $this->collection->findOne($criteria, $options); |
|
| 516 | |||
| 517 | 342 | if ($this->class->isLockable) { |
|
| 518 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 519 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 520 | 1 | throw LockException::lockFailed($result); |
|
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | 341 | return $this->createDocument($result, $document, $hints); |
|
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Finds documents by a set of criteria. |
||
| 529 | * |
||
| 530 | * @param array $criteria Query criteria |
||
| 531 | * @param array $sort Sort array for Cursor::sort() |
||
| 532 | * @param int|null $limit Limit for Cursor::limit() |
||
| 533 | * @param int|null $skip Skip for Cursor::skip() |
||
| 534 | * @return Iterator |
||
| 535 | */ |
||
| 536 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, $limit = null, $skip = null) |
|
| 537 | { |
||
| 538 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 539 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 540 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 541 | |||
| 542 | 22 | $options = []; |
|
| 543 | 22 | if ($sort !== null) { |
|
| 544 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 545 | } |
||
| 546 | |||
| 547 | 22 | if ($limit !== null) { |
|
| 548 | 10 | $options['limit'] = $limit; |
|
| 549 | } |
||
| 550 | |||
| 551 | 22 | if ($skip !== null) { |
|
| 552 | 1 | $options['skip'] = $skip; |
|
| 553 | } |
||
| 554 | |||
| 555 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 556 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 557 | |||
| 558 | 22 | return $cursor; |
|
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param object $document |
||
| 563 | * |
||
| 564 | * @return array |
||
| 565 | * @throws MongoDBException |
||
| 566 | */ |
||
| 567 | 267 | private function getShardKeyQuery($document) |
|
| 568 | { |
||
| 569 | 267 | if (! $this->class->isSharded()) { |
|
| 570 | 263 | return []; |
|
| 571 | } |
||
| 572 | |||
| 573 | 4 | $shardKey = $this->class->getShardKey(); |
|
| 574 | 4 | $keys = array_keys($shardKey['keys']); |
|
| 575 | 4 | $data = $this->uow->getDocumentActualData($document); |
|
| 576 | |||
| 577 | 4 | $shardKeyQueryPart = []; |
|
| 578 | 4 | foreach ($keys as $key) { |
|
| 579 | 4 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 580 | 4 | $this->guardMissingShardKey($document, $key, $data); |
|
| 581 | |||
| 582 | 4 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 583 | 1 | $reference = $this->prepareReference( |
|
| 584 | 1 | $key, |
|
| 585 | 1 | $data[$mapping['fieldName']], |
|
| 586 | 1 | $mapping, |
|
| 587 | 1 | false |
|
| 588 | ); |
||
| 589 | 1 | foreach ($reference as $keyValue) { |
|
| 590 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 591 | } |
||
| 592 | } else { |
||
| 593 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 594 | 4 | $shardKeyQueryPart[$key] = $value; |
|
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | 4 | return $shardKeyQueryPart; |
|
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 603 | * |
||
| 604 | */ |
||
| 605 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 606 | { |
||
| 607 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Checks whether the given managed document exists in the database. |
||
| 612 | * |
||
| 613 | * @param object $document |
||
| 614 | * @return bool TRUE if the document exists in the database, FALSE otherwise. |
||
| 615 | */ |
||
| 616 | 3 | public function exists($document) |
|
| 617 | { |
||
| 618 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 619 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 624 | * |
||
| 625 | * @param object $document |
||
| 626 | * @param int $lockMode |
||
| 627 | */ |
||
| 628 | 5 | public function lock($document, $lockMode) |
|
| 629 | { |
||
| 630 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 631 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 632 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 633 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 634 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 635 | 5 | } |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Releases any lock that exists on this document. |
||
| 639 | * |
||
| 640 | * @param object $document |
||
| 641 | */ |
||
| 642 | 1 | public function unlock($document) |
|
| 643 | { |
||
| 644 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 645 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 646 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 647 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 648 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 649 | 1 | } |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Creates or fills a single document object from an query result. |
||
| 653 | * |
||
| 654 | * @param object $result The query result. |
||
| 655 | * @param object $document The document object to fill, if any. |
||
| 656 | * @param array $hints Hints for document creation. |
||
| 657 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 658 | */ |
||
| 659 | 341 | private function createDocument($result, $document = null, array $hints = []) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 676 | * |
||
| 677 | */ |
||
| 678 | 163 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 679 | { |
||
| 680 | 163 | $mapping = $collection->getMapping(); |
|
| 681 | 163 | switch ($mapping['association']) { |
|
| 682 | case ClassMetadata::EMBED_MANY: |
||
| 683 | 109 | $this->loadEmbedManyCollection($collection); |
|
| 684 | 109 | break; |
|
| 685 | |||
| 686 | case ClassMetadata::REFERENCE_MANY: |
||
| 687 | 76 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 688 | 5 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 689 | } else { |
||
| 690 | 72 | if ($mapping['isOwningSide']) { |
|
| 691 | 60 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 699 | |||
| 700 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 701 | { |
||
| 702 | 109 | $embeddedDocuments = $collection->getMongoData(); |
|
| 729 | |||
| 730 | 60 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 803 | |||
| 804 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 812 | |||
| 813 | /** |
||
| 814 | * |
||
| 815 | * @return Query |
||
| 816 | */ |
||
| 817 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 856 | |||
| 857 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 870 | |||
| 871 | /** |
||
| 872 | * |
||
| 873 | * @return \Iterator |
||
| 874 | */ |
||
| 875 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 896 | |||
| 897 | /** |
||
| 898 | * Prepare a projection array by converting keys, which are PHP property |
||
| 899 | * names, to MongoDB field names. |
||
| 900 | * |
||
| 901 | * @param array $fields |
||
| 902 | * @return array |
||
| 903 | */ |
||
| 904 | 14 | public function prepareProjection(array $fields) |
|
| 914 | |||
| 915 | /** |
||
| 916 | * @param string $sort |
||
| 917 | * @return int |
||
| 918 | */ |
||
| 919 | 25 | private function getSortDirection($sort) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 934 | * names and changing direction strings to int. |
||
| 935 | * |
||
| 936 | * @param array $fields |
||
| 937 | * @return array |
||
| 938 | */ |
||
| 939 | 141 | public function prepareSort(array $fields) |
|
| 949 | |||
| 950 | /** |
||
| 951 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 952 | * |
||
| 953 | * @param string $fieldName |
||
| 954 | * @return string |
||
| 955 | */ |
||
| 956 | 433 | public function prepareFieldName($fieldName) |
|
| 962 | |||
| 963 | /** |
||
| 964 | * Adds discriminator criteria to an already-prepared query. |
||
| 965 | * |
||
| 966 | * This method should be used once for query criteria and not be used for |
||
| 967 | * nested expressions. It should be called before |
||
| 968 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 969 | * |
||
| 970 | * @param array $preparedQuery |
||
| 971 | * @return array |
||
| 972 | */ |
||
| 973 | 492 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 989 | |||
| 990 | /** |
||
| 991 | * Adds filter criteria to an already-prepared query. |
||
| 992 | * |
||
| 993 | * This method should be used once for query criteria and not be used for |
||
| 994 | * nested expressions. It should be called after |
||
| 995 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 996 | * |
||
| 997 | * @param array $preparedQuery |
||
| 998 | * @return array |
||
| 999 | */ |
||
| 1000 | 493 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Prepares the query criteria or new document object. |
||
| 1018 | * |
||
| 1019 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 1020 | * |
||
| 1021 | * @param array $query |
||
| 1022 | * @param bool $isNewObj |
||
| 1023 | * @return array |
||
| 1024 | */ |
||
| 1025 | 525 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Prepares a query value and converts the PHP value to the database value |
||
| 1056 | * if it is an identifier. |
||
| 1057 | * |
||
| 1058 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1059 | * |
||
| 1060 | * @param string $fieldName |
||
| 1061 | * @param mixed $value |
||
| 1062 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1063 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1064 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1065 | * @return array An array of tuples containing prepared field names and values |
||
| 1066 | */ |
||
| 1067 | 876 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Prepares a query expression. |
||
| 1257 | * |
||
| 1258 | * @param array|object $expression |
||
| 1259 | * @param ClassMetadata $class |
||
| 1260 | * @return array |
||
| 1261 | */ |
||
| 1262 | 78 | private function prepareQueryExpression($expression, $class) |
|
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Checks whether the value has DBRef fields. |
||
| 1292 | * |
||
| 1293 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1294 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1295 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1296 | * $elemMatch criteria for matching a DBRef. |
||
| 1297 | * |
||
| 1298 | * @param mixed $value |
||
| 1299 | * @return bool |
||
| 1300 | */ |
||
| 1301 | 79 | private function hasDBRefFields($value) |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Checks whether the value has query operators. |
||
| 1322 | * |
||
| 1323 | * @param mixed $value |
||
| 1324 | * @return bool |
||
| 1325 | */ |
||
| 1326 | 83 | private function hasQueryOperators($value) |
|
| 1344 | |||
| 1345 | /** |
||
| 1346 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1347 | * |
||
| 1348 | * @return array |
||
| 1349 | */ |
||
| 1350 | 29 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1369 | |||
| 1370 | 546 | private function handleCollections($document, $options) |
|
| 1393 | |||
| 1394 | /** |
||
| 1395 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1396 | * Also, shard key field should be present in actual document data. |
||
| 1397 | * |
||
| 1398 | * @param object $document |
||
| 1399 | * @param string $shardKeyField |
||
| 1400 | * @param array $actualDocumentData |
||
| 1401 | * |
||
| 1402 | * @throws MongoDBException |
||
| 1403 | */ |
||
| 1404 | 4 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1420 | |||
| 1421 | /** |
||
| 1422 | * Get shard key aware query for single document. |
||
| 1423 | * |
||
| 1424 | * @param object $document |
||
| 1425 | * |
||
| 1426 | * @return array |
||
| 1427 | */ |
||
| 1428 | 263 | private function getQueryForDocument($document) |
|
| 1438 | |||
| 1439 | /** |
||
| 1440 | * @param array $options |
||
| 1441 | * |
||
| 1442 | * @return array |
||
| 1443 | */ |
||
| 1444 | 547 | private function getWriteOptions(array $options = []) |
|
| 1454 | |||
| 1455 | /** |
||
| 1456 | * @param string $fieldName |
||
| 1457 | * @param mixed $value |
||
| 1458 | * @param array $mapping |
||
| 1459 | * @param bool $inNewObj |
||
| 1460 | * @return array |
||
| 1461 | */ |
||
| 1462 | 15 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1502 | } |
||
| 1503 |
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.