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 |
||
| 48 | class DocumentPersister |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * The PersistenceBuilder instance. |
||
| 52 | * |
||
| 53 | * @var PersistenceBuilder |
||
| 54 | */ |
||
| 55 | private $pb; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The DocumentManager instance. |
||
| 59 | * |
||
| 60 | * @var DocumentManager |
||
| 61 | */ |
||
| 62 | private $dm; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The EventManager instance |
||
| 66 | * |
||
| 67 | * @var EventManager |
||
| 68 | */ |
||
| 69 | private $evm; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The UnitOfWork instance. |
||
| 73 | * |
||
| 74 | * @var UnitOfWork |
||
| 75 | */ |
||
| 76 | private $uow; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The ClassMetadata instance for the document type being persisted. |
||
| 80 | * |
||
| 81 | * @var ClassMetadata |
||
| 82 | */ |
||
| 83 | private $class; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The MongoCollection instance for this document. |
||
| 87 | * |
||
| 88 | * @var \MongoCollection |
||
| 89 | */ |
||
| 90 | private $collection; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Array of queued inserts for the persister to insert. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private $queuedInserts = array(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Array of queued inserts for the persister to insert. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private $queuedUpserts = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The CriteriaMerger instance. |
||
| 108 | * |
||
| 109 | * @var CriteriaMerger |
||
| 110 | */ |
||
| 111 | private $cm; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The CollectionPersister instance. |
||
| 115 | * |
||
| 116 | * @var CollectionPersister |
||
| 117 | */ |
||
| 118 | private $cp; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Initializes this instance. |
||
| 122 | * |
||
| 123 | * @param PersistenceBuilder $pb |
||
| 124 | * @param DocumentManager $dm |
||
| 125 | * @param EventManager $evm |
||
| 126 | * @param UnitOfWork $uow |
||
| 127 | * @param HydratorFactory $hydratorFactory |
||
| 128 | * @param ClassMetadata $class |
||
| 129 | * @param CriteriaMerger $cm |
||
| 130 | */ |
||
| 131 | 769 | public function __construct( |
|
| 132 | PersistenceBuilder $pb, |
||
| 133 | DocumentManager $dm, |
||
| 134 | EventManager $evm, |
||
| 135 | UnitOfWork $uow, |
||
| 136 | HydratorFactory $hydratorFactory, |
||
| 137 | ClassMetadata $class, |
||
| 138 | CriteriaMerger $cm = null |
||
| 139 | ) { |
||
| 140 | 769 | $this->pb = $pb; |
|
| 141 | 769 | $this->dm = $dm; |
|
| 142 | 769 | $this->evm = $evm; |
|
| 143 | 769 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 144 | 769 | $this->uow = $uow; |
|
| 145 | 769 | $this->hydratorFactory = $hydratorFactory; |
|
|
|
|||
| 146 | 769 | $this->class = $class; |
|
| 147 | 769 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 148 | 769 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 149 | 769 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function getInserts() |
||
| 155 | { |
||
| 156 | return $this->queuedInserts; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param object $document |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function isQueuedForInsert($document) |
||
| 164 | { |
||
| 165 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds a document to the queued insertions. |
||
| 170 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 171 | * |
||
| 172 | * @param object $document The document to queue for insertion. |
||
| 173 | */ |
||
| 174 | 551 | public function addInsert($document) |
|
| 175 | { |
||
| 176 | 551 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 177 | 551 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function getUpserts() |
||
| 183 | { |
||
| 184 | return $this->queuedUpserts; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param object $document |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public function isQueuedForUpsert($document) |
||
| 192 | { |
||
| 193 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Adds a document to the queued upserts. |
||
| 198 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 199 | * |
||
| 200 | * @param object $document The document to queue for insertion. |
||
| 201 | */ |
||
| 202 | 88 | public function addUpsert($document) |
|
| 203 | { |
||
| 204 | 88 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 205 | 88 | } |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 209 | * |
||
| 210 | * @return ClassMetadata |
||
| 211 | */ |
||
| 212 | public function getClassMetadata() |
||
| 213 | { |
||
| 214 | return $this->class; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Executes all queued document insertions. |
||
| 219 | * |
||
| 220 | * Queued documents without an ID will inserted in a batch and queued |
||
| 221 | * documents with an ID will be upserted individually. |
||
| 222 | * |
||
| 223 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 224 | * |
||
| 225 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 226 | */ |
||
| 227 | 551 | public function executeInserts(array $options = array()) |
|
| 228 | { |
||
| 229 | 551 | if ( ! $this->queuedInserts) { |
|
| 230 | return; |
||
| 231 | } |
||
| 232 | |||
| 233 | 551 | $inserts = array(); |
|
| 234 | 551 | $options = $this->getWriteOptions($options); |
|
| 235 | 551 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 236 | 551 | $data = $this->pb->prepareInsertData($document); |
|
| 237 | |||
| 238 | // Set the initial version for each insert |
||
| 239 | 550 | View Code Duplication | if ($this->class->isVersioned) { |
| 240 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 241 | 39 | if ($versionMapping['type'] === 'int') { |
|
| 242 | 37 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 243 | 37 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 244 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 245 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 246 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 247 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 248 | } |
||
| 249 | 39 | $data[$versionMapping['name']] = $nextVersion; |
|
| 250 | } |
||
| 251 | |||
| 252 | 550 | $inserts[$oid] = $data; |
|
| 253 | } |
||
| 254 | |||
| 255 | 550 | if ($inserts) { |
|
| 256 | try { |
||
| 257 | 550 | $this->collection->batchInsert($inserts, $options); |
|
| 258 | 7 | } catch (\MongoException $e) { |
|
| 259 | 7 | $this->queuedInserts = array(); |
|
| 260 | 7 | throw $e; |
|
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /* All collections except for ones using addToSet have already been |
||
| 265 | * saved. We have left these to be handled separately to avoid checking |
||
| 266 | * collection for uniqueness on PHP side. |
||
| 267 | */ |
||
| 268 | 550 | foreach ($this->queuedInserts as $document) { |
|
| 269 | 550 | $this->handleCollections($document, $options); |
|
| 270 | } |
||
| 271 | |||
| 272 | 550 | $this->queuedInserts = array(); |
|
| 273 | 550 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Executes all queued document upserts. |
||
| 277 | * |
||
| 278 | * Queued documents with an ID are upserted individually. |
||
| 279 | * |
||
| 280 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 281 | * |
||
| 282 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 283 | */ |
||
| 284 | 88 | public function executeUpserts(array $options = array()) |
|
| 285 | { |
||
| 286 | 88 | if ( ! $this->queuedUpserts) { |
|
| 287 | return; |
||
| 288 | } |
||
| 289 | |||
| 290 | 88 | $options = $this->getWriteOptions($options); |
|
| 291 | 88 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 292 | try { |
||
| 293 | 88 | $this->executeUpsert($document, $options); |
|
| 294 | 88 | $this->handleCollections($document, $options); |
|
| 295 | 88 | unset($this->queuedUpserts[$oid]); |
|
| 296 | } catch (\MongoException $e) { |
||
| 297 | unset($this->queuedUpserts[$oid]); |
||
| 298 | 88 | throw $e; |
|
| 299 | } |
||
| 300 | } |
||
| 301 | 88 | } |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Executes a single upsert in {@link executeUpserts} |
||
| 305 | * |
||
| 306 | * @param object $document |
||
| 307 | * @param array $options |
||
| 308 | */ |
||
| 309 | 88 | private function executeUpsert($document, array $options) |
|
| 310 | { |
||
| 311 | 88 | $options['upsert'] = true; |
|
| 312 | 88 | $criteria = $this->getQueryForDocument($document); |
|
| 313 | |||
| 314 | 88 | $data = $this->pb->prepareUpsertData($document); |
|
| 315 | |||
| 316 | // Set the initial version for each upsert |
||
| 317 | 88 | View Code Duplication | if ($this->class->isVersioned) { |
| 318 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 319 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 320 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 321 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 322 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 323 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 324 | 1 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 325 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 326 | } |
||
| 327 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 328 | } |
||
| 329 | |||
| 330 | 88 | foreach (array_keys($criteria) as $field) { |
|
| 331 | 88 | unset($data['$set'][$field]); |
|
| 332 | } |
||
| 333 | |||
| 334 | // Do not send an empty $set modifier |
||
| 335 | 88 | if (empty($data['$set'])) { |
|
| 336 | 17 | unset($data['$set']); |
|
| 337 | } |
||
| 338 | |||
| 339 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 340 | * an identifier as its only field. Since a document with the identifier |
||
| 341 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 342 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 343 | * the identifier to the same value in our criteria. |
||
| 344 | * |
||
| 345 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 346 | * empty $set modifier. The best we can do (without attempting to check |
||
| 347 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 348 | * after the relevant exception. |
||
| 349 | * |
||
| 350 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 351 | */ |
||
| 352 | 88 | if (empty($data)) { |
|
| 353 | 17 | $retry = true; |
|
| 354 | 17 | $data = array('$set' => array('_id' => $criteria['_id'])); |
|
| 355 | } |
||
| 356 | |||
| 357 | try { |
||
| 358 | 88 | $this->collection->update($criteria, $data, $options); |
|
| 359 | 88 | return; |
|
| 360 | } catch (\MongoCursorException $e) { |
||
| 361 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 362 | throw $e; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | $this->collection->update($criteria, array('$set' => new \stdClass), $options); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Updates the already persisted document if it has any new changesets. |
||
| 371 | * |
||
| 372 | * @param object $document |
||
| 373 | * @param array $options Array of options to be used with update() |
||
| 374 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 375 | */ |
||
| 376 | 227 | public function update($document, array $options = array()) |
|
| 377 | { |
||
| 378 | 227 | $update = $this->pb->prepareUpdateData($document); |
|
| 379 | |||
| 380 | 227 | $query = $this->getQueryForDocument($document); |
|
| 381 | |||
| 382 | 225 | foreach (array_keys($query) as $field) { |
|
| 383 | 225 | unset($update['$set'][$field]); |
|
| 384 | } |
||
| 385 | |||
| 386 | 225 | if (empty($update['$set'])) { |
|
| 387 | 94 | unset($update['$set']); |
|
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | // Include versioning logic to set the new version value in the database |
||
| 392 | // and to ensure the version has not changed since this document object instance |
||
| 393 | // was fetched from the database |
||
| 394 | 225 | $nextVersion = null; |
|
| 395 | 225 | if ($this->class->isVersioned) { |
|
| 396 | 33 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 397 | 33 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 398 | 33 | if ($versionMapping['type'] === 'int') { |
|
| 399 | 30 | $nextVersion = $currentVersion + 1; |
|
| 400 | 30 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 401 | 30 | $query[$versionMapping['name']] = $currentVersion; |
|
| 402 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 403 | 3 | $nextVersion = new \DateTime(); |
|
| 404 | 3 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
|
| 405 | 3 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
|
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | 225 | if ( ! empty($update)) { |
|
| 410 | // Include locking logic so that if the document object in memory is currently |
||
| 411 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 412 | 157 | if ($this->class->isLockable) { |
|
| 413 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 414 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 415 | 11 | if ($isLocked) { |
|
| 416 | 2 | $update['$unset'] = array($lockMapping['name'] => true); |
|
| 417 | } else { |
||
| 418 | 9 | $query[$lockMapping['name']] = array('$exists' => false); |
|
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | 157 | $options = $this->getWriteOptions($options); |
|
| 423 | |||
| 424 | 157 | $result = $this->collection->update($query, $update, $options); |
|
| 425 | |||
| 426 | 157 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
|
| 427 | 6 | throw LockException::lockFailed($document); |
|
| 428 | 152 | } elseif ($this->class->isVersioned) { |
|
| 429 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | 220 | $this->handleCollections($document, $options); |
|
| 434 | 220 | } |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Removes document from mongo |
||
| 438 | * |
||
| 439 | * @param mixed $document |
||
| 440 | * @param array $options Array of options to be used with remove() |
||
| 441 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 442 | */ |
||
| 443 | 35 | public function delete($document, array $options = array()) |
|
| 444 | { |
||
| 445 | 35 | $query = $this->getQueryForDocument($document); |
|
| 446 | |||
| 447 | 35 | if ($this->class->isLockable) { |
|
| 448 | 2 | $query[$this->class->lockField] = array('$exists' => false); |
|
| 449 | } |
||
| 450 | |||
| 451 | 35 | $options = $this->getWriteOptions($options); |
|
| 452 | |||
| 453 | 35 | $result = $this->collection->remove($query, $options); |
|
| 454 | |||
| 455 | 35 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
|
| 456 | 2 | throw LockException::lockFailed($document); |
|
| 457 | } |
||
| 458 | 33 | } |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Refreshes a managed document. |
||
| 462 | * |
||
| 463 | * @param object $document The document to refresh. |
||
| 464 | */ |
||
| 465 | 22 | public function refresh($document) |
|
| 466 | { |
||
| 467 | 22 | $query = $this->getQueryForDocument($document); |
|
| 468 | 22 | $data = $this->collection->findOne($query); |
|
| 469 | 22 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 470 | 22 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 471 | 22 | } |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Finds a document by a set of criteria. |
||
| 475 | * |
||
| 476 | * If a scalar or MongoId is provided for $criteria, it will be used to |
||
| 477 | * match an _id value. |
||
| 478 | * |
||
| 479 | * @param mixed $criteria Query criteria |
||
| 480 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 481 | * @param array $hints Hints for document creation |
||
| 482 | * @param integer $lockMode |
||
| 483 | * @param array $sort Sort array for Cursor::sort() |
||
| 484 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 485 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 486 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 487 | */ |
||
| 488 | 384 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Finds documents by a set of criteria. |
||
| 519 | * |
||
| 520 | * @param array $criteria Query criteria |
||
| 521 | * @param array $sort Sort array for Cursor::sort() |
||
| 522 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 523 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 524 | * @return Cursor |
||
| 525 | */ |
||
| 526 | 26 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * @param object $document |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | * @throws MongoDBException |
||
| 555 | */ |
||
| 556 | 307 | private function getShardKeyQuery($document) |
|
| 557 | { |
||
| 558 | 307 | if ( ! $this->class->isSharded()) { |
|
| 559 | 298 | return array(); |
|
| 560 | } |
||
| 561 | |||
| 562 | 9 | $shardKey = $this->class->getShardKey(); |
|
| 563 | 9 | $keys = array_keys($shardKey['keys']); |
|
| 564 | 9 | $data = $this->uow->getDocumentActualData($document); |
|
| 565 | |||
| 566 | 9 | $shardKeyQueryPart = array(); |
|
| 567 | 9 | foreach ($keys as $key) { |
|
| 568 | 9 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 569 | 9 | $this->guardMissingShardKey($document, $key, $data); |
|
| 570 | 7 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 571 | 7 | $shardKeyQueryPart[$key] = $value; |
|
| 572 | } |
||
| 573 | |||
| 574 | 7 | return $shardKeyQueryPart; |
|
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 579 | * |
||
| 580 | * @param CursorInterface $baseCursor |
||
| 581 | * @return Cursor |
||
| 582 | */ |
||
| 583 | 26 | private function wrapCursor(CursorInterface $baseCursor) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Checks whether the given managed document exists in the database. |
||
| 590 | * |
||
| 591 | * @param object $document |
||
| 592 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 593 | */ |
||
| 594 | 3 | public function exists($document) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 602 | * |
||
| 603 | * @param object $document |
||
| 604 | * @param int $lockMode |
||
| 605 | */ |
||
| 606 | 5 | public function lock($document, $lockMode) |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Releases any lock that exists on this document. |
||
| 617 | * |
||
| 618 | * @param object $document |
||
| 619 | */ |
||
| 620 | 1 | public function unlock($document) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Creates or fills a single document object from an query result. |
||
| 631 | * |
||
| 632 | * @param object $result The query result. |
||
| 633 | * @param object $document The document object to fill, if any. |
||
| 634 | * @param array $hints Hints for document creation. |
||
| 635 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 636 | */ |
||
| 637 | 383 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 654 | * |
||
| 655 | * @param PersistentCollectionInterface $collection |
||
| 656 | */ |
||
| 657 | 173 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 678 | |||
| 679 | 119 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 708 | |||
| 709 | 55 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 710 | { |
||
| 711 | 55 | $hints = $collection->getHints(); |
|
| 712 | 55 | $mapping = $collection->getMapping(); |
|
| 713 | 55 | $groupedIds = array(); |
|
| 714 | |||
| 715 | 55 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 716 | |||
| 717 | 55 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 718 | 50 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 719 | 50 | $mongoId = ClassMetadataInfo::getReferenceId($reference, $mapping['storeAs']); |
|
| 720 | 50 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId); |
|
| 721 | |||
| 722 | // create a reference to the class and id |
||
| 778 | |||
| 779 | 15 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @param PersistentCollectionInterface $collection |
||
| 790 | * |
||
| 791 | * @return Query |
||
| 792 | */ |
||
| 793 | 18 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 833 | |||
| 834 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * @param PersistentCollectionInterface $collection |
||
| 850 | * |
||
| 851 | * @return CursorInterface |
||
| 852 | */ |
||
| 853 | 6 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 897 | * property names, to MongoDB field names. |
||
| 898 | * |
||
| 899 | * @param array $fields |
||
| 900 | * @return array |
||
| 901 | */ |
||
| 902 | 140 | public function prepareSortOrProjection(array $fields) |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 915 | * |
||
| 916 | * @param string $fieldName |
||
| 917 | * @return string |
||
| 918 | */ |
||
| 919 | 89 | public function prepareFieldName($fieldName) |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Adds discriminator criteria to an already-prepared query. |
||
| 928 | * |
||
| 929 | * This method should be used once for query criteria and not be used for |
||
| 930 | * nested expressions. It should be called before |
||
| 931 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 932 | * |
||
| 933 | * @param array $preparedQuery |
||
| 934 | * @return array |
||
| 935 | */ |
||
| 936 | 521 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Adds filter criteria to an already-prepared query. |
||
| 955 | * |
||
| 956 | * This method should be used once for query criteria and not be used for |
||
| 957 | * nested expressions. It should be called after |
||
| 958 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 959 | * |
||
| 960 | * @param array $preparedQuery |
||
| 961 | * @return array |
||
| 962 | */ |
||
| 963 | 522 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 977 | |||
| 978 | /** |
||
| 979 | * Prepares the query criteria or new document object. |
||
| 980 | * |
||
| 981 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 982 | * |
||
| 983 | * @param array $query |
||
| 984 | * @param bool $isNewObj |
||
| 985 | * @return array |
||
| 986 | */ |
||
| 987 | 546 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Prepares a query value and converts the PHP value to the database value |
||
| 1018 | * if it is an identifier. |
||
| 1019 | * |
||
| 1020 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1021 | * |
||
| 1022 | * @param string $fieldName |
||
| 1023 | * @param mixed $value |
||
| 1024 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1025 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1026 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1027 | * @return array An array of tuples containing prepared field names and values |
||
| 1028 | */ |
||
| 1029 | 551 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Prepares a query expression. |
||
| 1219 | * |
||
| 1220 | * @param array|object $expression |
||
| 1221 | * @param ClassMetadata $class |
||
| 1222 | * @return array |
||
| 1223 | */ |
||
| 1224 | 75 | private function prepareQueryExpression($expression, $class) |
|
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Checks whether the value has DBRef fields. |
||
| 1254 | * |
||
| 1255 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1256 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1257 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1258 | * $elemMatch criteria for matching a DBRef. |
||
| 1259 | * |
||
| 1260 | * @param mixed $value |
||
| 1261 | * @return boolean |
||
| 1262 | */ |
||
| 1263 | 76 | private function hasDBRefFields($value) |
|
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Checks whether the value has query operators. |
||
| 1284 | * |
||
| 1285 | * @param mixed $value |
||
| 1286 | * @return boolean |
||
| 1287 | */ |
||
| 1288 | 80 | private function hasQueryOperators($value) |
|
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1309 | * |
||
| 1310 | * @param ClassMetadata $metadata |
||
| 1311 | * @return array |
||
| 1312 | */ |
||
| 1313 | 29 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1329 | |||
| 1330 | 627 | private function handleCollections($document, $options) |
|
| 1349 | |||
| 1350 | /** |
||
| 1351 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1352 | * Also, shard key field should be present in actual document data. |
||
| 1353 | * |
||
| 1354 | * @param object $document |
||
| 1355 | * @param string $shardKeyField |
||
| 1356 | * @param array $actualDocumentData |
||
| 1357 | * |
||
| 1358 | * @throws MongoDBException |
||
| 1359 | */ |
||
| 1360 | 9 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Get shard key aware query for single document. |
||
| 1379 | * |
||
| 1380 | * @param object $document |
||
| 1381 | * |
||
| 1382 | * @return array |
||
| 1383 | */ |
||
| 1384 | 304 | private function getQueryForDocument($document) |
|
| 1394 | |||
| 1395 | /** |
||
| 1396 | * @param array $options |
||
| 1397 | * |
||
| 1398 | * @return array |
||
| 1399 | */ |
||
| 1400 | 629 | private function getWriteOptions(array $options = array()) |
|
| 1410 | |||
| 1411 | /** |
||
| 1412 | * @param string $fieldName |
||
| 1413 | * @param mixed $value |
||
| 1414 | * @param array $mapping |
||
| 1415 | * @param bool $inNewObj |
||
| 1416 | * @return array |
||
| 1417 | */ |
||
| 1418 | 14 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1458 | } |
||
| 1459 |
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: