Completed
Push — master ( 61f8e0...b1cbaf )
by Maciej
13:57
created

PersistenceBuilder::prepareEmbeddedDocumentValue()   F

Complexity

Conditions 22
Paths 541

Size

Total Lines 97
Code Lines 44

Duplication

Lines 23
Ratio 23.71 %

Code Coverage

Tests 47
CRAP Score 22.1045
Metric Value
dl 23
loc 97
ccs 47
cts 50
cp 0.94
rs 2.6561
cc 22
eloc 44
nc 541
nop 3
crap 22.1045

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;
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 681
    public function __construct(DocumentManager $dm, UnitOfWork $uow)
59
    {
60 681
        $this->dm = $dm;
61 681
        $this->uow = $uow;
62 681
    }
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 482
    public function prepareInsertData($document)
71
    {
72 482
        $class = $this->dm->getClassMetadata(get_class($document));
73 482
        $changeset = $this->uow->getDocumentChangeSet($document);
74
75 482
        $insertData = array();
76 482
        foreach ($class->fieldMappings as $mapping) {
77
78 482
            $new = isset($changeset[$mapping['fieldName']][1]) ? $changeset[$mapping['fieldName']][1] : null;
79
80 482
            if ($new === null && $mapping['nullable']) {
81 146
                $insertData[$mapping['name']] = null;
82 146
            }
83
84
            /* Nothing more to do for null values, since we're either storing
85
             * them (if nullable was true) or not.
86
             */
87 482
            if ($new === null) {
88 337
                continue;
89
            }
90
91
            // @Field, @String, @Date, etc.
92 482
            if ( ! isset($mapping['association'])) {
93 482
                $insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);
94
95
            // @ReferenceOne
96 482
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
97 79
                $insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);
98
99
            // @EmbedOne
100 393
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
101 62
                $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 369
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
107 347
                    && $mapping['strategy'] !== ClassMetadataInfo::STORAGE_STRATEGY_ADD_TO_SET && ! $new->isEmpty()) {
108 192
                $insertData[$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
109 192
            }
110 482
        }
111
112
        // add discriminator if the class has one
113 481 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 32
            $insertData[$class->discriminatorField] = isset($class->discriminatorValue)
115 32
                ? $class->discriminatorValue
116 32
                : $class->name;
117 32
        }
118
119 481
        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 212
    public function prepareUpdateData($document)
129
    {
130 212
        $class = $this->dm->getClassMetadata(get_class($document));
131 212
        $changeset = $this->uow->getDocumentChangeSet($document);
132
133 212
        $updateData = array();
134 212
        foreach ($changeset as $fieldName => $change) {
135 212
            $mapping = $class->fieldMappings[$fieldName];
136
137
            // skip non embedded document identifiers
138 212
            if ( ! $class->isEmbeddedDocument && ! empty($mapping['id'])) {
139 1
                continue;
140
            }
141
142 212
            list($old, $new) = $change;
143
144
            // Scalar fields
145 212
            if ( ! isset($mapping['association'])) {
146 116
                if ($new === null && $mapping['nullable'] !== true) {
147 1
                    $updateData['$unset'][$mapping['name']] = true;
148 1
                } else {
149 116 View Code Duplication
                    if ($new !== null && $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 4
                    } 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 212
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
162
                // If we have a new embedded document then lets set the whole thing
163 26
                if ($new && $this->uow->isScheduledForInsert($new)) {
164 8
                    $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 26
                } elseif ( ! $new) {
168 3
                    $updateData['$unset'][$mapping['name']] = true;
169
170
                // Update existing embedded document
171 3 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 14
                            $updateData[$cmd][$mapping['name'] . '.' . $key] = $value;
176 14
                        }
177 18
                    }
178
                }
179
180
            // @ReferenceMany, @EmbedMany
181 138
            } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) {
182 113
                if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) {
183 20
                    $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
184 113 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 95
                } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($old)) {
188 2
                    $updateData['$unset'][$mapping['name']] = true;
189 2
                    $this->uow->unscheduleCollectionDeletion($old);
190 93
                } elseif ($mapping['association'] === ClassMetadata::EMBED_MANY) {
191 57
                    foreach ($new as $key => $embeddedDoc) {
192 49
                        if ( ! $this->uow->isScheduledForInsert($embeddedDoc)) {
193 38
                            $update = $this->prepareUpdateData($embeddedDoc);
194 38
                            foreach ($update as $cmd => $values) {
195 14
                                foreach ($values as $name => $value) {
196 14
                                    $updateData[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value;
197 14
                                }
198 38
                            }
199 38
                        }
200 57
                    }
201 57
                }
202
203
            // @ReferenceOne
204 121
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
205 10 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 10
                    $updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
207 10
                } else {
208 2
                    $updateData['$unset'][$mapping['name']] = true;
209
                }
210 10
            }
211 212
        }
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 212
        foreach ($this->uow->getScheduledCollections($document) as $coll) {
215 91
            $mapping = $coll->getMapping();
216 91
            if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($coll)) {
217 3
                $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($coll, true);
218 91 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 1
                $this->uow->unscheduleCollectionDeletion($coll);
221 1
            }
222
            // @ReferenceMany is handled by CollectionPersister
223 212
        }
224 212
        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 77
    public function prepareUpsertData($document)
234
    {
235 77
        $class = $this->dm->getClassMetadata(get_class($document));
236 77
        $changeset = $this->uow->getDocumentChangeSet($document);
237
238 77
        $updateData = array();
239 77
        foreach ($changeset as $fieldName => $change) {
240 77
            $mapping = $class->fieldMappings[$fieldName];
241
242 77
            list($old, $new) = $change;
243
244
            // Scalar fields
245 77
            if ( ! isset($mapping['association'])) {
246 77
                if ($new !== null || $mapping['nullable'] === true) {
247 77 View Code Duplication
                    if ($new !== null && $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 3
                    } else {
251 77
                        $operator = '$set';
252 77
                        $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new);
253
                    }
254
255 77
                    $updateData[$operator][$mapping['name']] = $value;
256 77
                }
257
258
            // @EmbedOne
259 77
            } 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 3
                if ($new && $this->uow->isScheduledForInsert($new)) {
263 1
                    $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
                            $updateData[$cmd][$mapping['name'] . '.' . $key] = $value;
270
                        }
271
                    }
272
                }
273
274
            // @ReferenceOne
275 22 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 12
                if (isset($new) || $mapping['nullable'] === true) {
277 9
                    $updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
278 9
                }
279
280
            // @ReferenceMany, @EmbedMany
281 21
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
282 13
                    && $new instanceof PersistentCollection && $new->isDirty()
283 13
                    && CollectionHelper::isAtomic($mapping['strategy'])) {
284 1
                $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
285 1
            }
286
            // @EmbedMany and @ReferenceMany are handled by CollectionPersister
287 77
        }
288
289
        // add discriminator if the class has one
290 77 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 5
                : $class->name;
294 5
        }
295
296 77
        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 188
    public function prepareReferencedDocumentValue(array $referenceMapping, $document)
310
    {
311 188
        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 171
    public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false)
335
    {
336 171
        $embeddedDocumentValue = array();
337 171
        $class = $this->dm->getClassMetadata(get_class($embeddedDocument));
338
339 171
        foreach ($class->fieldMappings as $mapping) {
340
            // Skip notSaved fields
341 168
            if ( ! empty($mapping['notSaved'])) {
342 1
                continue;
343
            }
344
345
            // Inline ClassMetadataInfo::getFieldValue()
346 168
            $rawValue = $class->reflFields[$mapping['fieldName']]->getValue($embeddedDocument);
347
348 168
            $value = null;
349
350 168
            if ($rawValue !== null) {
351 165
                switch (isset($mapping['association']) ? $mapping['association'] : null) {
352
                    // @Field, @String, @Date, etc.
353 165
                    case null:
354 161
                        $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue);
355 161
                        break;
356
357 64
                    case ClassMetadata::EMBED_ONE:
358 64
                    case ClassMetadata::REFERENCE_ONE:
359
                        // Nested collections should only be included for embedded relationships
360 20
                        $value = $this->prepareAssociatedDocumentValue($mapping, $rawValue, $includeNestedCollections && isset($mapping['embedded']));
361 20
                        break;
362
363 45
                    case ClassMetadata::EMBED_MANY:
364 45
                    case ClassMetadata::REFERENCE_MANY:
365
                        // Skip PersistentCollections already scheduled for deletion
366 45
                        if ( ! $includeNestedCollections && $rawValue instanceof PersistentCollection
367 45
                            && $this->uow->isCollectionScheduledForDeletion($rawValue)) {
368
                            break;
369
                        }
370
371 45
                        $value = $this->prepareAssociatedCollectionValue($rawValue, $includeNestedCollections);
372 45
                        break;
373
374
                    default:
375
                        throw new \UnexpectedValueException('Unsupported mapping association: ' . $mapping['association']);
376 165
                }
377 165
            }
378
379
            // Omit non-nullable fields that would have a null value
380 168
            if ($value === null && $mapping['nullable'] === false) {
381 60
                continue;
382
            }
383
384 165
            $embeddedDocumentValue[$mapping['name']] = $value;
385 171
        }
386
387
        /* Add a discriminator value if the embedded document is not mapped
388
         * explicitly to a targetDocument class.
389
         */
390 171 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 13
            $discriminatorField = $embeddedMapping['discriminatorField'];
392 13
            $discriminatorValue = isset($embeddedMapping['discriminatorMap'])
393 13
                ? array_search($class->name, $embeddedMapping['discriminatorMap'])
394 13
                : $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 13
            if ($discriminatorValue === false) {
403 2
                $discriminatorValue = $class->name;
404 2
            }
405
406 13
            $embeddedDocumentValue[$discriminatorField] = $discriminatorValue;
407 13
        }
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 171 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 8
                ? $class->discriminatorValue
416 8
                : $class->name;
417 8
        }
418
419
        // Ensure empty embedded documents are stored as BSON objects
420 171
        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 166
        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 20
    public function prepareAssociatedDocumentValue(array $mapping, $document, $includeNestedCollections = false)
442
    {
443 20
        if (isset($mapping['embedded'])) {
444 10
            return $this->prepareEmbeddedDocumentValue($mapping, $document, $includeNestedCollections);
445
        }
446
447 18
        if (isset($mapping['reference'])) {
448 18
            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 PersistentCollection $coll
458
     * @param bool $includeNestedCollections
459
     * @return array
460
     */
461 208
    public function prepareAssociatedCollectionValue(PersistentCollection $coll, $includeNestedCollections = false)
462
    {
463 208
        $mapping = $coll->getMapping();
464 208
        $pb = $this;
465 208
        $callback = isset($mapping['embedded'])
466
            ? function($v) use ($pb, $mapping, $includeNestedCollections) {
467 115
                return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
468
            }
469
            : function($v) use ($pb, $mapping) { return $pb->prepareReferencedDocumentValue($mapping, $v); };
470
471 208
        $setData = $coll->map($callback)->toArray();
472 208
        if (CollectionHelper::isList($mapping['strategy'])) {
473 186
            $setData = array_values($setData);
474 186
        }
475
476 208
        $this->uow->unscheduleCollectionDeletion($coll);
477 208
        $this->uow->unscheduleCollectionUpdate($coll);
478
479 208
        return $setData;
480
    }
481
}
482