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 | 730 | public function __construct( |
|
| 130 | PersistenceBuilder $pb, |
||
| 131 | DocumentManager $dm, |
||
| 132 | EventManager $evm, |
||
| 133 | UnitOfWork $uow, |
||
| 134 | HydratorFactory $hydratorFactory, |
||
| 135 | ClassMetadata $class, |
||
| 136 | CriteriaMerger $cm = null |
||
| 137 | ) { |
||
| 138 | 730 | $this->pb = $pb; |
|
| 139 | 730 | $this->dm = $dm; |
|
| 140 | 730 | $this->evm = $evm; |
|
| 141 | 730 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 142 | 730 | $this->uow = $uow; |
|
| 143 | 730 | $this->hydratorFactory = $hydratorFactory; |
|
|
|
|||
| 144 | 730 | $this->class = $class; |
|
| 145 | 730 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 146 | 730 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 147 | 730 | } |
|
| 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 | 518 | 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 | 85 | 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 | 518 | public function executeInserts(array $options = array()) |
|
| 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 | */ |
||
| 282 | 85 | public function executeUpserts(array $options = array()) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Executes a single upsert in {@link executeUpserts} |
||
| 303 | * |
||
| 304 | * @param object $document |
||
| 305 | * @param array $options |
||
| 306 | */ |
||
| 307 | 85 | private function executeUpsert($document, array $options) |
|
| 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 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 373 | */ |
||
| 374 | 223 | public function update($document, array $options = array()) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Removes document from mongo |
||
| 435 | * |
||
| 436 | * @param mixed $document |
||
| 437 | * @param array $options Array of options to be used with remove() |
||
| 438 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 439 | */ |
||
| 440 | 33 | public function delete($document, array $options = array()) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Refreshes a managed document. |
||
| 459 | * |
||
| 460 | * @param string $id |
||
| 461 | * @param object $document The document to refresh. |
||
| 462 | * |
||
| 463 | * @deprecated The first argument is deprecated. |
||
| 464 | */ |
||
| 465 | 21 | public function refresh($id, $document) |
|
| 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 | 369 | 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 | 22 | 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 | 298 | private function getShardKeyQuery($document) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 579 | * |
||
| 580 | * @param CursorInterface $baseCursor |
||
| 581 | * @return Cursor |
||
| 582 | */ |
||
| 583 | 22 | 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 | 368 | 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 | 168 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 678 | |||
| 679 | 118 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 708 | |||
| 709 | 54 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 783 | |||
| 784 | 14 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 785 | { |
||
| 786 | 14 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 787 | 14 | $documents = $query->execute()->toArray(false); |
|
| 788 | 14 | foreach ($documents as $key => $document) { |
|
| 789 | 13 | $collection->add($document); |
|
| 790 | } |
||
| 791 | 14 | } |
|
| 792 | |||
| 793 | /** |
||
| 794 | * @param PersistentCollectionInterface $collection |
||
| 795 | * |
||
| 796 | * @return Query |
||
| 797 | */ |
||
| 798 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 834 | |||
| 835 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 836 | { |
||
| 837 | 3 | $cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection); |
|
| 838 | 3 | $mapping = $collection->getMapping(); |
|
| 839 | 3 | $documents = $cursor->toArray(false); |
|
| 840 | 3 | foreach ($documents as $key => $obj) { |
|
| 841 | 3 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 842 | 1 | $collection->set($key, $obj); |
|
| 843 | } else { |
||
| 844 | 3 | $collection->add($obj); |
|
| 845 | } |
||
| 846 | } |
||
| 847 | 3 | } |
|
| 848 | |||
| 849 | /** |
||
| 850 | * @param PersistentCollectionInterface $collection |
||
| 851 | * |
||
| 852 | * @return CursorInterface |
||
| 853 | */ |
||
| 854 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 855 | { |
||
| 856 | 3 | $hints = $collection->getHints(); |
|
| 857 | 3 | $mapping = $collection->getMapping(); |
|
| 858 | 3 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 859 | 3 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 860 | 3 | ->$repositoryMethod($collection->getOwner()); |
|
| 861 | |||
| 862 | 3 | if ( ! $cursor instanceof CursorInterface) { |
|
| 863 | throw new \BadMethodCallException("Expected repository method {$repositoryMethod} to return a CursorInterface"); |
||
| 864 | } |
||
| 865 | |||
| 866 | 3 | if (isset($mapping['sort'])) { |
|
| 867 | 3 | $cursor->sort($mapping['sort']); |
|
| 868 | } |
||
| 869 | 3 | if (isset($mapping['limit'])) { |
|
| 870 | $cursor->limit($mapping['limit']); |
||
| 871 | } |
||
| 872 | 3 | if (isset($mapping['skip'])) { |
|
| 873 | $cursor->skip($mapping['skip']); |
||
| 874 | } |
||
| 875 | 3 | if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) { |
|
| 876 | $cursor->slaveOkay(true); |
||
| 877 | } |
||
| 878 | 3 | View Code Duplication | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
| 879 | $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); |
||
| 880 | } |
||
| 881 | |||
| 882 | 3 | return $cursor; |
|
| 883 | } |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 887 | * property names, to MongoDB field names. |
||
| 888 | * |
||
| 889 | * @param array $fields |
||
| 890 | * @return array |
||
| 891 | */ |
||
| 892 | 139 | public function prepareSortOrProjection(array $fields) |
|
| 893 | { |
||
| 894 | 139 | $preparedFields = array(); |
|
| 895 | |||
| 896 | 139 | foreach ($fields as $key => $value) { |
|
| 897 | 33 | $preparedFields[$this->prepareFieldName($key)] = $value; |
|
| 898 | } |
||
| 899 | |||
| 900 | 139 | return $preparedFields; |
|
| 901 | } |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 905 | * |
||
| 906 | * @param string $fieldName |
||
| 907 | * @return string |
||
| 908 | */ |
||
| 909 | 85 | public function prepareFieldName($fieldName) |
|
| 910 | { |
||
| 911 | 85 | list($fieldName) = $this->prepareQueryElement($fieldName, null, null, false); |
|
| 912 | |||
| 913 | 85 | return $fieldName; |
|
| 914 | } |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Adds discriminator criteria to an already-prepared query. |
||
| 918 | * |
||
| 919 | * This method should be used once for query criteria and not be used for |
||
| 920 | * nested expressions. It should be called before |
||
| 921 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 922 | * |
||
| 923 | * @param array $preparedQuery |
||
| 924 | * @return array |
||
| 925 | */ |
||
| 926 | 499 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 927 | { |
||
| 928 | /* If the class has a discriminator field, which is not already in the |
||
| 929 | * criteria, inject it now. The field/values need no preparation. |
||
| 930 | */ |
||
| 931 | 499 | if ($this->class->hasDiscriminator() && ! isset($preparedQuery[$this->class->discriminatorField])) { |
|
| 932 | 21 | $discriminatorValues = $this->getClassDiscriminatorValues($this->class); |
|
| 933 | 21 | if (count($discriminatorValues) === 1) { |
|
| 934 | 13 | $preparedQuery[$this->class->discriminatorField] = $discriminatorValues[0]; |
|
| 935 | } else { |
||
| 936 | 10 | $preparedQuery[$this->class->discriminatorField] = array('$in' => $discriminatorValues); |
|
| 937 | } |
||
| 938 | } |
||
| 939 | |||
| 940 | 499 | return $preparedQuery; |
|
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Adds filter criteria to an already-prepared query. |
||
| 945 | * |
||
| 946 | * This method should be used once for query criteria and not be used for |
||
| 947 | * nested expressions. It should be called after |
||
| 948 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 949 | * |
||
| 950 | * @param array $preparedQuery |
||
| 951 | * @return array |
||
| 952 | */ |
||
| 953 | 500 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 954 | { |
||
| 955 | /* If filter criteria exists for this class, prepare it and merge |
||
| 956 | * over the existing query. |
||
| 957 | * |
||
| 958 | * @todo Consider recursive merging in case the filter criteria and |
||
| 959 | * prepared query both contain top-level $and/$or operators. |
||
| 960 | */ |
||
| 961 | 500 | if ($filterCriteria = $this->dm->getFilterCollection()->getFilterCriteria($this->class)) { |
|
| 962 | 16 | $preparedQuery = $this->cm->merge($preparedQuery, $this->prepareQueryOrNewObj($filterCriteria)); |
|
| 963 | } |
||
| 964 | |||
| 965 | 500 | return $preparedQuery; |
|
| 966 | } |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Prepares the query criteria or new document object. |
||
| 970 | * |
||
| 971 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 972 | * |
||
| 973 | * @param array $query |
||
| 974 | * @return array |
||
| 975 | */ |
||
| 976 | 533 | public function prepareQueryOrNewObj(array $query) |
|
| 977 | { |
||
| 978 | 533 | $preparedQuery = array(); |
|
| 979 | |||
| 980 | 533 | foreach ($query as $key => $value) { |
|
| 981 | // Recursively prepare logical query clauses |
||
| 982 | 494 | if (in_array($key, array('$and', '$or', '$nor')) && is_array($value)) { |
|
| 983 | 20 | foreach ($value as $k2 => $v2) { |
|
| 984 | 20 | $preparedQuery[$key][$k2] = $this->prepareQueryOrNewObj($v2); |
|
| 985 | } |
||
| 986 | 20 | continue; |
|
| 987 | } |
||
| 988 | |||
| 989 | 494 | if (isset($key[0]) && $key[0] === '$' && is_array($value)) { |
|
| 990 | 20 | $preparedQuery[$key] = $this->prepareQueryOrNewObj($value); |
|
| 991 | 20 | continue; |
|
| 992 | } |
||
| 993 | |||
| 994 | 494 | list($key, $value) = $this->prepareQueryElement($key, $value, null, true); |
|
| 995 | |||
| 996 | 494 | $preparedQuery[$key] = is_array($value) |
|
| 997 | 123 | ? array_map('\Doctrine\ODM\MongoDB\Types\Type::convertPHPToDatabaseValue', $value) |
|
| 998 | 494 | : Type::convertPHPToDatabaseValue($value); |
|
| 999 | } |
||
| 1000 | |||
| 1001 | 533 | return $preparedQuery; |
|
| 1002 | } |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Prepares a query value and converts the PHP value to the database value |
||
| 1006 | * if it is an identifier. |
||
| 1007 | * |
||
| 1008 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1009 | * |
||
| 1010 | * @param string $fieldName |
||
| 1011 | * @param mixed $value |
||
| 1012 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1013 | * @param boolean $prepareValue Whether or not to prepare the value |
||
| 1014 | * @return array Prepared field name and value |
||
| 1015 | */ |
||
| 1016 | 525 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
|
| 1017 | { |
||
| 1018 | 525 | $class = isset($class) ? $class : $this->class; |
|
| 1019 | |||
| 1020 | // @todo Consider inlining calls to ClassMetadataInfo methods |
||
| 1021 | |||
| 1022 | // Process all non-identifier fields by translating field names |
||
| 1023 | 525 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 1024 | 240 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 1025 | 240 | $fieldName = $mapping['name']; |
|
| 1026 | |||
| 1027 | 240 | if ( ! $prepareValue) { |
|
| 1028 | 62 | return array($fieldName, $value); |
|
| 1029 | } |
||
| 1030 | |||
| 1031 | // Prepare mapped, embedded objects |
||
| 1032 | 198 | if ( ! empty($mapping['embedded']) && is_object($value) && |
|
| 1033 | 198 | ! $this->dm->getMetadataFactory()->isTransient(get_class($value))) { |
|
| 1034 | 3 | return array($fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)); |
|
| 1035 | } |
||
| 1036 | |||
| 1037 | 196 | if (! empty($mapping['reference']) && is_object($value) && ! ($value instanceof \MongoId)) { |
|
| 1038 | try { |
||
| 1039 | 5 | return array($fieldName, $this->dm->createDBRef($value, $mapping)); |
|
| 1040 | 1 | } catch (MappingException $e) { |
|
| 1041 | // do nothing in case passed object is not mapped document |
||
| 1042 | } |
||
| 1043 | } |
||
| 1044 | |||
| 1045 | // No further preparation unless we're dealing with a simple reference |
||
| 1046 | // We can't have expressions in empty() with PHP < 5.5, so store it in a variable |
||
| 1047 | 192 | $arrayValue = (array) $value; |
|
| 1048 | 192 | if (empty($mapping['reference']) || $mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID || empty($arrayValue)) { |
|
| 1049 | 117 | return array($fieldName, $value); |
|
| 1050 | } |
||
| 1051 | |||
| 1052 | // Additional preparation for one or more simple reference values |
||
| 1053 | 103 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1054 | |||
| 1055 | 103 | if ( ! is_array($value)) { |
|
| 1056 | 99 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1057 | } |
||
| 1058 | |||
| 1059 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1060 | 6 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1061 | 3 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1062 | } |
||
| 1063 | |||
| 1064 | 6 | return array($fieldName, $this->prepareQueryExpression($value, $targetClass)); |
|
| 1065 | } |
||
| 1066 | |||
| 1067 | // Process identifier fields |
||
| 1068 | 397 | if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') { |
|
| 1069 | 329 | $fieldName = '_id'; |
|
| 1070 | |||
| 1071 | 329 | if ( ! $prepareValue) { |
|
| 1072 | 16 | return array($fieldName, $value); |
|
| 1073 | } |
||
| 1074 | |||
| 1075 | 315 | if ( ! is_array($value)) { |
|
| 1076 | 292 | return array($fieldName, $class->getDatabaseIdentifierValue($value)); |
|
| 1077 | } |
||
| 1078 | |||
| 1079 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1080 | 56 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1081 | 6 | return array($fieldName, $class->getDatabaseIdentifierValue($value)); |
|
| 1082 | } |
||
| 1083 | |||
| 1084 | 51 | return array($fieldName, $this->prepareQueryExpression($value, $class)); |
|
| 1085 | } |
||
| 1086 | |||
| 1087 | // No processing for unmapped, non-identifier, non-dotted field names |
||
| 1088 | 101 | if (strpos($fieldName, '.') === false) { |
|
| 1089 | 44 | return array($fieldName, $value); |
|
| 1090 | } |
||
| 1091 | |||
| 1092 | /* Process "fieldName.objectProperty" queries (on arrays or objects). |
||
| 1093 | * |
||
| 1094 | * We can limit parsing here, since at most three segments are |
||
| 1095 | * significant: "fieldName.objectProperty" with an optional index or key |
||
| 1096 | * for collections stored as either BSON arrays or objects. |
||
| 1097 | */ |
||
| 1098 | 63 | $e = explode('.', $fieldName, 4); |
|
| 1099 | |||
| 1100 | // No further processing for unmapped fields |
||
| 1101 | 63 | if ( ! isset($class->fieldMappings[$e[0]])) { |
|
| 1102 | 4 | return array($fieldName, $value); |
|
| 1103 | } |
||
| 1104 | |||
| 1105 | 60 | $mapping = $class->fieldMappings[$e[0]]; |
|
| 1106 | 60 | $e[0] = $mapping['name']; |
|
| 1107 | |||
| 1108 | // Hash and raw fields will not be prepared beyond the field name |
||
| 1109 | 60 | if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) { |
|
| 1110 | 1 | $fieldName = implode('.', $e); |
|
| 1111 | |||
| 1112 | 1 | return array($fieldName, $value); |
|
| 1113 | } |
||
| 1114 | |||
| 1115 | 59 | if ($mapping['type'] == 'many' && CollectionHelper::isHash($mapping['strategy']) |
|
| 1116 | 59 | && isset($e[2])) { |
|
| 1117 | 1 | $objectProperty = $e[2]; |
|
| 1118 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1119 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1120 | 58 | } elseif ($e[1] != '$') { |
|
| 1121 | 57 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1122 | 57 | $objectProperty = $e[1]; |
|
| 1123 | 57 | $objectPropertyPrefix = ''; |
|
| 1124 | 57 | $nextObjectProperty = implode('.', array_slice($e, 2)); |
|
| 1125 | 1 | } elseif (isset($e[2])) { |
|
| 1126 | 1 | $fieldName = $e[0] . '.' . $e[1] . '.' . $e[2]; |
|
| 1127 | 1 | $objectProperty = $e[2]; |
|
| 1128 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1129 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1130 | } else { |
||
| 1131 | 1 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1132 | |||
| 1133 | 1 | return array($fieldName, $value); |
|
| 1134 | } |
||
| 1135 | |||
| 1136 | // No further processing for fields without a targetDocument mapping |
||
| 1137 | 59 | if ( ! isset($mapping['targetDocument'])) { |
|
| 1138 | 3 | if ($nextObjectProperty) { |
|
| 1139 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1140 | } |
||
| 1141 | |||
| 1142 | 3 | return array($fieldName, $value); |
|
| 1143 | } |
||
| 1144 | |||
| 1145 | 56 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1146 | |||
| 1147 | // No further processing for unmapped targetDocument fields |
||
| 1148 | 56 | if ( ! $targetClass->hasField($objectProperty)) { |
|
| 1149 | 24 | if ($nextObjectProperty) { |
|
| 1150 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1151 | } |
||
| 1152 | |||
| 1153 | 24 | return array($fieldName, $value); |
|
| 1154 | } |
||
| 1155 | |||
| 1156 | 35 | $targetMapping = $targetClass->getFieldMapping($objectProperty); |
|
| 1157 | 35 | $objectPropertyIsId = $targetClass->isIdentifier($objectProperty); |
|
| 1158 | |||
| 1159 | // Prepare DBRef identifiers or the mapped field's property path |
||
| 1160 | 35 | $fieldName = ($objectPropertyIsId && ! empty($mapping['reference']) && $mapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) |
|
| 1161 | 13 | ? $e[0] . '.$id' |
|
| 1162 | 35 | : $e[0] . '.' . $objectPropertyPrefix . $targetMapping['name']; |
|
| 1163 | |||
| 1164 | // Process targetDocument identifier fields |
||
| 1165 | 35 | if ($objectPropertyIsId) { |
|
| 1166 | 14 | if ( ! $prepareValue) { |
|
| 1167 | 1 | return array($fieldName, $value); |
|
| 1168 | } |
||
| 1169 | |||
| 1170 | 13 | if ( ! is_array($value)) { |
|
| 1171 | 2 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1172 | } |
||
| 1173 | |||
| 1174 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1175 | 12 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1176 | 3 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1177 | } |
||
| 1178 | |||
| 1179 | 12 | return array($fieldName, $this->prepareQueryExpression($value, $targetClass)); |
|
| 1180 | } |
||
| 1181 | |||
| 1182 | /* The property path may include a third field segment, excluding the |
||
| 1183 | * collection item pointer. If present, this next object property must |
||
| 1184 | * be processed recursively. |
||
| 1185 | */ |
||
| 1186 | 21 | if ($nextObjectProperty) { |
|
| 1187 | // Respect the targetDocument's class metadata when recursing |
||
| 1188 | 14 | $nextTargetClass = isset($targetMapping['targetDocument']) |
|
| 1189 | 8 | ? $this->dm->getClassMetadata($targetMapping['targetDocument']) |
|
| 1190 | 14 | : null; |
|
| 1191 | |||
| 1192 | 14 | list($key, $value) = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue); |
|
| 1193 | |||
| 1194 | 14 | $fieldName .= '.' . $key; |
|
| 1195 | } |
||
| 1196 | |||
| 1197 | 21 | return array($fieldName, $value); |
|
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Prepares a query expression. |
||
| 1202 | * |
||
| 1203 | * @param array|object $expression |
||
| 1204 | * @param ClassMetadata $class |
||
| 1205 | * @return array |
||
| 1206 | */ |
||
| 1207 | 69 | private function prepareQueryExpression($expression, $class) |
|
| 1208 | { |
||
| 1209 | 69 | foreach ($expression as $k => $v) { |
|
| 1210 | // Ignore query operators whose arguments need no type conversion |
||
| 1211 | 69 | if (in_array($k, array('$exists', '$type', '$mod', '$size'))) { |
|
| 1212 | 12 | continue; |
|
| 1213 | } |
||
| 1214 | |||
| 1215 | // Process query operators whose argument arrays need type conversion |
||
| 1216 | 69 | if (in_array($k, array('$in', '$nin', '$all')) && is_array($v)) { |
|
| 1217 | 67 | foreach ($v as $k2 => $v2) { |
|
| 1218 | 67 | $expression[$k][$k2] = $class->getDatabaseIdentifierValue($v2); |
|
| 1219 | } |
||
| 1220 | 67 | continue; |
|
| 1221 | } |
||
| 1222 | |||
| 1223 | // Recursively process expressions within a $not operator |
||
| 1224 | 14 | if ($k === '$not' && is_array($v)) { |
|
| 1225 | 11 | $expression[$k] = $this->prepareQueryExpression($v, $class); |
|
| 1226 | 11 | continue; |
|
| 1227 | } |
||
| 1228 | |||
| 1229 | 14 | $expression[$k] = $class->getDatabaseIdentifierValue($v); |
|
| 1230 | } |
||
| 1231 | |||
| 1232 | 69 | return $expression; |
|
| 1233 | } |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Checks whether the value has DBRef fields. |
||
| 1237 | * |
||
| 1238 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1239 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1240 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1241 | * $elemMatch criteria for matching a DBRef. |
||
| 1242 | * |
||
| 1243 | * @param mixed $value |
||
| 1244 | * @return boolean |
||
| 1245 | */ |
||
| 1246 | 70 | private function hasDBRefFields($value) |
|
| 1247 | { |
||
| 1248 | 70 | if ( ! is_array($value) && ! is_object($value)) { |
|
| 1249 | return false; |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | 70 | if (is_object($value)) { |
|
| 1253 | $value = get_object_vars($value); |
||
| 1254 | } |
||
| 1255 | |||
| 1256 | 70 | foreach ($value as $key => $_) { |
|
| 1257 | 70 | if ($key === '$ref' || $key === '$id' || $key === '$db') { |
|
| 1258 | 70 | return true; |
|
| 1259 | } |
||
| 1260 | } |
||
| 1261 | |||
| 1262 | 69 | return false; |
|
| 1263 | } |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Checks whether the value has query operators. |
||
| 1267 | * |
||
| 1268 | * @param mixed $value |
||
| 1269 | * @return boolean |
||
| 1270 | */ |
||
| 1271 | 74 | private function hasQueryOperators($value) |
|
| 1272 | { |
||
| 1273 | 74 | if ( ! is_array($value) && ! is_object($value)) { |
|
| 1274 | return false; |
||
| 1275 | } |
||
| 1276 | |||
| 1277 | 74 | if (is_object($value)) { |
|
| 1278 | $value = get_object_vars($value); |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | 74 | foreach ($value as $key => $_) { |
|
| 1282 | 74 | if (isset($key[0]) && $key[0] === '$') { |
|
| 1283 | 74 | return true; |
|
| 1284 | } |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | 9 | return false; |
|
| 1288 | } |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1292 | * |
||
| 1293 | * @param ClassMetadata $metadata |
||
| 1294 | * @return array |
||
| 1295 | */ |
||
| 1296 | 21 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1312 | |||
| 1313 | 590 | private function handleCollections($document, $options) |
|
| 1314 | { |
||
| 1315 | // Collection deletions (deletions of complete collections) |
||
| 1316 | 590 | foreach ($this->uow->getScheduledCollections($document) as $coll) { |
|
| 1317 | 105 | if ($this->uow->isCollectionScheduledForDeletion($coll)) { |
|
| 1318 | 105 | $this->cp->delete($coll, $options); |
|
| 1319 | } |
||
| 1320 | } |
||
| 1321 | // Collection updates (deleteRows, updateRows, insertRows) |
||
| 1322 | 590 | foreach ($this->uow->getScheduledCollections($document) as $coll) { |
|
| 1323 | 105 | if ($this->uow->isCollectionScheduledForUpdate($coll)) { |
|
| 1324 | 105 | $this->cp->update($coll, $options); |
|
| 1332 | |||
| 1333 | /** |
||
| 1334 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1335 | * Also, shard key field should be present in actual document data. |
||
| 1336 | * |
||
| 1337 | * @param object $document |
||
| 1338 | * @param string $shardKeyField |
||
| 1339 | * @param array $actualDocumentData |
||
| 1340 | * |
||
| 1341 | * @throws MongoDBException |
||
| 1342 | */ |
||
| 1343 | 9 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Get shard key aware query for single document. |
||
| 1362 | * |
||
| 1363 | * @param object $document |
||
| 1364 | * |
||
| 1365 | * @return array |
||
| 1366 | */ |
||
| 1367 | 295 | private function getQueryForDocument($document) |
|
| 1377 | |||
| 1378 | /** |
||
| 1379 | * @param array $options |
||
| 1380 | * |
||
| 1381 | * @return array |
||
| 1382 | */ |
||
| 1383 | 591 | private function getWriteOptions(array $options = array()) |
|
| 1393 | } |
||
| 1394 |
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: