Completed
Pull Request — master (#1508)
by Andreas
10:52
created

PersistenceBuilder::prepareUpdateData()   D

Complexity

Conditions 42
Paths 72

Size

Total Lines 98
Code Lines 59

Duplication

Lines 28
Ratio 28.57 %

Code Coverage

Tests 52
CRAP Score 42.2855

Importance

Changes 0
Metric Value
dl 28
loc 98
ccs 52
cts 55
cp 0.9455
rs 4.2435
c 0
b 0
f 0
cc 42
eloc 59
nc 72
nop 1
crap 42.2855

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 737
    public function __construct(DocumentManager $dm, UnitOfWork $uow)
59
    {
60 737
        $this->dm = $dm;
61 737
        $this->uow = $uow;
62 737
    }
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 524
    public function prepareInsertData($document)
71
    {
72 524
        $class = $this->dm->getClassMetadata(get_class($document));
73 524
        $changeset = $this->uow->getDocumentChangeSet($document);
74
75 524
        $insertData = array();
76 524
        foreach ($class->fieldMappings as $mapping) {
77
78 524
            $new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
79
80 524
            if ($new === null && $mapping['nullable']) {
81 154
                $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 524
            if ($new === null) {
88 366
                continue;
89
            }
90
91
            // @Field, @String, @Date, etc.
92 524
            if ( ! isset($mapping['association'])) {
93 524
                $insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);
94
95
            // @ReferenceOne
96 417
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
97 83
                $insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);
98
99
            // @EmbedOne
100 393
            } 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 371
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
107 371
                    && $mapping['strategy'] !== ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET && ! $new->isEmpty()) {
108 524
                $insertData[$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
109
            }
110
        }
111
112
        // add discriminator if the class has one
113 523 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 523
        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 214
    public function prepareUpdateData($document)
129
    {
130 214
        $class = $this->dm->getClassMetadata(get_class($document));
131 214
        $changeset = $this->uow->getDocumentChangeSet($document);
132
133 214
        $updateData = array();
134 214
        foreach ($changeset as $fieldName => $change) {
135 213
            $mapping = $class->fieldMappings[$fieldName];
136
137
            // skip non embedded document identifiers
138 213
            if ( ! $class->isEmbeddedDocument && ! empty($mapping['id'])) {
139 2
                continue;
140
            }
141
142 212
            list($old, $new) = $change;
143
144
            // Scalar fields
145 212
            if ( ! isset($mapping['association'])) {
146 115
                if ($new === null && $mapping['nullable'] !== true) {
147 1
                    $updateData['$unset'][$mapping['name']] = true;
148
                } else {
149 115 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 112
                        $operator = '$set';
154 112
                        $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
155
                    }
156
157 115
                    $updateData[$operator][$mapping['name']] = $value;
158
                }
159
160
            // @EmbedOne
161 135
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
162
                // If we have a new embedded document then lets set the whole thing
163 27
                if ($new && $this->uow->isScheduledForInsert($new)) {
164 9
                    $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 27
                            $updateData[$cmd][$mapping['name'] . '.' . $key] = $value;
176
                        }
177
                    }
178
                }
179
180
            // @ReferenceMany, @EmbedMany
181 118
            } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) {
182 109
                if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) {
183 12
                    $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
                    $updateData['$unset'][$mapping['name']] = true;
189
                    $this->uow->unscheduleCollectionDeletion($old);
190 97
                } elseif ($mapping['association'] === ClassMetadata::EMBED_MANY) {
191 60
                    foreach ($new as $key => $embeddedDoc) {
192 52
                        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 109
                                    $updateData[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value;
197
                                }
198
                            }
199
                        }
200
                    }
201
                }
202
203
            // @ReferenceOne
204 15
            } 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 212
                    $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 214
        foreach ($this->uow->getScheduledCollections($document) as $coll) {
215 96
            $mapping = $coll->getMapping();
216 96
            if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($coll)) {
217 2
                $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($coll, true);
218 94 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
                $updateData['$unset'][$mapping['name']] = true;
220 96
                $this->uow->unscheduleCollectionDeletion($coll);
221
            }
222
            // @ReferenceMany is handled by CollectionPersister
223
        }
224 214
        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 86
    public function prepareUpsertData($document)
234
    {
235 86
        $class = $this->dm->getClassMetadata(get_class($document));
236 86
        $changeset = $this->uow->getDocumentChangeSet($document);
237
238 86
        $updateData = array();
239 86
        foreach ($changeset as $fieldName => $change) {
240 86
            $mapping = $class->fieldMappings[$fieldName];
241
242 86
            list($old, $new) = $change;
243
244
            // Scalar fields
245 86
            if ( ! isset($mapping['association'])) {
246 86
                if ($new !== null || $mapping['nullable'] === true) {
247 86 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 86
                        $operator = '$set';
252 86
                        $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
253
                    }
254
255 86
                    $updateData[$operator][$mapping['name']] = $value;
256
                }
257
258
            // @EmbedOne
259 25
            } 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 6
                if ($new && $this->uow->isScheduledForInsert($new)) {
263 3
                    $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 6
                            $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 86
                $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 86 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 86
        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 205
    public function prepareReferencedDocumentValue(array $referenceMapping, $document)
310
    {
311 205
        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 184
    public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false)
335
    {
336 184
        $embeddedDocumentValue = array();
337 184
        $class = $this->dm->getClassMetadata(get_class($embeddedDocument));
338
339 184
        foreach ($class->fieldMappings as $mapping) {
340
            // Skip notSaved fields
341 182
            if ( ! empty($mapping['notSaved'])) {
342 1
                continue;
343
            }
344
345
            // Inline ClassMetadataInfo::getFieldValue()
346 182
            $rawValue = $class->reflFields[$mapping['fieldName']]->getValue($embeddedDocument);
347
348 182
            $value = null;
349
350 182
            if ($rawValue !== null) {
351 179
                switch (isset($mapping['association']) ? $mapping['association'] : null) {
352
                    // @Field, @String, @Date, etc.
353 179
                    case null:
354 173
                        $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
355 173
                        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 182
            if ($value === null && $mapping['nullable'] === false) {
381 62
                continue;
382
            }
383
384 179
            $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 184 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 184 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 184
        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 180
        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 223
    public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false)
462
    {
463 223
        $mapping = $coll->getMapping();
464 223
        $pb = $this;
465 223
        $callback = isset($mapping['embedded'])
466
            ? function($v) use ($pb, $mapping, $includeNestedCollections) {
467 123
                return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
468 126
            }
469
            : function($v) use ($pb, $mapping) { return $pb->prepareReferencedDocumentValue($mapping, $v); };
470
471 223
        $setData = $coll->map($callback)->toArray();
472 223
        if (CollectionHelper::isList($mapping['strategy'])) {
473 201
            $setData = array_values($setData);
474
        }
475
476 223
        $this->uow->unscheduleCollectionDeletion($coll);
477 223
        $this->uow->unscheduleCollectionUpdate($coll);
478
479 223
        return $setData;
480
    }
481
}
482