Failed Conditions
Push — master ( a3e53b...559253 )
by Guilherme
14:58
created

EmbeddedMetadataBuilder::withEmbeddedAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
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 assert;
10
use function class_exists;
11
use function interface_exists;
12
13
class EmbeddedMetadataBuilder
14
{
15
    /** @var Mapping\ClassMetadataBuildingContext */
16
    protected $metadataBuildingContext;
17
18
    /** @var ValueGeneratorMetadataBuilder */
19
    private $valueGeneratorMetadataBuilder;
20
21
    /** @var Mapping\ClassMetadata */
22
    protected $componentMetadata;
23
24
    /** @var string */
25
    protected $fieldName;
26
27
    /** @var Annotation\Embedded */
28
    private $embeddedAnnotation;
29
30
    /** @var Annotation\Id|null */
31
    private $idAnnotation;
32
33 408
    public function __construct(
34
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext,
35
        ?ValueGeneratorMetadataBuilder $valueGeneratorMetadataBuilder = null
36
    ) {
37 408
        $this->metadataBuildingContext       = $metadataBuildingContext;
38 408
        $this->valueGeneratorMetadataBuilder = $valueGeneratorMetadataBuilder ?: new ValueGeneratorMetadataBuilder($metadataBuildingContext);
39 408
    }
40
41 407
    public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : EmbeddedMetadataBuilder
42
    {
43 407
        $this->componentMetadata = $componentMetadata;
44
45 407
        return $this;
46
    }
47
48 407
    public function withFieldName(string $fieldName) : EmbeddedMetadataBuilder
49
    {
50 407
        $this->fieldName = $fieldName;
51
52 407
        return $this;
53
    }
54
55
    public function withEmbeddedAnnotation(Annotation\Embedded $embeddedAnnotation) : EmbeddedMetadataBuilder
56
    {
57
        $this->embeddedAnnotation = $embeddedAnnotation;
58
59
        return $this;
60
    }
61
62 407
    public function withIdAnnotation(?Annotation\Id $idAnnotation) : EmbeddedMetadataBuilder
63
    {
64 407
        $this->idAnnotation = $idAnnotation;
65
66 407
        return $this;
67
    }
68
69
    public function build() : Mapping\EmbeddedMetadata
70
    {
71
        // Validate required fields
72
        assert($this->componentMetadata !== null);
73
        assert($this->embeddedAnnotation !== null);
74
        assert($this->fieldName !== null);
75
76
        $componentClassName = $this->componentMetadata->getClassName();
77
        $embeddedMetadata   = new Mapping\EmbeddedMetadata($this->fieldName);
78
79
        $embeddedMetadata->setSourceEntity($componentClassName);
80
        $embeddedMetadata->setTargetEntity($this->getTargetEmbedded($this->embeddedAnnotation->class));
81
82
        if (! empty($this->embeddedAnnotation->columnPrefix)) {
83
            $embeddedMetadata->setColumnPrefix($this->embeddedAnnotation->columnPrefix);
84
        }
85
86
        if ($this->idAnnotation !== null) {
87
            $embeddedMetadata->setPrimaryKey(true);
88
        }
89
90
        return $embeddedMetadata;
91
    }
92
93
    /**
94
     * Attempts to resolve target embedded.
95
     *
96
     * @param string $targetEmbedded The proposed target embedded
97
     *
98
     * @return string The processed target embedded
99
     *
100
     * @throws Mapping\MappingException If a target embedded is not valid.
101
     */
102
    protected function getTargetEmbedded(string $targetEmbedded) : string
103
    {
104
        // Validate if target embedded is defined
105
        if (! $targetEmbedded) {
106
            throw Mapping\MappingException::missingEmbeddedClass($this->fieldName);
107
        }
108
109
        // Validate that target entity exists
110
        if (! (class_exists($targetEmbedded) || interface_exists($targetEmbedded))) {
111
            throw Mapping\MappingException::invalidTargetEmbeddedClass(
112
                $targetEmbedded,
113
                $this->componentMetadata->getClassName(),
114
                $this->fieldName
115
            );
116
        }
117
118
        return $targetEmbedded;
119
    }
120
}