Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 44 | class DocumentPersister |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * The PersistenceBuilder instance. |
||
| 48 | * |
||
| 49 | * @var PersistenceBuilder |
||
| 50 | */ |
||
| 51 | private $pb; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The DocumentManager instance. |
||
| 55 | * |
||
| 56 | * @var DocumentManager |
||
| 57 | */ |
||
| 58 | private $dm; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The EventManager instance |
||
| 62 | * |
||
| 63 | * @var EventManager |
||
| 64 | */ |
||
| 65 | private $evm; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The UnitOfWork instance. |
||
| 69 | * |
||
| 70 | * @var UnitOfWork |
||
| 71 | */ |
||
| 72 | private $uow; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The ClassMetadata instance for the document type being persisted. |
||
| 76 | * |
||
| 77 | * @var ClassMetadata |
||
| 78 | */ |
||
| 79 | private $class; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The MongoCollection instance for this document. |
||
| 83 | * |
||
| 84 | * @var \MongoCollection |
||
| 85 | */ |
||
| 86 | private $collection; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Array of queued inserts for the persister to insert. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $queuedInserts = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Array of queued inserts for the persister to insert. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $queuedUpserts = array(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The CriteriaMerger instance. |
||
| 104 | * |
||
| 105 | * @var CriteriaMerger |
||
| 106 | */ |
||
| 107 | private $cm; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The CollectionPersister instance. |
||
| 111 | * |
||
| 112 | * @var CollectionPersister |
||
| 113 | */ |
||
| 114 | private $cp; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Initializes a new DocumentPersister instance. |
||
| 118 | * |
||
| 119 | * @param PersistenceBuilder $pb |
||
| 120 | * @param DocumentManager $dm |
||
| 121 | * @param EventManager $evm |
||
| 122 | * @param UnitOfWork $uow |
||
| 123 | * @param HydratorFactory $hydratorFactory |
||
| 124 | * @param ClassMetadata $class |
||
| 125 | */ |
||
| 126 | 685 | public function __construct(PersistenceBuilder $pb, DocumentManager $dm, EventManager $evm, UnitOfWork $uow, HydratorFactory $hydratorFactory, ClassMetadata $class, CriteriaMerger $cm = null) |
|
| 127 | { |
||
| 128 | 685 | $this->pb = $pb; |
|
| 129 | 685 | $this->dm = $dm; |
|
| 130 | 685 | $this->evm = $evm; |
|
| 131 | 685 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 132 | 685 | $this->uow = $uow; |
|
| 133 | 685 | $this->hydratorFactory = $hydratorFactory; |
|
|
|
|||
| 134 | 685 | $this->class = $class; |
|
| 135 | 685 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 136 | 685 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 137 | 685 | } |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function getInserts() |
||
| 143 | { |
||
| 144 | return $this->queuedInserts; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param object $document |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function isQueuedForInsert($document) |
||
| 152 | { |
||
| 153 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a document to the queued insertions. |
||
| 158 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 159 | * |
||
| 160 | * @param object $document The document to queue for insertion. |
||
| 161 | */ |
||
| 162 | 486 | public function addInsert($document) |
|
| 163 | { |
||
| 164 | 486 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 165 | 486 | } |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function getUpserts() |
||
| 171 | { |
||
| 172 | return $this->queuedUpserts; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param object $document |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public function isQueuedForUpsert($document) |
||
| 180 | { |
||
| 181 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Adds a document to the queued upserts. |
||
| 186 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 187 | * |
||
| 188 | * @param object $document The document to queue for insertion. |
||
| 189 | */ |
||
| 190 | 76 | public function addUpsert($document) |
|
| 191 | 52 | { |
|
| 192 | 76 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 193 | 76 | } |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 197 | * |
||
| 198 | * @return ClassMetadata |
||
| 199 | */ |
||
| 200 | public function getClassMetadata() |
||
| 201 | { |
||
| 202 | return $this->class; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Executes all queued document insertions. |
||
| 207 | * |
||
| 208 | * Queued documents without an ID will inserted in a batch and queued |
||
| 209 | * documents with an ID will be upserted individually. |
||
| 210 | * |
||
| 211 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 212 | * |
||
| 213 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 214 | */ |
||
| 215 | 486 | public function executeInserts(array $options = array()) |
|
| 216 | { |
||
| 217 | 486 | if ( ! $this->queuedInserts) { |
|
| 218 | return; |
||
| 219 | } |
||
| 220 | |||
| 221 | 486 | $inserts = array(); |
|
| 222 | 486 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 223 | 486 | $data = $this->pb->prepareInsertData($document); |
|
| 224 | |||
| 225 | // Set the initial version for each insert |
||
| 226 | 485 | View Code Duplication | if ($this->class->isVersioned) { |
| 227 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 228 | 39 | if ($versionMapping['type'] === 'int') { |
|
| 229 | 37 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 230 | 37 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 231 | 39 | } elseif ($versionMapping['type'] === 'date') { |
|
| 232 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 233 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 234 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 235 | 2 | } |
|
| 236 | 39 | $data[$versionMapping['name']] = $nextVersion; |
|
| 237 | 39 | } |
|
| 238 | |||
| 239 | 485 | $inserts[$oid] = $data; |
|
| 240 | 485 | } |
|
| 241 | |||
| 242 | 485 | if ($inserts) { |
|
| 243 | try { |
||
| 244 | 485 | $this->collection->batchInsert($inserts, $options); |
|
| 245 | 485 | } catch (\MongoException $e) { |
|
| 246 | 7 | $this->queuedInserts = array(); |
|
| 247 | 7 | throw $e; |
|
| 248 | } |
||
| 249 | 485 | } |
|
| 250 | |||
| 251 | /* All collections except for ones using addToSet have already been |
||
| 252 | * saved. We have left these to be handled separately to avoid checking |
||
| 253 | * collection for uniqueness on PHP side. |
||
| 254 | */ |
||
| 255 | 485 | foreach ($this->queuedInserts as $document) { |
|
| 256 | 485 | $this->handleCollections($document, $options); |
|
| 257 | 485 | } |
|
| 258 | |||
| 259 | 485 | $this->queuedInserts = array(); |
|
| 260 | 485 | } |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Executes all queued document upserts. |
||
| 264 | * |
||
| 265 | * Queued documents with an ID are upserted individually. |
||
| 266 | * |
||
| 267 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 268 | * |
||
| 269 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 270 | */ |
||
| 271 | 76 | public function executeUpserts(array $options = array()) |
|
| 272 | { |
||
| 273 | 76 | if ( ! $this->queuedUpserts) { |
|
| 274 | return; |
||
| 275 | } |
||
| 276 | |||
| 277 | 76 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 278 | 76 | $data = $this->pb->prepareUpsertData($document); |
|
| 279 | |||
| 280 | // Set the initial version for each upsert |
||
| 281 | 76 | View Code Duplication | if ($this->class->isVersioned) { |
| 282 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 283 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 284 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 285 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 286 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 287 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 288 | 1 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 289 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 290 | 1 | } |
|
| 291 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 292 | 3 | } |
|
| 293 | |||
| 294 | try { |
||
| 295 | 76 | $this->executeUpsert($data, $options); |
|
| 296 | 76 | $this->handleCollections($document, $options); |
|
| 297 | 76 | unset($this->queuedUpserts[$oid]); |
|
| 298 | 76 | } catch (\MongoException $e) { |
|
| 299 | unset($this->queuedUpserts[$oid]); |
||
| 300 | throw $e; |
||
| 301 | } |
||
| 302 | 76 | } |
|
| 303 | 76 | } |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Executes a single upsert in {@link executeInserts} |
||
| 307 | * |
||
| 308 | * @param array $data |
||
| 309 | * @param array $options |
||
| 310 | */ |
||
| 311 | 76 | private function executeUpsert(array $data, array $options) |
|
| 312 | { |
||
| 313 | 76 | $options['upsert'] = true; |
|
| 314 | 76 | $criteria = array('_id' => $data['$set']['_id']); |
|
| 315 | 76 | unset($data['$set']['_id']); |
|
| 316 | |||
| 317 | // Do not send an empty $set modifier |
||
| 318 | 76 | if (empty($data['$set'])) { |
|
| 319 | 13 | unset($data['$set']); |
|
| 320 | 13 | } |
|
| 321 | |||
| 322 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 323 | * an identifier as its only field. Since a document with the identifier |
||
| 324 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 325 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 326 | * the identifier to the same value in our criteria. |
||
| 327 | * |
||
| 328 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 329 | * empty $set modifier. The best we can do (without attempting to check |
||
| 330 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 331 | * after the relevant exception. |
||
| 332 | * |
||
| 333 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 334 | */ |
||
| 335 | 76 | if (empty($data)) { |
|
| 336 | 13 | $retry = true; |
|
| 337 | 13 | $data = array('$set' => array('_id' => $criteria['_id'])); |
|
| 338 | 13 | } |
|
| 339 | |||
| 340 | try { |
||
| 341 | 76 | $this->collection->update($criteria, $data, $options); |
|
| 342 | 64 | return; |
|
| 343 | 14 | } catch (\MongoCursorException $e) { |
|
| 344 | 13 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
|
| 345 | throw $e; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | 13 | $this->collection->update($criteria, array('$set' => new \stdClass), $options); |
|
| 350 | 13 | } |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Updates the already persisted document if it has any new changesets. |
||
| 354 | * |
||
| 355 | * @param object $document |
||
| 356 | * @param array $options Array of options to be used with update() |
||
| 357 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 358 | */ |
||
| 359 | 211 | public function update($document, array $options = array()) |
|
| 360 | { |
||
| 361 | 211 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 362 | 211 | $update = $this->pb->prepareUpdateData($document); |
|
| 363 | |||
| 364 | 211 | $id = $this->class->getDatabaseIdentifierValue($id); |
|
| 365 | 211 | $query = array('_id' => $id); |
|
| 366 | |||
| 367 | // Include versioning logic to set the new version value in the database |
||
| 368 | // and to ensure the version has not changed since this document object instance |
||
| 369 | // was fetched from the database |
||
| 370 | 211 | if ($this->class->isVersioned) { |
|
| 371 | 31 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 372 | 31 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 373 | 31 | if ($versionMapping['type'] === 'int') { |
|
| 374 | 28 | $nextVersion = $currentVersion + 1; |
|
| 375 | 28 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 376 | 28 | $query[$versionMapping['name']] = $currentVersion; |
|
| 377 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 378 | 31 | } elseif ($versionMapping['type'] === 'date') { |
|
| 379 | 3 | $nextVersion = new \DateTime(); |
|
| 380 | 3 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
|
| 381 | 3 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
|
| 382 | 3 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 383 | 3 | } |
|
| 384 | 31 | } |
|
| 385 | |||
| 386 | 211 | if ( ! empty($update)) { |
|
| 387 | // Include locking logic so that if the document object in memory is currently |
||
| 388 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 389 | 150 | if ($this->class->isLockable) { |
|
| 390 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 391 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 392 | 11 | if ($isLocked) { |
|
| 393 | 2 | $update['$unset'] = array($lockMapping['name'] => true); |
|
| 394 | 2 | } else { |
|
| 395 | 9 | $query[$lockMapping['name']] = array('$exists' => false); |
|
| 396 | } |
||
| 397 | 11 | } |
|
| 398 | |||
| 399 | 150 | $result = $this->collection->update($query, $update, $options); |
|
| 400 | |||
| 401 | 150 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 402 | 5 | throw LockException::lockFailed($document); |
|
| 403 | } |
||
| 404 | 146 | } |
|
| 405 | |||
| 406 | 207 | $this->handleCollections($document, $options); |
|
| 407 | 207 | } |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Removes document from mongo |
||
| 411 | * |
||
| 412 | * @param mixed $document |
||
| 413 | * @param array $options Array of options to be used with remove() |
||
| 414 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 415 | */ |
||
| 416 | 28 | public function delete($document, array $options = array()) |
|
| 417 | { |
||
| 418 | 28 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 419 | 28 | $query = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
|
| 420 | |||
| 421 | 28 | if ($this->class->isLockable) { |
|
| 422 | 2 | $query[$this->class->lockField] = array('$exists' => false); |
|
| 423 | 2 | } |
|
| 424 | |||
| 425 | 28 | $result = $this->collection->remove($query, $options); |
|
| 426 | |||
| 427 | 28 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 428 | 2 | throw LockException::lockFailed($document); |
|
| 429 | } |
||
| 430 | 26 | } |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Refreshes a managed document. |
||
| 434 | * |
||
| 435 | * @param array $id The identifier of the document. |
||
| 436 | * @param object $document The document to refresh. |
||
| 437 | */ |
||
| 438 | 20 | public function refresh($id, $document) |
|
| 439 | { |
||
| 440 | 20 | $data = $this->collection->findOne(array('_id' => $id)); |
|
| 441 | 20 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 442 | 20 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 443 | 20 | } |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Finds a document by a set of criteria. |
||
| 447 | * |
||
| 448 | * If a scalar or MongoId is provided for $criteria, it will be used to |
||
| 449 | * match an _id value. |
||
| 450 | * |
||
| 451 | * @param mixed $criteria Query criteria |
||
| 452 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 453 | * @param array $hints Hints for document creation |
||
| 454 | * @param integer $lockMode |
||
| 455 | * @param array $sort Sort array for Cursor::sort() |
||
| 456 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 457 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 458 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 459 | */ |
||
| 460 | 357 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 461 | 1 | { |
|
| 462 | // TODO: remove this |
||
| 463 | 357 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) { |
|
| 464 | $criteria = array('_id' => $criteria); |
||
| 465 | } |
||
| 466 | |||
| 467 | 357 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 468 | 357 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 469 | 357 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 470 | |||
| 471 | 357 | $cursor = $this->collection->find($criteria); |
|
| 472 | |||
| 473 | 357 | if (null !== $sort) { |
|
| 474 | 101 | $cursor->sort($this->prepareSortOrProjection($sort)); |
|
| 475 | 101 | } |
|
| 476 | |||
| 477 | 357 | $result = $cursor->getSingleResult(); |
|
| 478 | |||
| 479 | 357 | if ($this->class->isLockable) { |
|
| 480 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 481 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 482 | 1 | throw LockException::lockFailed($result); |
|
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | 356 | return $this->createDocument($result, $document, $hints); |
|
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Finds documents by a set of criteria. |
||
| 491 | * |
||
| 492 | * @param array $criteria Query criteria |
||
| 493 | * @param array $sort Sort array for Cursor::sort() |
||
| 494 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 495 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 496 | * @return Cursor |
||
| 497 | */ |
||
| 498 | 22 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 499 | { |
||
| 500 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 501 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 502 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 503 | |||
| 504 | 22 | $baseCursor = $this->collection->find($criteria); |
|
| 505 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 506 | |||
| 507 | 22 | if (null !== $sort) { |
|
| 508 | 3 | $cursor->sort($sort); |
|
| 509 | 3 | } |
|
| 510 | |||
| 511 | 22 | if (null !== $limit) { |
|
| 512 | 2 | $cursor->limit($limit); |
|
| 513 | 2 | } |
|
| 514 | |||
| 515 | 22 | if (null !== $skip) { |
|
| 516 | 2 | $cursor->skip($skip); |
|
| 517 | 2 | } |
|
| 518 | |||
| 519 | 22 | return $cursor; |
|
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 524 | * |
||
| 525 | * @param CursorInterface $baseCursor |
||
| 526 | * @return Cursor |
||
| 527 | */ |
||
| 528 | 22 | private function wrapCursor(CursorInterface $baseCursor) |
|
| 529 | { |
||
| 530 | 22 | return new Cursor($baseCursor, $this->dm->getUnitOfWork(), $this->class); |
|
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Checks whether the given managed document exists in the database. |
||
| 535 | * |
||
| 536 | * @param object $document |
||
| 537 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 538 | */ |
||
| 539 | 3 | public function exists($document) |
|
| 540 | { |
||
| 541 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 542 | 3 | return (boolean) $this->collection->findOne(array('_id' => $id), array('_id')); |
|
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 547 | * |
||
| 548 | * @param object $document |
||
| 549 | * @param int $lockMode |
||
| 550 | */ |
||
| 551 | 5 | public function lock($document, $lockMode) |
|
| 552 | { |
||
| 553 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 554 | 5 | $criteria = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
|
| 555 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 556 | 5 | $this->collection->update($criteria, array('$set' => array($lockMapping['name'] => $lockMode))); |
|
| 557 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 558 | 5 | } |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Releases any lock that exists on this document. |
||
| 562 | * |
||
| 563 | * @param object $document |
||
| 564 | */ |
||
| 565 | 1 | public function unlock($document) |
|
| 566 | { |
||
| 567 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 568 | 1 | $criteria = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
|
| 569 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 570 | 1 | $this->collection->update($criteria, array('$unset' => array($lockMapping['name'] => true))); |
|
| 571 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 572 | 1 | } |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Creates or fills a single document object from an query result. |
||
| 576 | * |
||
| 577 | * @param object $result The query result. |
||
| 578 | * @param object $document The document object to fill, if any. |
||
| 579 | * @param array $hints Hints for document creation. |
||
| 580 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 581 | */ |
||
| 582 | 356 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 583 | { |
||
| 584 | 356 | if ($result === null) { |
|
| 585 | 115 | return null; |
|
| 586 | } |
||
| 587 | |||
| 588 | 304 | if ($document !== null) { |
|
| 589 | 36 | $hints[Query::HINT_REFRESH] = true; |
|
| 590 | 36 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 591 | 36 | $this->uow->registerManaged($document, $id, $result); |
|
| 592 | 36 | } |
|
| 593 | |||
| 594 | 304 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 599 | * |
||
| 600 | * @param PersistentCollection $collection |
||
| 601 | */ |
||
| 602 | 158 | public function loadCollection(PersistentCollection $collection) |
|
| 603 | { |
||
| 604 | 158 | $mapping = $collection->getMapping(); |
|
| 605 | 158 | switch ($mapping['association']) { |
|
| 606 | 158 | case ClassMetadata::EMBED_MANY: |
|
| 607 | 111 | $this->loadEmbedManyCollection($collection); |
|
| 608 | 111 | break; |
|
| 609 | |||
| 610 | 63 | case ClassMetadata::REFERENCE_MANY: |
|
| 611 | 63 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 612 | 3 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 613 | 3 | } else { |
|
| 614 | 60 | if ($mapping['isOwningSide']) { |
|
| 615 | 50 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 616 | 50 | } else { |
|
| 617 | 14 | $this->loadReferenceManyCollectionInverseSide($collection); |
|
| 618 | } |
||
| 619 | } |
||
| 620 | 63 | break; |
|
| 621 | 158 | } |
|
| 622 | 158 | } |
|
| 623 | |||
| 624 | 111 | private function loadEmbedManyCollection(PersistentCollection $collection) |
|
| 625 | { |
||
| 626 | 111 | $embeddedDocuments = $collection->getMongoData(); |
|
| 627 | 111 | $mapping = $collection->getMapping(); |
|
| 628 | 111 | $owner = $collection->getOwner(); |
|
| 629 | 111 | if ($embeddedDocuments) { |
|
| 630 | 82 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 631 | 82 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
| 632 | 82 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
| 633 | 82 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
| 634 | |||
| 635 | 82 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
|
| 636 | |||
| 637 | 82 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument); |
|
| 638 | 82 | $id = $embeddedMetadata->identifier && isset($data[$embeddedMetadata->identifier]) |
|
| 639 | 82 | ? $data[$embeddedMetadata->identifier] |
|
| 640 | 82 | : null; |
|
| 641 | |||
| 642 | 82 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
|
| 643 | 82 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 644 | 25 | $collection->set($key, $embeddedDocumentObject); |
|
| 645 | 25 | } else { |
|
| 646 | 64 | $collection->add($embeddedDocumentObject); |
|
| 647 | } |
||
| 648 | 82 | } |
|
| 649 | 82 | } |
|
| 650 | 111 | } |
|
| 651 | |||
| 652 | 50 | private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection) |
|
| 653 | { |
||
| 654 | 50 | $hints = $collection->getHints(); |
|
| 655 | 50 | $mapping = $collection->getMapping(); |
|
| 656 | 50 | $groupedIds = array(); |
|
| 657 | |||
| 658 | 50 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 659 | |||
| 660 | 50 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 661 | 45 | if (isset($mapping['simple']) && $mapping['simple']) { |
|
| 662 | 4 | $className = $mapping['targetDocument']; |
|
| 663 | 4 | $mongoId = $reference; |
|
| 664 | 4 | } else { |
|
| 665 | 41 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 666 | 41 | $mongoId = $reference['$id']; |
|
| 667 | } |
||
| 668 | 45 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId); |
|
| 669 | |||
| 670 | // create a reference to the class and id |
||
| 671 | 45 | $reference = $this->dm->getReference($className, $id); |
|
| 672 | |||
| 673 | // no custom sort so add the references right now in the order they are embedded |
||
| 674 | 45 | if ( ! $sorted) { |
|
| 675 | 44 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 676 | 2 | $collection->set($key, $reference); |
|
| 677 | 2 | } else { |
|
| 678 | 42 | $collection->add($reference); |
|
| 679 | } |
||
| 680 | 44 | } |
|
| 681 | |||
| 682 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 683 | 45 | if (($reference instanceof Proxy && ! $reference->__isInitialized__) || $sorted) { |
|
| 684 | 33 | $groupedIds[$className][] = $mongoId; |
|
| 685 | 33 | } |
|
| 686 | 50 | } |
|
| 687 | 50 | foreach ($groupedIds as $className => $ids) { |
|
| 688 | 33 | $class = $this->dm->getClassMetadata($className); |
|
| 689 | 33 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 690 | 33 | $criteria = $this->cm->merge( |
|
| 691 | 33 | array('_id' => array('$in' => array_values($ids))), |
|
| 692 | 33 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 693 | 33 | isset($mapping['criteria']) ? $mapping['criteria'] : array() |
|
| 694 | 33 | ); |
|
| 695 | 33 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 696 | 33 | $cursor = $mongoCollection->find($criteria); |
|
| 697 | 33 | if (isset($mapping['sort'])) { |
|
| 698 | 33 | $cursor->sort($mapping['sort']); |
|
| 699 | 33 | } |
|
| 700 | 33 | if (isset($mapping['limit'])) { |
|
| 701 | $cursor->limit($mapping['limit']); |
||
| 702 | } |
||
| 703 | 33 | if (isset($mapping['skip'])) { |
|
| 704 | $cursor->skip($mapping['skip']); |
||
| 705 | } |
||
| 706 | 33 | if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) { |
|
| 707 | $cursor->slaveOkay(true); |
||
| 708 | } |
||
| 709 | 33 | View Code Duplication | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
| 710 | $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); |
||
| 711 | } |
||
| 712 | 33 | $documents = $cursor->toArray(false); |
|
| 713 | 33 | foreach ($documents as $documentData) { |
|
| 714 | 32 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 715 | 32 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 716 | 32 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 717 | 32 | $document->__isInitialized__ = true; |
|
| 718 | 32 | if ($sorted) { |
|
| 719 | 1 | $collection->add($document); |
|
| 720 | 1 | } |
|
| 721 | 33 | } |
|
| 722 | 50 | } |
|
| 723 | 50 | } |
|
| 724 | |||
| 725 | 14 | private function loadReferenceManyCollectionInverseSide(PersistentCollection $collection) |
|
| 726 | { |
||
| 727 | 14 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 728 | 14 | $documents = $query->execute()->toArray(false); |
|
| 729 | 14 | foreach ($documents as $key => $document) { |
|
| 730 | 13 | $collection->add($document); |
|
| 731 | 14 | } |
|
| 732 | 14 | } |
|
| 733 | |||
| 734 | /** |
||
| 735 | * @param PersistentCollection $collection |
||
| 736 | * |
||
| 737 | * @return Query |
||
| 738 | */ |
||
| 739 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollection $collection) |
|
| 740 | { |
||
| 741 | 16 | $hints = $collection->getHints(); |
|
| 742 | 16 | $mapping = $collection->getMapping(); |
|
| 743 | 16 | $owner = $collection->getOwner(); |
|
| 744 | 16 | $ownerClass = $this->dm->getClassMetadata(get_class($owner)); |
|
| 745 | 16 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 746 | 16 | $mappedByMapping = isset($targetClass->fieldMappings[$mapping['mappedBy']]) ? $targetClass->fieldMappings[$mapping['mappedBy']] : array(); |
|
| 747 | 16 | $mappedByFieldName = isset($mappedByMapping['simple']) && $mappedByMapping['simple'] ? $mapping['mappedBy'] : $mapping['mappedBy'] . '.$id'; |
|
| 748 | 16 | $criteria = $this->cm->merge( |
|
| 749 | 16 | array($mappedByFieldName => $ownerClass->getIdentifierObject($owner)), |
|
| 750 | 16 | $this->dm->getFilterCollection()->getFilterCriteria($targetClass), |
|
| 751 | 16 | isset($mapping['criteria']) ? $mapping['criteria'] : array() |
|
| 752 | 16 | ); |
|
| 753 | 16 | $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); |
|
| 754 | 16 | $qb = $this->dm->createQueryBuilder($mapping['targetDocument']) |
|
| 755 | 16 | ->setQueryArray($criteria); |
|
| 756 | |||
| 757 | 16 | if (isset($mapping['sort'])) { |
|
| 758 | 16 | $qb->sort($mapping['sort']); |
|
| 759 | 16 | } |
|
| 760 | 16 | if (isset($mapping['limit'])) { |
|
| 761 | 1 | $qb->limit($mapping['limit']); |
|
| 762 | 1 | } |
|
| 763 | 16 | if (isset($mapping['skip'])) { |
|
| 764 | $qb->skip($mapping['skip']); |
||
| 765 | } |
||
| 766 | 16 | if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) { |
|
| 767 | $qb->slaveOkay(true); |
||
| 768 | } |
||
| 769 | 16 | View Code Duplication | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
| 770 | $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); |
||
| 771 | } |
||
| 772 | |||
| 773 | 16 | return $qb->getQuery(); |
|
| 774 | } |
||
| 775 | |||
| 776 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection) |
|
| 777 | { |
||
| 778 | 3 | $cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection); |
|
| 779 | 3 | $mapping = $collection->getMapping(); |
|
| 780 | 3 | $documents = $cursor->toArray(false); |
|
| 781 | 3 | foreach ($documents as $key => $obj) { |
|
| 782 | 3 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 783 | 1 | $collection->set($key, $obj); |
|
| 784 | 1 | } else { |
|
| 785 | 2 | $collection->add($obj); |
|
| 786 | } |
||
| 787 | 3 | } |
|
| 788 | 3 | } |
|
| 789 | |||
| 790 | /** |
||
| 791 | * @param PersistentCollection $collection |
||
| 792 | * |
||
| 793 | * @return CursorInterface |
||
| 794 | */ |
||
| 795 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) |
|
| 796 | { |
||
| 797 | 3 | $hints = $collection->getHints(); |
|
| 798 | 3 | $mapping = $collection->getMapping(); |
|
| 799 | 3 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 800 | 3 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 801 | 3 | ->$repositoryMethod($collection->getOwner()); |
|
| 802 | |||
| 803 | 3 | if ( ! $cursor instanceof CursorInterface) { |
|
| 804 | throw new \BadMethodCallException("Expected repository method {$repositoryMethod} to return a CursorInterface"); |
||
| 805 | } |
||
| 806 | |||
| 807 | 3 | if (isset($mapping['sort'])) { |
|
| 808 | 3 | $cursor->sort($mapping['sort']); |
|
| 809 | 3 | } |
|
| 810 | 3 | if (isset($mapping['limit'])) { |
|
| 811 | $cursor->limit($mapping['limit']); |
||
| 812 | } |
||
| 813 | 3 | if (isset($mapping['skip'])) { |
|
| 814 | $cursor->skip($mapping['skip']); |
||
| 815 | } |
||
| 816 | 3 | if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) { |
|
| 817 | $cursor->slaveOkay(true); |
||
| 818 | } |
||
| 819 | 3 | View Code Duplication | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
| 820 | $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); |
||
| 821 | } |
||
| 822 | |||
| 823 | 3 | return $cursor; |
|
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 828 | * property names, to MongoDB field names. |
||
| 829 | * |
||
| 830 | * @param array $fields |
||
| 831 | * @return array |
||
| 832 | */ |
||
| 833 | 138 | public function prepareSortOrProjection(array $fields) |
|
| 834 | { |
||
| 835 | 138 | $preparedFields = array(); |
|
| 836 | |||
| 837 | 138 | foreach ($fields as $key => $value) { |
|
| 838 | 33 | $preparedFields[$this->prepareFieldName($key)] = $value; |
|
| 839 | 138 | } |
|
| 840 | |||
| 841 | 138 | return $preparedFields; |
|
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 846 | * |
||
| 847 | * @param string $fieldName |
||
| 848 | * @return string |
||
| 849 | */ |
||
| 850 | 85 | public function prepareFieldName($fieldName) |
|
| 851 | { |
||
| 852 | 85 | list($fieldName) = $this->prepareQueryElement($fieldName, null, null, false); |
|
| 853 | |||
| 854 | 85 | return $fieldName; |
|
| 855 | } |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Adds discriminator criteria to an already-prepared query. |
||
| 859 | * |
||
| 860 | * This method should be used once for query criteria and not be used for |
||
| 861 | * nested expressions. It should be called before |
||
| 862 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 863 | * |
||
| 864 | * @param array $preparedQuery |
||
| 865 | * @return array |
||
| 866 | */ |
||
| 867 | 481 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 883 | |||
| 884 | /** |
||
| 885 | * Adds filter criteria to an already-prepared query. |
||
| 886 | * |
||
| 887 | * This method should be used once for query criteria and not be used for |
||
| 888 | * nested expressions. It should be called after |
||
| 889 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 890 | * |
||
| 891 | * @param array $preparedQuery |
||
| 892 | * @return array |
||
| 893 | */ |
||
| 894 | 482 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Prepares the query criteria or new document object. |
||
| 911 | * |
||
| 912 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 913 | * |
||
| 914 | * @param array $query |
||
| 915 | * @return array |
||
| 916 | */ |
||
| 917 | 515 | public function prepareQueryOrNewObj(array $query) |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Prepares a query value and converts the PHP value to the database value |
||
| 947 | * if it is an identifier. |
||
| 948 | * |
||
| 949 | * It also handles converting $fieldName to the database name if they are different. |
||
| 950 | * |
||
| 951 | * @param string $fieldName |
||
| 952 | * @param mixed $value |
||
| 953 | * @param ClassMetadata $class Defaults to $this->class |
||
| 954 | * @param boolean $prepareValue Whether or not to prepare the value |
||
| 955 | * @return array Prepared field name and value |
||
| 956 | */ |
||
| 957 | 508 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
|
| 958 | { |
||
| 959 | 508 | $class = isset($class) ? $class : $this->class; |
|
| 960 | |||
| 961 | // @todo Consider inlining calls to ClassMetadataInfo methods |
||
| 962 | |||
| 963 | // Process all non-identifier fields by translating field names |
||
| 964 | 508 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 965 | 236 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 966 | 236 | $fieldName = $mapping['name']; |
|
| 967 | |||
| 968 | 236 | if ( ! $prepareValue) { |
|
| 969 | 62 | return array($fieldName, $value); |
|
| 970 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Prepares a query expression. |
||
| 1143 | * |
||
| 1144 | * @param array|object $expression |
||
| 1145 | * @param ClassMetadata $class |
||
| 1146 | * @return array |
||
| 1147 | */ |
||
| 1148 | 66 | private function prepareQueryExpression($expression, $class) |
|
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Checks whether the value has DBRef fields. |
||
| 1178 | * |
||
| 1179 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1180 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1181 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1182 | * $elemMatch criteria for matching a DBRef. |
||
| 1183 | * |
||
| 1184 | * @param mixed $value |
||
| 1185 | * @return boolean |
||
| 1186 | */ |
||
| 1187 | 67 | private function hasDBRefFields($value) |
|
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Checks whether the value has query operators. |
||
| 1208 | * |
||
| 1209 | * @param mixed $value |
||
| 1210 | * @return boolean |
||
| 1211 | */ |
||
| 1212 | 71 | private function hasQueryOperators($value) |
|
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1233 | * |
||
| 1234 | * @param ClassMetadata $metadata |
||
| 1235 | * @return array |
||
| 1236 | */ |
||
| 1237 | 21 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1253 | |||
| 1254 | 549 | private function handleCollections($document, $options) |
|
| 1273 | } |
||
| 1274 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: