Completed
Pull Request — master (#1624)
by Abir
08:47
created

PersistenceBuilder::prepareInsertData()   C

Complexity

Conditions 16
Paths 75

Size

Total Lines 51
Code Lines 24

Duplication

Lines 5
Ratio 9.8 %

Code Coverage

Tests 24
CRAP Score 16

Importance

Changes 0
Metric Value
dl 5
loc 51
ccs 24
cts 24
cp 1
rs 5.6194
c 0
b 0
f 0
cc 16
eloc 24
nc 75
nop 1
crap 16

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
namespace Doctrine\ODM\MongoDB\Persisters;
20
21
use Doctrine\ODM\MongoDB\DocumentManager;
22
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
23
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
24
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
25
use Doctrine\ODM\MongoDB\Types\Type;
26
use Doctrine\ODM\MongoDB\UnitOfWork;
27
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
28
29
/**
30
 * PersistenceBuilder builds the queries used by the persisters to update and insert
31
 * documents when a DocumentManager is flushed. It uses the changeset information in the
32
 * UnitOfWork to build queries using atomic operators like $set, $unset, etc.
33
 *
34
 * @since       1.0
35
 */
36
class PersistenceBuilder
37
{
38
    /**
39
     * The DocumentManager instance.
40
     *
41
     * @var DocumentManager
42
     */
43
    private $dm;
44
45
    /**
46
     * The UnitOfWork instance.
47
     *
48
     * @var UnitOfWork
49
     */
50
    private $uow;
51
52
    /**
53
     * Initializes a new PersistenceBuilder instance.
54
     *
55
     * @param DocumentManager $dm
56
     * @param UnitOfWork $uow
57
     */
58 759
    public function __construct(DocumentManager $dm, UnitOfWork $uow)
59
    {
60 759
        $this->dm = $dm;
61 759
        $this->uow = $uow;
62 759
    }
63
64
    /**
65
     * Prepares the array that is ready to be inserted to mongodb for a given object document.
66
     *
67
     * @param object $document
68
     * @return array $insertData
69
     */
70 532
    public function prepareInsertData($document)
71
    {
72 532
        $class = $this->dm->getClassMetadata(get_class($document));
73 532
        $changeset = $this->uow->getDocumentChangeSet($document);
74
75 532
        $insertData = array();
76 532
        foreach ($class->fieldMappings as $mapping) {
77
78 532
            $new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
79
80 532
            if ($new === null && $mapping['nullable']) {
81 166
                $insertData[$mapping['name']] = null;
82
            }
83
84
            /* Nothing more to do for null values, since we're either storing
85
             * them (if nullable was true) or not.
86
             */
87 532
            if ($new === null) {
88 380
                continue;
89
            }
90
91
            // @Field, @String, @Date, etc.
92 532
            if ( ! isset($mapping['association'])) {
93 532
                $insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);
94
95
            // @ReferenceOne
96 431
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
97 94
                $insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);
98
99
            // @EmbedOne
100 406
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
101 68
                $insertData[$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);
102
103
            // @ReferenceMany, @EmbedMany
104
            // We're excluding collections using addToSet since there is a risk
105
            // of duplicated entries stored in the collection
106 384
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
107 384
                    && $mapping['strategy'] !== ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET) {
108 532
                $insertData[$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
109
            }
110
        }
111
112
        // add discriminator if the class has one
113 531 View Code Duplication
        if (isset($class->discriminatorField)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 33
            $insertData[$class->discriminatorField] = isset($class->discriminatorValue)
115 26
                ? $class->discriminatorValue
116 11
                : $class->name;
117
        }
118
119 531
        return $insertData;
120
    }
121
122
    /**
123
     * Prepares the update query to update a given document object in mongodb.
124
     *
125
     * @param object $document
126
     * @return array $updateData
127
     */
128 220
    public function prepareUpdateData($document)
129
    {
130 220
        $class = $this->dm->getClassMetadata(get_class($document));
131 220
        $changeset = $this->uow->getDocumentChangeSet($document);
132
133 220
        $updateData = array();
134 220
        foreach ($changeset as $fieldName => $change) {
135 219
            $mapping = $class->fieldMappings[$fieldName];
136
137
            // skip non embedded document identifiers
138 219
            if ( ! $class->isEmbeddedDocument && ! empty($mapping['id'])) {
139 2
                continue;
140
            }
141
142 218
            list($old, $new) = $change;
143
144
            // Scalar fields
145 218
            if ( ! isset($mapping['association'])) {
146 116
                if ($new === null && $mapping['nullable'] !== true) {
147 1
                    $updateData['$unset'][$mapping['name']] = true;
148
                } else {
149 116 View Code Duplication
                    if ($new !== null && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150 4
                        $operator = '$inc';
151 4
                        $value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old);
152
                    } else {
153 113
                        $operator = '$set';
154 113
                        $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
155
                    }
156
157 116
                    $updateData[$operator][$mapping['name']] = $value;
158
                }
159
160
            // @EmbedOne
161 144
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
162
                // If we have a new embedded document then lets set the whole thing
163 28
                if ($new && $this->uow->isScheduledForInsert($new)) {
164 10
                    $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);
165
166
                // If we don't have a new value then lets unset the embedded document
167 21
                } elseif ( ! $new) {
168 3
                    $updateData['$unset'][$mapping['name']] = true;
169
170
                // Update existing embedded document
171 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172 18
                    $update = $this->prepareUpdateData($new);
173 18
                    foreach ($update as $cmd => $values) {
174 14
                        foreach ($values as $key => $value) {
175 28
                            $updateData[$cmd][$mapping['name'] . '.' . $key] = $value;
176
                        }
177
                    }
178
                }
179
180
            // @ReferenceMany, @EmbedMany
181 127
            } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) {
182 117
                if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) {
183 20
                    $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
184 99 View Code Duplication
                } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($new)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185 2
                    $updateData['$unset'][$mapping['name']] = true;
186 2
                    $this->uow->unscheduleCollectionDeletion($new);
187 97
                } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($old)) {
188 2
                    $updateData['$unset'][$mapping['name']] = true;
189 2
                    $this->uow->unscheduleCollectionDeletion($old);
190 95
                } elseif ($mapping['association'] === ClassMetadata::EMBED_MANY) {
191 59
                    foreach ($new as $key => $embeddedDoc) {
192 51
                        if ( ! $this->uow->isScheduledForInsert($embeddedDoc)) {
193 40
                            $update = $this->prepareUpdateData($embeddedDoc);
194 40
                            foreach ($update as $cmd => $values) {
195 14
                                foreach ($values as $name => $value) {
196 117
                                    $updateData[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value;
197
                                }
198
                            }
199
                        }
200
                    }
201
                }
202
203
            // @ReferenceOne
204 14
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
205 12 View Code Duplication
                if (isset($new) || $mapping['nullable'] === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206 12
                    $updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
207
                } else {
208 218
                    $updateData['$unset'][$mapping['name']] = true;
209
                }
210
            }
211
        }
212
        // collections that aren't dirty but could be subject to update are
213
        // excluded from change set, let's go through them now
214 220
        foreach ($this->uow->getScheduledCollections($document) as $coll) {
215 96
            $mapping = $coll->getMapping();
216 96
            if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($coll)) {
217 3
                $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($coll, true);
218 93 View Code Duplication
            } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($coll)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219 1
                $updateData['$unset'][$mapping['name']] = true;
220 96
                $this->uow->unscheduleCollectionDeletion($coll);
221
            }
222
            // @ReferenceMany is handled by CollectionPersister
223
        }
224 220
        return $updateData;
225
    }
226
227
    /**
228
     * Prepares the update query to upsert a given document object in mongodb.
229
     *
230
     * @param object $document
231
     * @return array $updateData
232
     */
233 87
    public function prepareUpsertData($document)
234
    {
235 87
        $class = $this->dm->getClassMetadata(get_class($document));
236 87
        $changeset = $this->uow->getDocumentChangeSet($document);
237
238 87
        $updateData = array();
239 87
        foreach ($changeset as $fieldName => $change) {
240 87
            $mapping = $class->fieldMappings[$fieldName];
241
242 87
            list($old, $new) = $change;
243
244
            // Scalar fields
245 87
            if ( ! isset($mapping['association'])) {
246 87
                if ($new !== null || $mapping['nullable'] === true) {
247 87 View Code Duplication
                    if ($new !== null && empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadataInfo::STORAGE_STRATEGY_INCREMENT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248 3
                        $operator = '$inc';
249 3
                        $value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old);
250
                    } else {
251 87
                        $operator = '$set';
252 87
                        $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
253
                    }
254
255 87
                    $updateData[$operator][$mapping['name']] = $value;
256
                }
257
258
            // @EmbedOne
259 27
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
260
                // If we don't have a new value then do nothing on upsert
261
                // If we have a new embedded document then lets set the whole thing
262 8
                if ($new && $this->uow->isScheduledForInsert($new)) {
263 5
                    $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);
264 3 View Code Duplication
                } elseif ($new) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
                    // Update existing embedded document
266
                    $update = $this->prepareUpsertData($new);
267
                    foreach ($update as $cmd => $values) {
268
                        foreach ($values as $key => $value) {
269 8
                            $updateData[$cmd][$mapping['name'] . '.' . $key] = $value;
270
                        }
271
                    }
272
                }
273
274
            // @ReferenceOne
275 24 View Code Duplication
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276 13
                if (isset($new) || $mapping['nullable'] === true) {
277 13
                    $updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
278
                }
279
280
            // @ReferenceMany, @EmbedMany
281 15
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
282 15
                    && $new instanceof PersistentCollectionInterface && $new->isDirty()
283 15
                    && CollectionHelper::isAtomic($mapping['strategy'])) {
284 87
                $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
285
            }
286
            // @EmbedMany and @ReferenceMany are handled by CollectionPersister
287
        }
288
289
        // add discriminator if the class has one
290 87 View Code Duplication
        if (isset($class->discriminatorField)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291 5
            $updateData['$set'][$class->discriminatorField] = isset($class->discriminatorValue)
292 5
                ? $class->discriminatorValue
293
                : $class->name;
294
        }
295
296 87
        return $updateData;
297
    }
298
299
    /**
300
     * Returns the reference representation to be stored in MongoDB.
301
     *
302
     * If the document does not have an identifier and the mapping calls for a
303
     * simple reference, null may be returned.
304
     *
305
     * @param array $referenceMapping
306
     * @param object $document
307
     * @return array|null
308
     */
309 217
    public function prepareReferencedDocumentValue(array $referenceMapping, $document)
310
    {
311 217
        return $this->dm->createDBRef($document, $referenceMapping);
312
    }
313
314
    /**
315
     * Returns the embedded document to be stored in MongoDB.
316
     *
317
     * The return value will usually be an associative array with string keys
318
     * corresponding to field names on the embedded document. An object may be
319
     * returned if the document is empty, to ensure that a BSON object will be
320
     * stored in lieu of an array.
321
     *
322
     * If $includeNestedCollections is true, nested collections will be included
323
     * in this prepared value and the option will cascade to all embedded
324
     * associations. If any nested PersistentCollections (embed or reference)
325
     * within this value were previously scheduled for deletion or update, they
326
     * will also be unscheduled.
327
     *
328
     * @param array $embeddedMapping
329
     * @param object $embeddedDocument
330
     * @param boolean $includeNestedCollections
331
     * @return array|object
332
     * @throws \UnexpectedValueException if an unsupported associating mapping is found
333
     */
334 185
    public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false)
335
    {
336 185
        $embeddedDocumentValue = array();
337 185
        $class = $this->dm->getClassMetadata(get_class($embeddedDocument));
338
339 185
        foreach ($class->fieldMappings as $mapping) {
340
            // Skip notSaved fields
341 183
            if ( ! empty($mapping['notSaved'])) {
342 1
                continue;
343
            }
344
345
            // Inline ClassMetadataInfo::getFieldValue()
346 183
            $rawValue = $class->reflFields[$mapping['fieldName']]->getValue($embeddedDocument);
347
348 183
            $value = null;
349
350 183
            if ($rawValue !== null) {
351 180
                switch (isset($mapping['association']) ? $mapping['association'] : null) {
352
                    // @Field, @String, @Date, etc.
353 180
                    case null:
354 174
                        $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
355 174
                        break;
356
357 67
                    case ClassMetadata::EMBED_ONE:
358 65
                    case ClassMetadata::REFERENCE_ONE:
359
                        // Nested collections should only be included for embedded relationships
360 23
                        $value = $this->prepareAssociatedDocumentValue($mapping, $rawValue, $includeNestedCollections && isset($mapping['embedded']));
361 23
                        break;
362
363 46
                    case ClassMetadata::EMBED_MANY:
364 4
                    case ClassMetadata::REFERENCE_MANY:
365
                        // Skip PersistentCollections already scheduled for deletion
366 46
                        if ( ! $includeNestedCollections && $rawValue instanceof PersistentCollectionInterface
367 46
                            && $this->uow->isCollectionScheduledForDeletion($rawValue)) {
368
                            break;
369
                        }
370
371 46
                        $value = $this->prepareAssociatedCollectionValue($rawValue, $includeNestedCollections);
372 46
                        break;
373
374
                    default:
375
                        throw new \UnexpectedValueException('Unsupported mapping association: ' . $mapping['association']);
376
                }
377
            }
378
379
            // Omit non-nullable fields that would have a null value
380 183
            if ($value === null && $mapping['nullable'] === false) {
381 61
                continue;
382
            }
383
384 180
            $embeddedDocumentValue[$mapping['name']] = $value;
385
        }
386
387
        /* Add a discriminator value if the embedded document is not mapped
388
         * explicitly to a targetDocument class.
389
         */
390 185 View Code Duplication
        if ( ! isset($embeddedMapping['targetDocument'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
391 16
            $discriminatorField = $embeddedMapping['discriminatorField'];
392 16
            $discriminatorValue = isset($embeddedMapping['discriminatorMap'])
393 5
                ? array_search($class->name, $embeddedMapping['discriminatorMap'])
394 16
                : $class->name;
395
396
            /* If the discriminator value was not found in the map, use the full
397
             * class name. In the future, it may be preferable to throw an
398
             * exception here (perhaps based on some strictness option).
399
             *
400
             * @see DocumentManager::createDBRef()
401
             */
402 16
            if ($discriminatorValue === false) {
403 2
                $discriminatorValue = $class->name;
404
            }
405
406 16
            $embeddedDocumentValue[$discriminatorField] = $discriminatorValue;
407
        }
408
409
        /* If the class has a discriminator (field and value), use it. A child
410
         * class that is not defined in the discriminator map may only have a
411
         * discriminator field and no value, so default to the full class name.
412
         */
413 185 View Code Duplication
        if (isset($class->discriminatorField)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
414 8
            $embeddedDocumentValue[$class->discriminatorField] = isset($class->discriminatorValue)
415 6
                ? $class->discriminatorValue
416 4
                : $class->name;
417
        }
418
419
        // Ensure empty embedded documents are stored as BSON objects
420 185
        if (empty($embeddedDocumentValue)) {
421 6
            return (object) $embeddedDocumentValue;
422
        }
423
424
        /* @todo Consider always casting the return value to an object, or
425
         * building $embeddedDocumentValue as an object instead of an array, to
426
         * handle the edge case where all database field names are sequential,
427
         * numeric keys.
428
         */
429 181
        return $embeddedDocumentValue;
430
    }
431
432
    /*
433
     * Returns the embedded document or reference representation to be stored.
434
     *
435
     * @param array $mapping
436
     * @param object $document
437
     * @param boolean $includeNestedCollections
438
     * @return array|object|null
439
     * @throws \InvalidArgumentException if the mapping is neither embedded nor reference
440
     */
441 23
    public function prepareAssociatedDocumentValue(array $mapping, $document, $includeNestedCollections = false)
442
    {
443 23
        if (isset($mapping['embedded'])) {
444 11
            return $this->prepareEmbeddedDocumentValue($mapping, $document, $includeNestedCollections);
445
        }
446
447 20
        if (isset($mapping['reference'])) {
448 20
            return $this->prepareReferencedDocumentValue($mapping, $document);
449
        }
450
451
        throw new \InvalidArgumentException('Mapping is neither embedded nor reference.');
452
    }
453
454
    /**
455
     * Returns the collection representation to be stored and unschedules it afterwards.
456
     *
457
     * @param PersistentCollectionInterface $coll
458
     * @param bool $includeNestedCollections
459
     * @return array
460
     */
461 389
    public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false)
462
    {
463 389
        $mapping = $coll->getMapping();
464 389
        $pb = $this;
465 389
        $callback = isset($mapping['embedded'])
466
            ? function($v) use ($pb, $mapping, $includeNestedCollections) {
467 123
                return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
468 319
            }
469
            : function($v) use ($pb, $mapping) { return $pb->prepareReferencedDocumentValue($mapping, $v); };
470
471 389
        $setData = $coll->map($callback)->toArray();
472 389
        if (CollectionHelper::isList($mapping['strategy'])) {
473 377
            $setData = array_values($setData);
474
        }
475
476 389
        $this->uow->unscheduleCollectionDeletion($coll);
477 389
        $this->uow->unscheduleCollectionUpdate($coll);
478
479 389
        return $setData;
480
    }
481
}
482