Failed Conditions
Pull Request — master (#7842)
by
unknown
09:15
created

ToManyAssociationMetadata::getOrderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\PersistentCollection;
11
12
class ToManyAssociationMetadata extends AssociationMetadata
13
{
14
    /** @var string[] */
15
    private $orderBy = [];
16
17
    /** @var string|null */
18
    private $indexedBy;
19
20
    /**
21
     * @param mixed[] $orderBy
22
     */
23 20
    public function setOrderBy(array $orderBy) : void
24
    {
25 20
        $this->orderBy = $orderBy;
26 20
    }
27
28
    /**
29
     * @return mixed[]
30
     */
31 299
    public function getOrderBy() : array
32
    {
33 299
        return $this->orderBy;
34
    }
35
36 61
    public function setIndexedBy(?string $indexedBy = null) : void
37
    {
38 61
        $this->indexedBy = $indexedBy;
39 61
    }
40
41 291
    public function getIndexedBy() : ?string
42
    {
43 291
        return $this->indexedBy;
44
    }
45
46
    /**
47
     * @param object                         $owner
48
     * @param Collection|array|object[]|null $collection
49
     */
50 742
    public function wrap($owner, $collection, EntityManagerInterface $entityManager) : PersistentCollection
51
    {
52 742
        if ($collection instanceof PersistentCollection) {
53
            if ($collection->getOwner() === $owner) {
54
                return $collection;
55
            }
56
57
            $collection = $collection->getValues();
58
        }
59
60
        // If $value is not a Collection then use an ArrayCollection.
61 742
        if (! $collection instanceof Collection) {
62
            // @todo guilhermeblanco Conceptually, support to custom collections by replacing ArrayCollection creation.
63 549
            $collection = new ArrayCollection((array) $collection);
64
        }
65
66
        // Inject PersistentCollection
67 742
        $targetClass = $entityManager->getClassMetadata($this->getTargetEntity());
68 742
        $collection  = new PersistentCollection($entityManager, $targetClass, $collection);
69
70 742
        $collection->setOwner($owner, $this);
71 742
        $collection->setDirty(! $collection->isEmpty());
72 742
        $collection->setInitialized(true);
73
74 742
        return $collection;
75
    }
76
}
77