Completed
Pull Request — master (#1219)
by Maciej
09:29 queued 03:09
created

DefaultPersistentCollectionFactory::create()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 6
nop 3
crap 4
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
20
namespace Doctrine\ODM\MongoDB\PersistentCollection;
21
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\Common\Collections\Collection as BaseCollection;
24
use Doctrine\ODM\MongoDB\DocumentManager;
25
use Doctrine\ODM\MongoDB\PersistentCollection;
26
27
/**
28
 * Default factory class for persistent collection classes.
29
 *
30
 * @since  1.1
31
 * @author Maciej Malarz <[email protected]>
32
 */
33
final class DefaultPersistentCollectionFactory implements PersistentCollectionFactory
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38 382
    public function create(DocumentManager $dm, array $mapping, BaseCollection $coll = null)
39
    {
40 382
        if ($coll === null) {
41 253
            $coll = ! empty($mapping['collectionClass'])
42 253
                    ? new $mapping['collectionClass']
43 253
                    : new ArrayCollection();
44 253
        }
45 382
        if (empty($mapping['collectionClass'])) {
46 382
            return new PersistentCollection($coll, $dm, $dm->getUnitOfWork());
47
        }
48
49 2
        $generator = $dm->getConfiguration()->getPersistentCollectionGenerator();
50 2
        $className = $generator->loadClass($mapping['collectionClass'], $dm->getConfiguration()->getAutoGeneratePersistentCollectionClasses());
51
52 2
        return new $className($coll, $dm, $dm->getUnitOfWork());
53
    }
54
}
55