Failed Conditions
Push — master ( 4bfa22...148895 )
by Guilherme
17:21 queued 09:56
created

withOneToManyAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Mapping\Builder;
6
7
use Doctrine\ORM\Annotation;
8
use Doctrine\ORM\Mapping;
9
use function array_merge;
10
use function array_unique;
11
use function assert;
12
13
class OneToManyAssociationMetadataBuilder extends ToManyAssociationMetadataBuilder
14
{
15
    /** @var Annotation\OneToMany */
16
    private $oneToManyAnnotation;
17
18 118
    public function withOneToManyAnnotation(Annotation\OneToMany $oneToManyAnnotation) : OneToManyAssociationMetadataBuilder
19
    {
20 118
        $this->oneToManyAnnotation = $oneToManyAnnotation;
21
22 118
        return $this;
23
    }
24
25
    /**
26
     * @internal Association metadata order of definition settings is important.
27
     */
28 118
    public function build() : Mapping\OneToManyAssociationMetadata
29
    {
30
        // Validate required fields
31 118
        assert($this->componentMetadata !== null);
32 118
        assert($this->oneToManyAnnotation !== null);
33 118
        assert($this->fieldName !== null);
34
35 118
        if (empty($this->oneToManyAnnotation->mappedBy)) {
36
            throw Mapping\MappingException::oneToManyRequiresMappedBy($this->fieldName);
37
        }
38
39 118
        $componentClassName  = $this->componentMetadata->getClassName();
40 118
        $associationMetadata = new Mapping\OneToManyAssociationMetadata($this->fieldName);
41
42 118
        $associationMetadata->setSourceEntity($componentClassName);
43 118
        $associationMetadata->setTargetEntity($this->getTargetEntity($this->oneToManyAnnotation->targetEntity));
44 118
        $associationMetadata->setCascade($this->getCascade($this->oneToManyAnnotation->cascade));
45 118
        $associationMetadata->setFetchMode($this->getFetchMode($this->oneToManyAnnotation->fetch));
46 118
        $associationMetadata->setOwningSide(false);
47 118
        $associationMetadata->setMappedBy($this->oneToManyAnnotation->mappedBy);
48
49 118
        if (! empty($this->oneToManyAnnotation->indexBy)) {
50 11
            $associationMetadata->setIndexedBy($this->oneToManyAnnotation->indexBy);
51
        }
52
53 118
        if ($this->oneToManyAnnotation->orphanRemoval) {
54 19
            $associationMetadata->setOrphanRemoval($this->oneToManyAnnotation->orphanRemoval);
55
56
            // Orphan removal also implies a cascade remove
57 19
            $associationMetadata->setCascade(array_unique(array_merge($associationMetadata->getCascade(), ['remove'])));
58
        }
59
60 118
        if ($this->orderByAnnotation !== null) {
61 19
            $associationMetadata->setOrderBy($this->orderByAnnotation->value);
62
        }
63
64 118
        $this->buildCache($associationMetadata);
65
66 118
        return $associationMetadata;
67
    }
68
}
69