Failed Conditions
Pull Request — master (#7898)
by Guilherme
63:09
created

ToOneAssociationMetadataBuilder::buildPrimaryKey()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9.5839

Importance

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