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

ToOneAssociationMetadataBuilder::buildPrimaryKey()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.1535

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
nc 6
nop 1
dl 0
loc 32
ccs 11
cts 17
cp 0.6471
crap 9.1535
rs 8.8333
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\Cache\Exception\NonCacheableEntityAssociation;
9
use Doctrine\ORM\Mapping;
10
use function count;
11
use function in_array;
12
13
abstract class ToOneAssociationMetadataBuilder extends AssociationMetadataBuilder
14
{
15
    /** @var JoinColumnMetadataBuilder */
16
    protected $joinColumnMetadataBuilder;
17
18
    /** @var Annotation\Id|null */
19
    protected $idAnnotation;
20
21
    /** @var Annotation\JoinColumn|null */
22
    protected $joinColumnAnnotation;
23
24
    /** @var Annotation\JoinColumns|null */
25
    protected $joinColumnsAnnotation;
26
27 237
    public function __construct(
28
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext,
29
        ?JoinColumnMetadataBuilder $joinColumnMetadataBuilder = null,
30
        ?CacheMetadataBuilder $cacheMetadataBuilder = null
31
    ) {
32 237
        parent::__construct($metadataBuildingContext, $cacheMetadataBuilder);
33
34 237
        $this->joinColumnMetadataBuilder = $joinColumnMetadataBuilder ?: new JoinColumnMetadataBuilder($metadataBuildingContext);
35 237
    }
36
37 237
    public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : AssociationMetadataBuilder
38
    {
39 237
        parent::withComponentMetadata($componentMetadata);
40
41 237
        $this->joinColumnMetadataBuilder->withComponentMetadata($componentMetadata);
42
43 237
        return $this;
44
    }
45
46 237
    public function withFieldName(string $fieldName) : AssociationMetadataBuilder
47
    {
48 237
        parent::withFieldName($fieldName);
49
50 237
        $this->joinColumnMetadataBuilder->withFieldName($fieldName);
51
52 237
        return $this;
53
    }
54
55 237
    public function withIdAnnotation(?Annotation\Id $idAnnotation) : ToOneAssociationMetadataBuilder
56
    {
57 237
        $this->idAnnotation = $idAnnotation;
58
59 237
        return $this;
60
    }
61
62 237
    public function withJoinColumnAnnotation(?Annotation\JoinColumn $joinColumnAnnotation) : ToOneAssociationMetadataBuilder
63
    {
64 237
        $this->joinColumnAnnotation = $joinColumnAnnotation;
65
66 237
        return $this;
67
    }
68
69 237
    public function withJoinColumnsAnnotation(?Annotation\JoinColumns $joinColumnsAnnotation) : ToOneAssociationMetadataBuilder
70
    {
71 237
        $this->joinColumnsAnnotation = $joinColumnsAnnotation;
72
73 237
        return $this;
74
    }
75
76 237
    protected function buildPrimaryKey(Mapping\AssociationMetadata $associationMetadata) : void
77
    {
78 237
        if ($this->idAnnotation !== null) {
79 43
            if ($associationMetadata->isOrphanRemoval()) {
80
                throw Mapping\MappingException::illegalOrphanRemovalOnIdentifierAssociation(
81
                    $this->componentMetadata->getClassName(),
82
                    $this->fieldName
83
                );
84
            }
85
86 43
            if (! $associationMetadata->isOwningSide()) {
87
                throw Mapping\MappingException::illegalInverseIdentifierAssociation(
88
                    $this->componentMetadata->getClassName(),
89
                    $this->fieldName
90
                );
91
            }
92
93 43
            if ($this->componentMetadata->getCache() !== null && $associationMetadata->getCache() === null) {
94 2
                throw NonCacheableEntityAssociation::fromEntityAndField(
95 2
                    $this->componentMetadata->getClassName(),
96 2
                    $this->fieldName
97
                );
98
            }
99
100
            // @todo guilhermeblanco The below block of code modifies component metadata properties, and it should be moved
101
            //                       to the component metadata builder instead of here.
102
103 41
            if (! in_array($this->fieldName, $this->componentMetadata->identifier, true)) {
104 41
                $this->componentMetadata->identifier[] = $this->fieldName;
105
            }
106
107 41
            $associationMetadata->setPrimaryKey(true);
108
        }
109 235
    }
110
111 232
    protected function buildJoinColumns(Mapping\ToOneAssociationMetadata $associationMetadata) : void
112
    {
113 232
        switch (true) {
114 232
            case $this->joinColumnsAnnotation !== null:
115 19
                foreach ($this->joinColumnsAnnotation->value as $joinColumnAnnotation) {
116 19
                    $this->joinColumnMetadataBuilder->withJoinColumnAnnotation($joinColumnAnnotation);
117
118 19
                    $associationMetadata->addJoinColumn($this->joinColumnMetadataBuilder->build());
119
                }
120
121
                // Prevent currently unsupported scenario: association with multiple columns and being marked as primary
122 19
                if ($associationMetadata->isPrimaryKey() && count($associationMetadata->getJoinColumns()) > 1) {
123
                    throw Mapping\MappingException::cannotMapCompositePrimaryKeyEntitiesAsForeignId(
124
                        $this->componentMetadata->getClassName(),
125
                        $associationMetadata->getTargetEntity(),
126
                        $this->fieldName
127
                    );
128
                }
129
130 19
                break;
131
132 225
            case $this->joinColumnAnnotation !== null:
133 158
                $this->joinColumnMetadataBuilder->withJoinColumnAnnotation($this->joinColumnAnnotation);
134
135 158
                $associationMetadata->addJoinColumn($this->joinColumnMetadataBuilder->build());
136 158
                break;
137
138
            default:
139 76
                $joinColumnMetadata = $this->joinColumnMetadataBuilder->build();
0 ignored issues
show
Unused Code introduced by
The assignment to $joinColumnMetadata is dead and can be removed.
Loading history...
140
141 76
                $associationMetadata->addJoinColumn($this->joinColumnMetadataBuilder->build());
142 76
                break;
143
        }
144 232
    }
145
}
146