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 |
||
| 35 | class DocumentPersister |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The PersistenceBuilder instance. |
||
| 39 | * |
||
| 40 | * @var PersistenceBuilder |
||
| 41 | */ |
||
| 42 | private $pb; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The DocumentManager instance. |
||
| 46 | * |
||
| 47 | * @var DocumentManager |
||
| 48 | */ |
||
| 49 | private $dm; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The EventManager instance |
||
| 53 | * |
||
| 54 | * @var EventManager |
||
| 55 | */ |
||
| 56 | private $evm; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The UnitOfWork instance. |
||
| 60 | * |
||
| 61 | * @var UnitOfWork |
||
| 62 | */ |
||
| 63 | private $uow; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The ClassMetadata instance for the document type being persisted. |
||
| 67 | * |
||
| 68 | * @var ClassMetadata |
||
| 69 | */ |
||
| 70 | private $class; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The MongoCollection instance for this document. |
||
| 74 | * |
||
| 75 | * @var Collection |
||
| 76 | */ |
||
| 77 | private $collection; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Array of queued inserts for the persister to insert. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $queuedInserts = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Array of queued inserts for the persister to insert. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $queuedUpserts = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The CriteriaMerger instance. |
||
| 95 | * |
||
| 96 | * @var CriteriaMerger |
||
| 97 | */ |
||
| 98 | private $cm; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The CollectionPersister instance. |
||
| 102 | * |
||
| 103 | * @var CollectionPersister |
||
| 104 | */ |
||
| 105 | private $cp; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initializes this instance. |
||
| 109 | * |
||
| 110 | * @param PersistenceBuilder $pb |
||
| 111 | * @param DocumentManager $dm |
||
| 112 | * @param EventManager $evm |
||
| 113 | * @param UnitOfWork $uow |
||
| 114 | * @param HydratorFactory $hydratorFactory |
||
| 115 | * @param ClassMetadata $class |
||
| 116 | * @param CriteriaMerger $cm |
||
| 117 | */ |
||
| 118 | 1084 | public function __construct( |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getInserts() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param object $document |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isQueuedForInsert($document) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Adds a document to the queued insertions. |
||
| 157 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 158 | * |
||
| 159 | * @param object $document The document to queue for insertion. |
||
| 160 | */ |
||
| 161 | 485 | public function addInsert($document) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function getUpserts() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param object $document |
||
| 176 | * @return boolean |
||
| 177 | */ |
||
| 178 | public function isQueuedForUpsert($document) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds a document to the queued upserts. |
||
| 185 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 186 | * |
||
| 187 | * @param object $document The document to queue for insertion. |
||
| 188 | */ |
||
| 189 | 85 | public function addUpsert($document) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 196 | * |
||
| 197 | * @return ClassMetadata |
||
| 198 | */ |
||
| 199 | public function getClassMetadata() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Executes all queued document insertions. |
||
| 206 | * |
||
| 207 | * Queued documents without an ID will inserted in a batch and queued |
||
| 208 | * documents with an ID will be upserted individually. |
||
| 209 | * |
||
| 210 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 211 | * |
||
| 212 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 213 | */ |
||
| 214 | 485 | public function executeInserts(array $options = array()) |
|
| 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 | 85 | public function executeUpserts(array $options = array()) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Executes a single upsert in {@link executeUpserts} |
||
| 292 | * |
||
| 293 | * @param object $document |
||
| 294 | * @param array $options |
||
| 295 | */ |
||
| 296 | 85 | private function executeUpsert($document, array $options) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Updates the already persisted document if it has any new changesets. |
||
| 358 | * |
||
| 359 | * @param object $document |
||
| 360 | * @param array $options Array of options to be used with update() |
||
| 361 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 362 | */ |
||
| 363 | 199 | public function update($document, array $options = array()) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Removes document from mongo |
||
| 425 | * |
||
| 426 | * @param mixed $document |
||
| 427 | * @param array $options Array of options to be used with remove() |
||
| 428 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 429 | */ |
||
| 430 | 33 | public function delete($document, array $options = array()) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Refreshes a managed document. |
||
| 449 | * |
||
| 450 | * @param object $document The document to refresh. |
||
| 451 | */ |
||
| 452 | 20 | public function refresh($document) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Finds a document by a set of criteria. |
||
| 462 | * |
||
| 463 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 464 | * be used to match an _id value. |
||
| 465 | * |
||
| 466 | * @param mixed $criteria Query criteria |
||
| 467 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 468 | * @param array $hints Hints for document creation |
||
| 469 | * @param integer $lockMode |
||
| 470 | * @param array $sort Sort array for Cursor::sort() |
||
| 471 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 472 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 473 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 474 | */ |
||
| 475 | 348 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Finds documents by a set of criteria. |
||
| 504 | * |
||
| 505 | * @param array $criteria Query criteria |
||
| 506 | * @param array $sort Sort array for Cursor::sort() |
||
| 507 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 508 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 509 | * @return Iterator |
||
| 510 | */ |
||
| 511 | 22 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @param object $document |
||
| 538 | * |
||
| 539 | * @return array |
||
| 540 | * @throws MongoDBException |
||
| 541 | */ |
||
| 542 | 274 | private function getShardKeyQuery($document) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 578 | * |
||
| 579 | * @param Cursor $baseCursor |
||
| 580 | * @return Iterator |
||
| 581 | */ |
||
| 582 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Checks whether the given managed document exists in the database. |
||
| 589 | * |
||
| 590 | * @param object $document |
||
| 591 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 592 | */ |
||
| 593 | 3 | public function exists($document) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 601 | * |
||
| 602 | * @param object $document |
||
| 603 | * @param int $lockMode |
||
| 604 | */ |
||
| 605 | 5 | public function lock($document, $lockMode) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Releases any lock that exists on this document. |
||
| 616 | * |
||
| 617 | * @param object $document |
||
| 618 | */ |
||
| 619 | 1 | public function unlock($document) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Creates or fills a single document object from an query result. |
||
| 630 | * |
||
| 631 | * @param object $result The query result. |
||
| 632 | * @param object $document The document object to fill, if any. |
||
| 633 | * @param array $hints Hints for document creation. |
||
| 634 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 635 | */ |
||
| 636 | 347 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 653 | * |
||
| 654 | * @param PersistentCollectionInterface $collection |
||
| 655 | */ |
||
| 656 | 163 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 657 | { |
||
| 658 | 163 | $mapping = $collection->getMapping(); |
|
| 659 | 163 | switch ($mapping['association']) { |
|
| 660 | case ClassMetadata::EMBED_MANY: |
||
| 661 | 109 | $this->loadEmbedManyCollection($collection); |
|
| 662 | 109 | break; |
|
| 663 | |||
| 664 | case ClassMetadata::REFERENCE_MANY: |
||
| 665 | 76 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 666 | 5 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 667 | } else { |
||
| 668 | 72 | if ($mapping['isOwningSide']) { |
|
| 669 | 60 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 670 | } else { |
||
| 671 | 17 | $this->loadReferenceManyCollectionInverseSide($collection); |
|
| 672 | } |
||
| 673 | } |
||
| 674 | 76 | break; |
|
| 675 | } |
||
| 676 | 163 | } |
|
| 677 | |||
| 678 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 707 | |||
| 708 | 60 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 709 | { |
||
| 710 | 60 | $hints = $collection->getHints(); |
|
| 711 | 60 | $mapping = $collection->getMapping(); |
|
| 712 | 60 | $groupedIds = array(); |
|
| 713 | |||
| 714 | 60 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 715 | |||
| 716 | 60 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 717 | 54 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 718 | 54 | $identifier = ClassMetadataInfo::getReferenceId($reference, $mapping['storeAs']); |
|
| 719 | 54 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier); |
|
| 720 | |||
| 721 | // create a reference to the class and id |
||
| 722 | 54 | $reference = $this->dm->getReference($className, $id); |
|
| 723 | |||
| 724 | // no custom sort so add the references right now in the order they are embedded |
||
| 725 | 54 | if ( ! $sorted) { |
|
| 726 | 53 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 727 | 2 | $collection->set($key, $reference); |
|
| 728 | } else { |
||
| 729 | 51 | $collection->add($reference); |
|
| 730 | } |
||
| 731 | } |
||
| 732 | |||
| 733 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 734 | 54 | if (($reference instanceof Proxy && ! $reference->__isInitialized__) || $sorted) { |
|
| 735 | 54 | $groupedIds[$className][] = $identifier; |
|
| 736 | } |
||
| 737 | } |
||
| 738 | 60 | foreach ($groupedIds as $className => $ids) { |
|
| 739 | 39 | $class = $this->dm->getClassMetadata($className); |
|
| 740 | 39 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 741 | 39 | $criteria = $this->cm->merge( |
|
| 742 | 39 | array('_id' => array('$in' => array_values($ids))), |
|
| 743 | 39 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 744 | 39 | $mapping['criteria'] ?? array() |
|
| 745 | ); |
||
| 746 | 39 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 747 | |||
| 748 | 39 | $options = []; |
|
| 749 | 39 | if (isset($mapping['sort'])) { |
|
| 750 | 39 | $options['sort'] = $this->prepareSort($mapping['sort']); |
|
| 751 | } |
||
| 752 | 39 | if (isset($mapping['limit'])) { |
|
| 753 | $options['limit'] = $mapping['limit']; |
||
| 754 | } |
||
| 755 | 39 | if (isset($mapping['skip'])) { |
|
| 756 | $options['skip'] = $mapping['skip']; |
||
| 757 | } |
||
| 758 | 39 | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 759 | $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE]; |
||
| 760 | } |
||
| 761 | |||
| 762 | 39 | $cursor = $mongoCollection->find($criteria, $options); |
|
| 763 | 39 | $documents = $cursor->toArray(); |
|
| 764 | 39 | foreach ($documents as $documentData) { |
|
| 765 | 38 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 766 | 38 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 767 | 38 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 768 | 38 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 769 | 38 | $document->__isInitialized__ = true; |
|
| 770 | } |
||
| 771 | 38 | if ($sorted) { |
|
| 772 | 39 | $collection->add($document); |
|
| 773 | } |
||
| 774 | } |
||
| 775 | } |
||
| 776 | 60 | } |
|
| 777 | |||
| 778 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * @param PersistentCollectionInterface $collection |
||
| 789 | * |
||
| 790 | * @return Query |
||
| 791 | */ |
||
| 792 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 793 | { |
||
| 794 | 17 | $hints = $collection->getHints(); |
|
| 795 | 17 | $mapping = $collection->getMapping(); |
|
| 796 | 17 | $owner = $collection->getOwner(); |
|
| 797 | 17 | $ownerClass = $this->dm->getClassMetadata(get_class($owner)); |
|
| 798 | 17 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 799 | 17 | $mappedByMapping = isset($targetClass->fieldMappings[$mapping['mappedBy']]) ? $targetClass->fieldMappings[$mapping['mappedBy']] : array(); |
|
| 800 | 17 | $mappedByFieldName = ClassMetadataInfo::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']); |
|
| 801 | |||
| 802 | 17 | $criteria = $this->cm->merge( |
|
| 803 | 17 | array($mappedByFieldName => $ownerClass->getIdentifierObject($owner)), |
|
| 804 | 17 | $this->dm->getFilterCollection()->getFilterCriteria($targetClass), |
|
| 805 | 17 | $mapping['criteria'] ?? array() |
|
| 806 | ); |
||
| 807 | 17 | $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); |
|
| 808 | 17 | $qb = $this->dm->createQueryBuilder($mapping['targetDocument']) |
|
| 809 | 17 | ->setQueryArray($criteria); |
|
| 810 | |||
| 811 | 17 | if (isset($mapping['sort'])) { |
|
| 812 | 17 | $qb->sort($mapping['sort']); |
|
| 813 | } |
||
| 814 | 17 | if (isset($mapping['limit'])) { |
|
| 815 | 2 | $qb->limit($mapping['limit']); |
|
| 816 | } |
||
| 817 | 17 | if (isset($mapping['skip'])) { |
|
| 818 | $qb->skip($mapping['skip']); |
||
| 819 | } |
||
| 820 | |||
| 821 | 17 | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 822 | $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE]); |
||
| 823 | } |
||
| 824 | |||
| 825 | 17 | foreach ($mapping['prime'] as $field) { |
|
| 826 | 4 | $qb->field($field)->prime(true); |
|
| 827 | } |
||
| 828 | |||
| 829 | 17 | return $qb->getQuery(); |
|
| 830 | } |
||
| 831 | |||
| 832 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 845 | |||
| 846 | /** |
||
| 847 | * @param PersistentCollectionInterface $collection |
||
| 848 | * |
||
| 849 | * @return \Iterator |
||
| 850 | */ |
||
| 851 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Prepare a projection array by converting keys, which are PHP property |
||
| 875 | * names, to MongoDB field names. |
||
| 876 | * |
||
| 877 | * @param array $fields |
||
| 878 | * @return array |
||
| 879 | */ |
||
| 880 | 14 | public function prepareProjection(array $fields) |
|
| 890 | |||
| 891 | /** |
||
| 892 | * @param $sort |
||
| 893 | * @return int |
||
| 894 | */ |
||
| 895 | 25 | private function getSortDirection($sort) |
|
| 896 | { |
||
| 897 | 25 | switch (strtolower($sort)) { |
|
| 898 | case 'desc': |
||
| 899 | 15 | return -1; |
|
| 900 | |||
| 901 | case 'asc': |
||
| 902 | 13 | return 1; |
|
| 903 | } |
||
| 904 | |||
| 905 | 12 | return $sort; |
|
| 906 | } |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 910 | * names and changing direction strings to int. |
||
| 911 | * |
||
| 912 | * @param array $fields |
||
| 913 | * @return array |
||
| 914 | */ |
||
| 915 | 141 | public function prepareSort(array $fields) |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 928 | * |
||
| 929 | * @param string $fieldName |
||
| 930 | * @return string |
||
| 931 | */ |
||
| 932 | 433 | public function prepareFieldName($fieldName) |
|
| 938 | |||
| 939 | /** |
||
| 940 | * Adds discriminator criteria to an already-prepared query. |
||
| 941 | * |
||
| 942 | * This method should be used once for query criteria and not be used for |
||
| 943 | * nested expressions. It should be called before |
||
| 944 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 945 | * |
||
| 946 | * @param array $preparedQuery |
||
| 947 | * @return array |
||
| 948 | */ |
||
| 949 | 498 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 965 | |||
| 966 | /** |
||
| 967 | * Adds filter criteria to an already-prepared query. |
||
| 968 | * |
||
| 969 | * This method should be used once for query criteria and not be used for |
||
| 970 | * nested expressions. It should be called after |
||
| 971 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 972 | * |
||
| 973 | * @param array $preparedQuery |
||
| 974 | * @return array |
||
| 975 | */ |
||
| 976 | 499 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Prepares the query criteria or new document object. |
||
| 993 | * |
||
| 994 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 995 | * |
||
| 996 | * @param array $query |
||
| 997 | * @param bool $isNewObj |
||
| 998 | * @return array |
||
| 999 | */ |
||
| 1000 | 531 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Prepares a query value and converts the PHP value to the database value |
||
| 1031 | * if it is an identifier. |
||
| 1032 | * |
||
| 1033 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1034 | * |
||
| 1035 | * @param string $fieldName |
||
| 1036 | * @param mixed $value |
||
| 1037 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1038 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1039 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1040 | * @return array An array of tuples containing prepared field names and values |
||
| 1041 | */ |
||
| 1042 | 882 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1043 | { |
||
| 1044 | 882 | $class = $class ?? $this->class; |
|
| 1045 | |||
| 1046 | // @todo Consider inlining calls to ClassMetadataInfo methods |
||
| 1047 | |||
| 1048 | // Process all non-identifier fields by translating field names |
||
| 1049 | 882 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 1050 | 248 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 1051 | 248 | $fieldName = $mapping['name']; |
|
| 1052 | |||
| 1053 | 248 | if ( ! $prepareValue) { |
|
| 1054 | 52 | return [[$fieldName, $value]]; |
|
| 1055 | } |
||
| 1056 | |||
| 1057 | // Prepare mapped, embedded objects |
||
| 1058 | 206 | if ( ! empty($mapping['embedded']) && is_object($value) && |
|
| 1059 | 206 | ! $this->dm->getMetadataFactory()->isTransient(get_class($value))) { |
|
| 1060 | 3 | return [[$fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)]]; |
|
| 1061 | } |
||
| 1062 | |||
| 1063 | 204 | if (! empty($mapping['reference']) && is_object($value) && ! ($value instanceof \MongoDB\BSON\ObjectId)) { |
|
| 1064 | try { |
||
| 1065 | 14 | return $this->prepareReference($fieldName, $value, $mapping, $inNewObj); |
|
| 1066 | 1 | } catch (MappingException $e) { |
|
| 1067 | // do nothing in case passed object is not mapped document |
||
| 1068 | } |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | // No further preparation unless we're dealing with a simple reference |
||
| 1072 | // We can't have expressions in empty() with PHP < 5.5, so store it in a variable |
||
| 1073 | 191 | $arrayValue = (array) $value; |
|
| 1074 | 191 | if (empty($mapping['reference']) || $mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID || empty($arrayValue)) { |
|
| 1075 | 127 | return [[$fieldName, $value]]; |
|
| 1076 | } |
||
| 1077 | |||
| 1078 | // Additional preparation for one or more simple reference values |
||
| 1079 | 91 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1080 | |||
| 1081 | 91 | if ( ! is_array($value)) { |
|
| 1082 | 87 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1083 | } |
||
| 1084 | |||
| 1085 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1086 | 6 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1087 | 3 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1088 | } |
||
| 1089 | |||
| 1090 | 6 | return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]]; |
|
| 1091 | } |
||
| 1092 | |||
| 1093 | // Process identifier fields |
||
| 1094 | 794 | if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') { |
|
| 1095 | 339 | $fieldName = '_id'; |
|
| 1096 | |||
| 1097 | 339 | if ( ! $prepareValue) { |
|
| 1098 | 42 | return [[$fieldName, $value]]; |
|
| 1099 | } |
||
| 1100 | |||
| 1101 | 300 | if ( ! is_array($value)) { |
|
| 1102 | 277 | return [[$fieldName, $class->getDatabaseIdentifierValue($value)]]; |
|
| 1103 | } |
||
| 1104 | |||
| 1105 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1106 | 61 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1107 | 6 | return [[$fieldName, $class->getDatabaseIdentifierValue($value)]]; |
|
| 1108 | } |
||
| 1109 | |||
| 1110 | 56 | return [[$fieldName, $this->prepareQueryExpression($value, $class)]]; |
|
| 1111 | } |
||
| 1112 | |||
| 1113 | // No processing for unmapped, non-identifier, non-dotted field names |
||
| 1114 | 553 | if (strpos($fieldName, '.') === false) { |
|
| 1115 | 414 | return [[$fieldName, $value]]; |
|
| 1116 | } |
||
| 1117 | |||
| 1118 | /* Process "fieldName.objectProperty" queries (on arrays or objects). |
||
| 1119 | * |
||
| 1120 | * We can limit parsing here, since at most three segments are |
||
| 1121 | * significant: "fieldName.objectProperty" with an optional index or key |
||
| 1122 | * for collections stored as either BSON arrays or objects. |
||
| 1123 | */ |
||
| 1124 | 152 | $e = explode('.', $fieldName, 4); |
|
| 1125 | |||
| 1126 | // No further processing for unmapped fields |
||
| 1127 | 152 | if ( ! isset($class->fieldMappings[$e[0]])) { |
|
| 1128 | 6 | return [[$fieldName, $value]]; |
|
| 1129 | } |
||
| 1130 | |||
| 1131 | 147 | $mapping = $class->fieldMappings[$e[0]]; |
|
| 1132 | 147 | $e[0] = $mapping['name']; |
|
| 1133 | |||
| 1134 | // Hash and raw fields will not be prepared beyond the field name |
||
| 1135 | 147 | if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) { |
|
| 1136 | 1 | $fieldName = implode('.', $e); |
|
| 1137 | |||
| 1138 | 1 | return [[$fieldName, $value]]; |
|
| 1139 | } |
||
| 1140 | |||
| 1141 | 146 | if ($mapping['type'] == 'many' && CollectionHelper::isHash($mapping['strategy']) |
|
| 1142 | 146 | && isset($e[2])) { |
|
| 1143 | 1 | $objectProperty = $e[2]; |
|
| 1144 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1145 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1146 | 145 | } elseif ($e[1] != '$') { |
|
| 1147 | 144 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1148 | 144 | $objectProperty = $e[1]; |
|
| 1149 | 144 | $objectPropertyPrefix = ''; |
|
| 1150 | 144 | $nextObjectProperty = implode('.', array_slice($e, 2)); |
|
| 1151 | 1 | } elseif (isset($e[2])) { |
|
| 1152 | 1 | $fieldName = $e[0] . '.' . $e[1] . '.' . $e[2]; |
|
| 1153 | 1 | $objectProperty = $e[2]; |
|
| 1154 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1155 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1156 | } else { |
||
| 1157 | 1 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1158 | |||
| 1159 | 1 | return [[$fieldName, $value]]; |
|
| 1160 | } |
||
| 1161 | |||
| 1162 | // No further processing for fields without a targetDocument mapping |
||
| 1163 | 146 | if ( ! isset($mapping['targetDocument'])) { |
|
| 1164 | 3 | if ($nextObjectProperty) { |
|
| 1165 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1166 | } |
||
| 1167 | |||
| 1168 | 3 | return [[$fieldName, $value]]; |
|
| 1169 | } |
||
| 1170 | |||
| 1171 | 143 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1172 | |||
| 1173 | // No further processing for unmapped targetDocument fields |
||
| 1174 | 143 | if ( ! $targetClass->hasField($objectProperty)) { |
|
| 1175 | 25 | if ($nextObjectProperty) { |
|
| 1176 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | 25 | return [[$fieldName, $value]]; |
|
| 1180 | } |
||
| 1181 | |||
| 1182 | 123 | $targetMapping = $targetClass->getFieldMapping($objectProperty); |
|
| 1183 | 123 | $objectPropertyIsId = $targetClass->isIdentifier($objectProperty); |
|
| 1184 | |||
| 1185 | // Prepare DBRef identifiers or the mapped field's property path |
||
| 1186 | 123 | $fieldName = ($objectPropertyIsId && ! empty($mapping['reference']) && $mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) |
|
| 1187 | 105 | ? ClassMetadataInfo::getReferenceFieldName($mapping['storeAs'], $e[0]) |
|
| 1188 | 123 | : $e[0] . '.' . $objectPropertyPrefix . $targetMapping['name']; |
|
| 1189 | |||
| 1190 | // Process targetDocument identifier fields |
||
| 1191 | 123 | if ($objectPropertyIsId) { |
|
| 1192 | 106 | if ( ! $prepareValue) { |
|
| 1193 | 7 | return [[$fieldName, $value]]; |
|
| 1194 | } |
||
| 1195 | |||
| 1196 | 99 | if ( ! is_array($value)) { |
|
| 1197 | 85 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1198 | } |
||
| 1199 | |||
| 1200 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1201 | 16 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1202 | 6 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1203 | } |
||
| 1204 | |||
| 1205 | 16 | return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]]; |
|
| 1206 | } |
||
| 1207 | |||
| 1208 | /* The property path may include a third field segment, excluding the |
||
| 1209 | * collection item pointer. If present, this next object property must |
||
| 1210 | * be processed recursively. |
||
| 1211 | */ |
||
| 1212 | 17 | if ($nextObjectProperty) { |
|
| 1213 | // Respect the targetDocument's class metadata when recursing |
||
| 1214 | 14 | $nextTargetClass = isset($targetMapping['targetDocument']) |
|
| 1215 | 8 | ? $this->dm->getClassMetadata($targetMapping['targetDocument']) |
|
| 1216 | 14 | : null; |
|
| 1217 | |||
| 1218 | 14 | $fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue); |
|
| 1219 | |||
| 1220 | 14 | return array_map(function ($preparedTuple) use ($fieldName) { |
|
| 1221 | 14 | list($key, $value) = $preparedTuple; |
|
| 1222 | |||
| 1223 | 14 | return [$fieldName . '.' . $key, $value]; |
|
| 1224 | 14 | }, $fieldNames); |
|
| 1225 | } |
||
| 1226 | |||
| 1227 | 5 | return [[$fieldName, $value]]; |
|
| 1228 | } |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Prepares a query expression. |
||
| 1232 | * |
||
| 1233 | * @param array|object $expression |
||
| 1234 | * @param ClassMetadata $class |
||
| 1235 | * @return array |
||
| 1236 | */ |
||
| 1237 | 78 | private function prepareQueryExpression($expression, $class) |
|
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Checks whether the value has DBRef fields. |
||
| 1267 | * |
||
| 1268 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1269 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1270 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1271 | * $elemMatch criteria for matching a DBRef. |
||
| 1272 | * |
||
| 1273 | * @param mixed $value |
||
| 1274 | * @return boolean |
||
| 1275 | */ |
||
| 1276 | 79 | private function hasDBRefFields($value) |
|
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Checks whether the value has query operators. |
||
| 1297 | * |
||
| 1298 | * @param mixed $value |
||
| 1299 | * @return boolean |
||
| 1300 | */ |
||
| 1301 | 83 | private function hasQueryOperators($value) |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1322 | * |
||
| 1323 | * @param ClassMetadata $metadata |
||
| 1324 | * @return array |
||
| 1325 | */ |
||
| 1326 | 29 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1342 | |||
| 1343 | 557 | private function handleCollections($document, $options) |
|
| 1362 | |||
| 1363 | /** |
||
| 1364 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1365 | * Also, shard key field should be present in actual document data. |
||
| 1366 | * |
||
| 1367 | * @param object $document |
||
| 1368 | * @param string $shardKeyField |
||
| 1369 | * @param array $actualDocumentData |
||
| 1370 | * |
||
| 1371 | * @throws MongoDBException |
||
| 1372 | */ |
||
| 1373 | 4 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Get shard key aware query for single document. |
||
| 1392 | * |
||
| 1393 | * @param object $document |
||
| 1394 | * |
||
| 1395 | * @return array |
||
| 1396 | */ |
||
| 1397 | 270 | private function getQueryForDocument($document) |
|
| 1407 | |||
| 1408 | /** |
||
| 1409 | * @param array $options |
||
| 1410 | * |
||
| 1411 | * @return array |
||
| 1412 | */ |
||
| 1413 | 558 | private function getWriteOptions(array $options = array()) |
|
| 1423 | |||
| 1424 | /** |
||
| 1425 | * @param string $fieldName |
||
| 1426 | * @param mixed $value |
||
| 1427 | * @param array $mapping |
||
| 1428 | * @param bool $inNewObj |
||
| 1429 | * @return array |
||
| 1430 | */ |
||
| 1431 | 15 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1471 | } |
||
| 1472 |
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: