Completed
Pull Request — master (#5800)
by Herberto
10:37
created

CollectionFactory::createDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Doctrine\ORM\Mapping\Factory;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\ORMInvalidArgumentException;
9
use Doctrine\ORM\PersistentCollection;
10
11
class CollectionFactory
12
{
13
    /**
14
     * This method creates a custom collection for the entity defined by ClassMetadata $class.
15
     * If no custom collection is specified, it will fallback to PersistentCollection.
16
     * The custom collection must extend from PersistentCollection.
17
     *
18
     * @param EntityManagerInterface $em
19
     * @param ClassMetadata          $class
20
     * @param Collection             $collection the contents of the specific collection to create
21
     *
22
     * @return PersistentCollection
23
     */
24 494
    public function create(EntityManagerInterface $em, ClassMetadata $class, Collection $collection)
25
    {
26 494
        $customCollectionClassName = $class->customCollectionClassName;
27
28 494
        if (empty($customCollectionClassName)) {
29 485
            $customCollectionClassName = PersistentCollection::class;
30 9
        } elseif (! is_a($customCollectionClassName, PersistentCollection::class, true)) {
31 3
            throw new ORMInvalidArgumentException(
32 3
                'The custom collection specified for entity ' . $class->getName()
33 3
                . ' (' . $customCollectionClassName . ') must extend ' . PersistentCollection::class . '.'
34
            );
35
        }
36
37 491
        return new $customCollectionClassName($em, $class, $collection);
38
    }
39
40
    /**
41
     * This method creates the default collection to be used in the UnitOfWork.
42
     *
43
     * @param EntityManagerInterface $em
44
     * @param ClassMetadata          $class
45
     * @param Collection             $collection the contents of the specific collection to create
46
     *
47
     * @return PersistentCollection
48
     */
49 772
    public function createDefault(EntityManagerInterface $em, ClassMetadata $class, Collection $collection)
50
    {
51 772
        return new PersistentCollection($em, $class, $collection);
52
    }
53
}
54