Passed
Push — master ( 3cda3f...c35142 )
by Guilherme
09:24
created

TableMetadataBuilder::withEntityClassMetadata()   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 assert;
10
11
class TableMetadataBuilder
12
{
13
    /** @var Mapping\ClassMetadataBuildingContext */
14
    private $metadataBuildingContext;
15
16
    /** @var Mapping\ClassMetadata */
17
    private $entityClassMetadata;
18
19
    /** @var Annotation\Table */
20
    private $tableAnnotation;
21
22 406
    public function __construct(Mapping\ClassMetadataBuildingContext $metadataBuildingContext)
23
    {
24 406
        $this->metadataBuildingContext = $metadataBuildingContext;
25
26 406
        return $this;
27
    }
28
29 406
    public function withEntityClassMetadata(Mapping\ClassMetadata $entityClassMetadata) : TableMetadataBuilder
30
    {
31 406
        $this->entityClassMetadata = $entityClassMetadata;
32
33 406
        return $this;
34
    }
35
36 225
    public function withTableAnnotation(Annotation\Table $tableAnnotation) : TableMetadataBuilder
37
    {
38 225
        $this->tableAnnotation = $tableAnnotation;
39
40 225
        return $this;
41
    }
42
43 406
    public function build() : Mapping\TableMetadata
44
    {
45
        // Validate required fields
46 406
        assert($this->entityClassMetadata !== null);
47
48 406
        $namingStrategy = $this->metadataBuildingContext->getNamingStrategy();
49 406
        $tableMetadata  = new Mapping\TableMetadata();
50
51 406
        $tableMetadata->setName($namingStrategy->classToTableName($this->entityClassMetadata->getClassName()));
52
53 406
        if ($this->tableAnnotation === null) {
54 191
            return $tableMetadata;
55
        }
56
57 225
        if (! empty($this->tableAnnotation->name)) {
58 198
            $tableMetadata->setName($this->tableAnnotation->name);
59
        }
60
61 225
        if (! empty($this->tableAnnotation->schema)) {
62 7
            $tableMetadata->setSchema($this->tableAnnotation->schema);
63
        }
64
65 225
        foreach ($this->tableAnnotation->options as $optionName => $optionValue) {
66 7
            $tableMetadata->addOption($optionName, $optionValue);
67
        }
68
69
        /** @var Annotation\Index $indexAnnotation */
70 225
        foreach ($this->tableAnnotation->indexes as $indexAnnotation) {
71 17
            $tableMetadata->addIndex([
72 17
                'name'    => $indexAnnotation->name,
73 17
                'columns' => $indexAnnotation->columns,
74 17
                'unique'  => $indexAnnotation->unique,
75 17
                'options' => $indexAnnotation->options,
76 17
                'flags'   => $indexAnnotation->flags,
77
            ]);
78
        }
79
80
        /** @var Annotation\UniqueConstraint $uniqueConstraintAnnotation */
81 225
        foreach ($this->tableAnnotation->uniqueConstraints as $uniqueConstraintAnnotation) {
82 9
            $tableMetadata->addUniqueConstraint([
83 9
                'name'    => $uniqueConstraintAnnotation->name,
84 9
                'columns' => $uniqueConstraintAnnotation->columns,
85 9
                'options' => $uniqueConstraintAnnotation->options,
86 9
                'flags'   => $uniqueConstraintAnnotation->flags,
87
            ]);
88
        }
89
90 225
        return $tableMetadata;
91
    }
92
}