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 |
||
46 | class DocumentPersister |
||
47 | { |
||
48 | /** |
||
49 | * The PersistenceBuilder instance. |
||
50 | * |
||
51 | * @var PersistenceBuilder |
||
52 | */ |
||
53 | private $pb; |
||
54 | |||
55 | /** |
||
56 | * The DocumentManager instance. |
||
57 | * |
||
58 | * @var DocumentManager |
||
59 | */ |
||
60 | private $dm; |
||
61 | |||
62 | /** |
||
63 | * The EventManager instance |
||
64 | * |
||
65 | * @var EventManager |
||
66 | */ |
||
67 | private $evm; |
||
68 | |||
69 | /** |
||
70 | * The UnitOfWork instance. |
||
71 | * |
||
72 | * @var UnitOfWork |
||
73 | */ |
||
74 | private $uow; |
||
75 | |||
76 | /** |
||
77 | * The ClassMetadata instance for the document type being persisted. |
||
78 | * |
||
79 | * @var ClassMetadata |
||
80 | */ |
||
81 | private $class; |
||
82 | |||
83 | /** |
||
84 | * The MongoCollection instance for this document. |
||
85 | * |
||
86 | * @var \MongoCollection |
||
87 | */ |
||
88 | private $collection; |
||
89 | |||
90 | /** |
||
91 | * Array of queued inserts for the persister to insert. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | private $queuedInserts = array(); |
||
96 | |||
97 | /** |
||
98 | * Array of queued inserts for the persister to insert. |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | private $queuedUpserts = array(); |
||
103 | |||
104 | /** |
||
105 | * The CriteriaMerger instance. |
||
106 | * |
||
107 | * @var CriteriaMerger |
||
108 | */ |
||
109 | private $cm; |
||
110 | |||
111 | /** |
||
112 | * The CollectionPersister instance. |
||
113 | * |
||
114 | * @var CollectionPersister |
||
115 | */ |
||
116 | private $cp; |
||
117 | |||
118 | /** |
||
119 | * Initializes this instance. |
||
120 | * |
||
121 | * @param PersistenceBuilder $pb |
||
122 | * @param DocumentManager $dm |
||
123 | * @param EventManager $evm |
||
124 | * @param UnitOfWork $uow |
||
125 | * @param HydratorFactory $hydratorFactory |
||
126 | * @param ClassMetadata $class |
||
127 | * @param CriteriaMerger $cm |
||
128 | */ |
||
129 | 711 | public function __construct( |
|
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getInserts() |
||
156 | |||
157 | /** |
||
158 | * @param object $document |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function isQueuedForInsert($document) |
||
165 | |||
166 | /** |
||
167 | * Adds a document to the queued insertions. |
||
168 | * The document remains queued until {@link executeInserts} is invoked. |
||
169 | * |
||
170 | * @param object $document The document to queue for insertion. |
||
171 | */ |
||
172 | 507 | public function addInsert($document) |
|
176 | |||
177 | /** |
||
178 | * @return array |
||
179 | */ |
||
180 | public function getUpserts() |
||
184 | |||
185 | /** |
||
186 | * @param object $document |
||
187 | * @return boolean |
||
188 | */ |
||
189 | public function isQueuedForUpsert($document) |
||
193 | |||
194 | /** |
||
195 | * Adds a document to the queued upserts. |
||
196 | * The document remains queued until {@link executeUpserts} is invoked. |
||
197 | * |
||
198 | * @param object $document The document to queue for insertion. |
||
199 | */ |
||
200 | 77 | public function addUpsert($document) |
|
204 | |||
205 | /** |
||
206 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
207 | * |
||
208 | * @return ClassMetadata |
||
209 | */ |
||
210 | public function getClassMetadata() |
||
214 | |||
215 | /** |
||
216 | * Executes all queued document insertions. |
||
217 | * |
||
218 | * Queued documents without an ID will inserted in a batch and queued |
||
219 | * documents with an ID will be upserted individually. |
||
220 | * |
||
221 | * If no inserts are queued, invoking this method is a NOOP. |
||
222 | * |
||
223 | * @param array $options Options for batchInsert() and update() driver methods |
||
224 | */ |
||
225 | 507 | public function executeInserts(array $options = array()) |
|
226 | { |
||
227 | 507 | if ( ! $this->queuedInserts) { |
|
228 | return; |
||
229 | } |
||
230 | |||
231 | 507 | $inserts = array(); |
|
232 | 507 | $options = $this->getWriteOptions($options); |
|
233 | 507 | foreach ($this->queuedInserts as $oid => $document) { |
|
234 | $data = $this->pb->prepareInsertData($document); |
||
235 | |||
236 | 506 | // Set the initial version for each insert |
|
237 | 39 | View Code Duplication | if ($this->class->isVersioned) { |
238 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
239 | 37 | if ($versionMapping['type'] === 'int') { |
|
240 | 37 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
241 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
242 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
243 | 2 | $nextVersionDateTime = new \DateTime(); |
|
244 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
245 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
||
246 | 39 | } |
|
247 | $data[$versionMapping['name']] = $nextVersion; |
||
248 | } |
||
249 | 506 | ||
250 | $inserts[$oid] = $data; |
||
251 | } |
||
252 | 506 | ||
253 | if ($inserts) { |
||
254 | 506 | try { |
|
255 | 8 | $this->collection->batchInsert($inserts, $options); |
|
256 | 8 | } catch (\MongoException $e) { |
|
257 | 8 | $this->queuedInserts = array(); |
|
258 | throw $e; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /* All collections except for ones using addToSet have already been |
||
263 | * saved. We have left these to be handled separately to avoid checking |
||
264 | * collection for uniqueness on PHP side. |
||
265 | 506 | */ |
|
266 | 506 | foreach ($this->queuedInserts as $document) { |
|
267 | $this->handleCollections($document, $options); |
||
268 | } |
||
269 | 506 | ||
270 | 506 | $this->queuedInserts = array(); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Executes all queued document upserts. |
||
275 | * |
||
276 | * Queued documents with an ID are upserted individually. |
||
277 | * |
||
278 | * If no upserts are queued, invoking this method is a NOOP. |
||
279 | * |
||
280 | * @param array $options Options for batchInsert() and update() driver methods |
||
281 | 77 | */ |
|
282 | public function executeUpserts(array $options = array()) |
||
283 | 77 | { |
|
284 | if ( ! $this->queuedUpserts) { |
||
285 | return; |
||
286 | } |
||
287 | 77 | ||
288 | $options = $this->getWriteOptions($options); |
||
289 | 77 | foreach ($this->queuedUpserts as $oid => $document) { |
|
290 | 77 | try { |
|
291 | 77 | $this->executeUpsert($document, $options); |
|
292 | $this->handleCollections($document, $options); |
||
293 | unset($this->queuedUpserts[$oid]); |
||
294 | 77 | } catch (\MongoException $e) { |
|
295 | unset($this->queuedUpserts[$oid]); |
||
296 | throw $e; |
||
297 | 77 | } |
|
298 | } |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Executes a single upsert in {@link executeUpserts} |
||
303 | * |
||
304 | * @param object $document |
||
305 | 77 | * @param array $options |
|
306 | */ |
||
307 | 77 | private function executeUpsert($document, array $options) |
|
308 | 77 | { |
|
309 | $options['upsert'] = true; |
||
310 | 77 | $criteria = $this->getQueryForDocument($document); |
|
311 | |||
312 | $data = $this->pb->prepareUpsertData($document); |
||
313 | 77 | ||
314 | 3 | // Set the initial version for each upsert |
|
315 | 3 | View Code Duplication | if ($this->class->isVersioned) { |
316 | 2 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
317 | 2 | if ($versionMapping['type'] === 'int') { |
|
318 | 1 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
319 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
320 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
321 | 1 | $nextVersionDateTime = new \DateTime(); |
|
322 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
||
323 | 3 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
324 | } |
||
325 | $data['$set'][$versionMapping['name']] = $nextVersion; |
||
326 | 77 | } |
|
327 | 77 | ||
328 | foreach (array_keys($criteria) as $field) { |
||
329 | unset($data['$set'][$field]); |
||
330 | } |
||
331 | 77 | ||
332 | 13 | // Do not send an empty $set modifier |
|
333 | if (empty($data['$set'])) { |
||
334 | unset($data['$set']); |
||
335 | } |
||
336 | |||
337 | /* If there are no modifiers remaining, we're upserting a document with |
||
338 | * an identifier as its only field. Since a document with the identifier |
||
339 | * may already exist, the desired behavior is "insert if not exists" and |
||
340 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
341 | * the identifier to the same value in our criteria. |
||
342 | * |
||
343 | * This will fail for versions before MongoDB 2.6, which require an |
||
344 | * empty $set modifier. The best we can do (without attempting to check |
||
345 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
346 | * after the relevant exception. |
||
347 | * |
||
348 | 77 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
|
349 | 13 | */ |
|
350 | 13 | if (empty($data)) { |
|
351 | $retry = true; |
||
352 | $data = array('$set' => array('_id' => $criteria['_id'])); |
||
353 | } |
||
354 | 77 | ||
355 | 65 | try { |
|
356 | 13 | $this->collection->update($criteria, $data, $options); |
|
357 | 13 | return; |
|
358 | } catch (\MongoCursorException $e) { |
||
359 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
360 | throw $e; |
||
361 | } |
||
362 | 13 | } |
|
363 | 13 | ||
364 | $this->collection->update($criteria, array('$set' => new \stdClass), $options); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Updates the already persisted document if it has any new changesets. |
||
369 | * |
||
370 | * @param object $document |
||
371 | * @param array $options Array of options to be used with update() |
||
372 | 221 | * @throws \Doctrine\ODM\MongoDB\LockException |
|
373 | */ |
||
374 | 221 | public function update($document, array $options = array()) |
|
375 | { |
||
376 | 221 | $update = $this->pb->prepareUpdateData($document); |
|
377 | |||
378 | 219 | $query = $this->getQueryForDocument($document); |
|
379 | 219 | ||
380 | foreach (array_keys($query) as $field) { |
||
381 | unset($update['$set'][$field]); |
||
382 | 219 | } |
|
383 | 91 | ||
384 | if (empty($update['$set'])) { |
||
385 | unset($update['$set']); |
||
386 | } |
||
387 | |||
388 | |||
389 | // Include versioning logic to set the new version value in the database |
||
390 | 219 | // and to ensure the version has not changed since this document object instance |
|
391 | 31 | // was fetched from the database |
|
392 | 31 | if ($this->class->isVersioned) { |
|
393 | 31 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
394 | 28 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
395 | 28 | if ($versionMapping['type'] === 'int') { |
|
396 | 28 | $nextVersion = $currentVersion + 1; |
|
397 | 28 | $update['$inc'][$versionMapping['name']] = 1; |
|
398 | 3 | $query[$versionMapping['name']] = $currentVersion; |
|
399 | 3 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
400 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
401 | 3 | $nextVersion = new \DateTime(); |
|
402 | 3 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
|
403 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
||
404 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
||
405 | } |
||
406 | 219 | } |
|
407 | |||
408 | if ( ! empty($update)) { |
||
409 | 153 | // Include locking logic so that if the document object in memory is currently |
|
410 | 11 | // locked then it will remove it, otherwise it ensures the document is not locked. |
|
411 | 11 | if ($this->class->isLockable) { |
|
412 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
413 | 2 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
414 | if ($isLocked) { |
||
415 | 9 | $update['$unset'] = array($lockMapping['name'] => true); |
|
416 | } else { |
||
417 | $query[$lockMapping['name']] = array('$exists' => false); |
||
418 | } |
||
419 | 153 | } |
|
420 | |||
421 | 153 | $options = $this->getWriteOptions($options); |
|
422 | 5 | ||
423 | $result = $this->collection->update($query, $update, $options); |
||
424 | |||
425 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
|
426 | 215 | throw LockException::lockFailed($document); |
|
427 | 215 | } |
|
428 | } |
||
429 | |||
430 | $this->handleCollections($document, $options); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Removes document from mongo |
||
435 | * |
||
436 | 29 | * @param mixed $document |
|
437 | * @param array $options Array of options to be used with remove() |
||
438 | 29 | * @throws \Doctrine\ODM\MongoDB\LockException |
|
439 | */ |
||
440 | 29 | public function delete($document, array $options = array()) |
|
441 | 2 | { |
|
442 | $query = $this->getQueryForDocument($document); |
||
443 | |||
444 | 29 | if ($this->class->isLockable) { |
|
445 | $query[$this->class->lockField] = array('$exists' => false); |
||
446 | 29 | } |
|
447 | 2 | ||
448 | $options = $this->getWriteOptions($options); |
||
449 | 27 | ||
450 | $result = $this->collection->remove($query, $options); |
||
451 | |||
452 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
|
453 | throw LockException::lockFailed($document); |
||
454 | } |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Refreshes a managed document. |
||
459 | 21 | * |
|
460 | * @param string $id |
||
461 | 21 | * @param object $document The document to refresh. |
|
462 | 21 | * |
|
463 | 21 | * @deprecated The first argument is deprecated. |
|
464 | 21 | */ |
|
465 | 21 | public function refresh($id, $document) |
|
466 | { |
||
467 | $query = $this->getQueryForDocument($document); |
||
468 | $data = $this->collection->findOne($query); |
||
469 | $data = $this->hydratorFactory->hydrate($document, $data); |
||
470 | $this->uow->setOriginalDocumentData($document, $data); |
||
471 | } |
||
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 | 364 | * @param integer $lockMode |
|
483 | * @param array $sort Sort array for Cursor::sort() |
||
484 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
485 | 364 | * @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 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
||
489 | 364 | { |
|
490 | 364 | // TODO: remove this |
|
491 | 364 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) { |
|
492 | $criteria = array('_id' => $criteria); |
||
493 | 364 | } |
|
494 | |||
495 | 364 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
496 | 102 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
497 | $criteria = $this->addFilterToPreparedQuery($criteria); |
||
498 | |||
499 | 364 | $cursor = $this->collection->find($criteria); |
|
500 | |||
501 | 364 | if (null !== $sort) { |
|
502 | 1 | $cursor->sort($this->prepareSortOrProjection($sort)); |
|
503 | 1 | } |
|
504 | 1 | ||
505 | $result = $cursor->getSingleResult(); |
||
506 | |||
507 | if ($this->class->isLockable) { |
||
508 | 363 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
509 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
||
510 | throw LockException::lockFailed($result); |
||
511 | } |
||
512 | } |
||
513 | |||
514 | return $this->createDocument($result, $document, $hints); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Finds documents by a set of criteria. |
||
519 | * |
||
520 | 23 | * @param array $criteria Query criteria |
|
521 | * @param array $sort Sort array for Cursor::sort() |
||
522 | 23 | * @param integer|null $limit Limit for Cursor::limit() |
|
523 | 23 | * @param integer|null $skip Skip for Cursor::skip() |
|
524 | 23 | * @return Cursor |
|
525 | */ |
||
526 | 23 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
527 | 23 | { |
|
528 | $criteria = $this->prepareQueryOrNewObj($criteria); |
||
529 | 23 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
530 | 3 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
531 | |||
532 | $baseCursor = $this->collection->find($criteria); |
||
533 | 23 | $cursor = $this->wrapCursor($baseCursor); |
|
534 | 2 | ||
535 | if (null !== $sort) { |
||
536 | $cursor->sort($sort); |
||
537 | 23 | } |
|
538 | 2 | ||
539 | if (null !== $limit) { |
||
540 | $cursor->limit($limit); |
||
541 | 23 | } |
|
542 | |||
543 | if (null !== $skip) { |
||
544 | $cursor->skip($skip); |
||
545 | } |
||
546 | |||
547 | return $cursor; |
||
548 | } |
||
549 | |||
550 | 285 | /** |
|
551 | * @param object $document |
||
552 | 285 | * |
|
553 | 276 | * @return array |
|
554 | * @throws MongoDBException |
||
555 | */ |
||
556 | 9 | private function getShardKeyQuery($document) |
|
557 | 9 | { |
|
558 | 9 | if ( ! $this->class->isSharded()) { |
|
559 | return array(); |
||
560 | 9 | } |
|
561 | 9 | ||
562 | 9 | $shardKey = $this->class->getShardKey(); |
|
563 | 9 | $keys = array_keys($shardKey['keys']); |
|
564 | 7 | $data = $this->uow->getDocumentActualData($document); |
|
565 | 7 | ||
566 | $shardKeyQueryPart = array(); |
||
567 | foreach ($keys as $key) { |
||
568 | 7 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
569 | $this->guardMissingShardKey($document, $key, $data); |
||
570 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
||
571 | $shardKeyQueryPart[$key] = $value; |
||
572 | } |
||
573 | |||
574 | return $shardKeyQueryPart; |
||
575 | } |
||
576 | |||
577 | 23 | /** |
|
578 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
579 | 23 | * |
|
580 | * @param CursorInterface $baseCursor |
||
581 | * @return Cursor |
||
582 | */ |
||
583 | private function wrapCursor(CursorInterface $baseCursor) |
||
584 | { |
||
585 | return new Cursor($baseCursor, $this->dm->getUnitOfWork(), $this->class); |
||
586 | } |
||
587 | |||
588 | 3 | /** |
|
589 | * Checks whether the given managed document exists in the database. |
||
590 | 3 | * |
|
591 | 3 | * @param object $document |
|
592 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
593 | */ |
||
594 | public function exists($document) |
||
595 | { |
||
596 | $id = $this->class->getIdentifierObject($document); |
||
597 | return (boolean) $this->collection->findOne(array('_id' => $id), array('_id')); |
||
598 | } |
||
599 | |||
600 | 5 | /** |
|
601 | * Locks document by storing the lock mode on the mapped lock field. |
||
602 | 5 | * |
|
603 | 5 | * @param object $document |
|
604 | 5 | * @param int $lockMode |
|
605 | 5 | */ |
|
606 | 5 | public function lock($document, $lockMode) |
|
607 | 5 | { |
|
608 | $id = $this->uow->getDocumentIdentifier($document); |
||
609 | $criteria = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
||
610 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
||
611 | $this->collection->update($criteria, array('$set' => array($lockMapping['name'] => $lockMode))); |
||
612 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
||
613 | } |
||
614 | 1 | ||
615 | /** |
||
616 | 1 | * Releases any lock that exists on this document. |
|
617 | 1 | * |
|
618 | 1 | * @param object $document |
|
619 | 1 | */ |
|
620 | 1 | public function unlock($document) |
|
621 | 1 | { |
|
622 | $id = $this->uow->getDocumentIdentifier($document); |
||
623 | $criteria = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
||
624 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
||
625 | $this->collection->update($criteria, array('$unset' => array($lockMapping['name'] => true))); |
||
626 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * Creates or fills a single document object from an query result. |
||
631 | 363 | * |
|
632 | * @param object $result The query result. |
||
633 | 363 | * @param object $document The document object to fill, if any. |
|
634 | 116 | * @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 | 310 | private function createDocument($result, $document = null, array $hints = array()) |
|
638 | 37 | { |
|
639 | 37 | if ($result === null) { |
|
640 | 37 | return null; |
|
641 | } |
||
642 | |||
643 | 310 | if ($document !== null) { |
|
644 | $hints[Query::HINT_REFRESH] = true; |
||
645 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
||
646 | $this->uow->registerManaged($document, $id, $result); |
||
647 | } |
||
648 | |||
649 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
||
650 | } |
||
651 | 166 | ||
652 | /** |
||
653 | 166 | * Loads a PersistentCollection data. Used in the initialize() method. |
|
654 | 166 | * |
|
655 | 166 | * @param PersistentCollectionInterface $collection |
|
656 | 116 | */ |
|
657 | 116 | public function loadCollection(PersistentCollectionInterface $collection) |
|
658 | { |
||
659 | 67 | $mapping = $collection->getMapping(); |
|
660 | 67 | switch ($mapping['association']) { |
|
661 | 3 | case ClassMetadata::EMBED_MANY: |
|
662 | $this->loadEmbedManyCollection($collection); |
||
663 | 64 | break; |
|
664 | 54 | ||
665 | case ClassMetadata::REFERENCE_MANY: |
||
666 | 14 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
667 | $this->loadReferenceManyWithRepositoryMethod($collection); |
||
668 | } else { |
||
669 | 67 | if ($mapping['isOwningSide']) { |
|
670 | $this->loadReferenceManyCollectionOwningSide($collection); |
||
671 | 166 | } else { |
|
672 | $this->loadReferenceManyCollectionInverseSide($collection); |
||
673 | 116 | } |
|
674 | } |
||
675 | 116 | break; |
|
676 | 116 | } |
|
677 | 116 | } |
|
678 | 116 | ||
679 | 87 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
680 | 87 | { |
|
681 | 87 | $embeddedDocuments = $collection->getMongoData(); |
|
682 | 87 | $mapping = $collection->getMapping(); |
|
683 | $owner = $collection->getOwner(); |
||
684 | 87 | if ($embeddedDocuments) { |
|
685 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
||
686 | 87 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
687 | 87 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
688 | 21 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
689 | 87 | ||
690 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
||
691 | 87 | ||
692 | 86 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints()); |
|
693 | $id = $embeddedMetadata->identifier && isset($data[$embeddedMetadata->identifier]) |
||
694 | 87 | ? $data[$embeddedMetadata->identifier] |
|
695 | 25 | : null; |
|
696 | |||
697 | 87 | if (empty($collection->getHints()[Query::HINT_READ_ONLY])) { |
|
698 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
||
699 | } |
||
700 | if (CollectionHelper::isHash($mapping['strategy'])) { |
||
701 | 116 | $collection->set($key, $embeddedDocumentObject); |
|
702 | } else { |
||
703 | 54 | $collection->add($embeddedDocumentObject); |
|
704 | } |
||
705 | 54 | } |
|
706 | 54 | } |
|
707 | 54 | } |
|
708 | |||
709 | 54 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
781 | 13 | ||
782 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
||
783 | 14 | { |
|
784 | $query = $this->createReferenceManyInverseSideQuery($collection); |
||
785 | $documents = $query->execute()->toArray(false); |
||
786 | foreach ($documents as $key => $document) { |
||
787 | $collection->add($document); |
||
788 | } |
||
789 | } |
||
790 | 16 | ||
791 | /** |
||
792 | 16 | * @param PersistentCollectionInterface $collection |
|
793 | 16 | * |
|
794 | 16 | * @return Query |
|
795 | 16 | */ |
|
796 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
797 | 16 | { |
|
798 | 16 | $hints = $collection->getHints(); |
|
832 | 3 | ||
833 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
846 | 3 | ||
847 | /** |
||
848 | 3 | * @param PersistentCollectionInterface $collection |
|
849 | 3 | * |
|
850 | 3 | * @return CursorInterface |
|
851 | 3 | */ |
|
852 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
882 | |||
883 | /** |
||
884 | 139 | * Prepare a sort or projection array by converting keys, which are PHP |
|
885 | * property names, to MongoDB field names. |
||
886 | 139 | * |
|
887 | * @param array $fields |
||
888 | 139 | * @return array |
|
889 | 33 | */ |
|
890 | public function prepareSortOrProjection(array $fields) |
||
900 | |||
901 | 85 | /** |
|
902 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
903 | 85 | * |
|
904 | * @param string $fieldName |
||
905 | 85 | * @return string |
|
906 | */ |
||
907 | public function prepareFieldName($fieldName) |
||
913 | |||
914 | /** |
||
915 | * Adds discriminator criteria to an already-prepared query. |
||
916 | * |
||
917 | * This method should be used once for query criteria and not be used for |
||
918 | 495 | * nested expressions. It should be called before |
|
919 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
920 | * |
||
921 | * @param array $preparedQuery |
||
922 | * @return array |
||
923 | 495 | */ |
|
924 | 21 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
940 | |||
941 | /** |
||
942 | * Adds filter criteria to an already-prepared query. |
||
943 | * |
||
944 | * This method should be used once for query criteria and not be used for |
||
945 | 496 | * nested expressions. It should be called after |
|
946 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
947 | * |
||
948 | * @param array $preparedQuery |
||
949 | * @return array |
||
950 | */ |
||
951 | public function addFilterToPreparedQuery(array $preparedQuery) |
||
965 | |||
966 | /** |
||
967 | * Prepares the query criteria or new document object. |
||
968 | 529 | * |
|
969 | * PHP field names and types will be converted to those used by MongoDB. |
||
970 | 529 | * |
|
971 | * @param array $query |
||
972 | 529 | * @return array |
|
973 | */ |
||
974 | 490 | public function prepareQueryOrNewObj(array $query) |
|
1001 | |||
1002 | /** |
||
1003 | * Prepares a query value and converts the PHP value to the database value |
||
1004 | * if it is an identifier. |
||
1005 | * |
||
1006 | * It also handles converting $fieldName to the database name if they are different. |
||
1007 | * |
||
1008 | 521 | * @param string $fieldName |
|
1009 | * @param mixed $value |
||
1010 | 521 | * @param ClassMetadata $class Defaults to $this->class |
|
1011 | * @param boolean $prepareValue Whether or not to prepare the value |
||
1012 | * @return array Prepared field name and value |
||
1013 | */ |
||
1014 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
||
1197 | |||
1198 | /** |
||
1199 | 69 | * Prepares a query expression. |
|
1200 | * |
||
1201 | 69 | * @param array|object $expression |
|
1202 | * @param ClassMetadata $class |
||
1203 | 69 | * @return array |
|
1204 | 12 | */ |
|
1205 | private function prepareQueryExpression($expression, $class) |
||
1232 | |||
1233 | /** |
||
1234 | * Checks whether the value has DBRef fields. |
||
1235 | * |
||
1236 | * This method doesn't check if the the value is a complete DBRef object, |
||
1237 | * although it should return true for a DBRef. Rather, we're checking that |
||
1238 | 70 | * the value has one or more fields for a DBref. In practice, this could be |
|
1239 | * $elemMatch criteria for matching a DBRef. |
||
1240 | 70 | * |
|
1241 | * @param mixed $value |
||
1242 | * @return boolean |
||
1243 | */ |
||
1244 | 70 | private function hasDBRefFields($value) |
|
1262 | |||
1263 | 74 | /** |
|
1264 | * Checks whether the value has query operators. |
||
1265 | 74 | * |
|
1266 | * @param mixed $value |
||
1267 | * @return boolean |
||
1268 | */ |
||
1269 | 74 | private function hasQueryOperators($value) |
|
1287 | |||
1288 | 21 | /** |
|
1289 | * Gets the array of discriminator values for the given ClassMetadata |
||
1290 | 21 | * |
|
1291 | 21 | * @param ClassMetadata $metadata |
|
1292 | 8 | * @return array |
|
1293 | 8 | */ |
|
1294 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
||
1310 | 103 | ||
1311 | private function handleCollections($document, $options) |
||
1330 | |||
1331 | /** |
||
1332 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
1333 | * Also, shard key field should be present in actual document data. |
||
1334 | * |
||
1335 | 9 | * @param object $document |
|
1336 | * @param string $shardKeyField |
||
1337 | 9 | * @param array $actualDocumentData |
|
1338 | 9 | * |
|
1339 | * @throws MongoDBException |
||
1340 | 9 | */ |
|
1341 | 9 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
1357 | |||
1358 | /** |
||
1359 | 282 | * Get shard key aware query for single document. |
|
1360 | * |
||
1361 | 282 | * @param object $document |
|
1362 | 282 | * |
|
1363 | * @return array |
||
1364 | 282 | */ |
|
1365 | 280 | private function getQueryForDocument($document) |
|
1375 | |||
1376 | /** |
||
1377 | * @param array $options |
||
1378 | * |
||
1379 | * @return array |
||
1380 | */ |
||
1381 | private function getWriteOptions(array $options = array()) |
||
1391 | } |
||
1392 |
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: