Failed Conditions
Push — master ( 148895...b07393 )
by Guilherme
09:59
created

ClassMetadataBuilder::getClassName()   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 4
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
11
class ClassMetadataBuilder
12
{
13
    /** @var Mapping\ClassMetadataBuildingContext */
14
    private $metadataBuildingContext;
15
16
    /** @var CacheMetadataBuilder */
17
    protected $cacheMetadataBuilder;
18
19
    /** @var string */
20
    private $className;
21
22
    /** @var Mapping\ComponentMetadata */
23
    private $parentMetadata;
24
25
    /** @var Annotation\Cache|null */
26
    protected $cacheAnnotation;
27
28
    public function __construct(
29
        Mapping\ClassMetadataBuildingContext $metadataBuildingContext,
30
        ?CacheMetadataBuilder $cacheMetadataBuilder = null
31
    ) {
32
        $this->metadataBuildingContext = $metadataBuildingContext;
33
        $this->cacheMetadataBuilder    = $cacheMetadataBuilder ?: new CacheMetadataBuilder($metadataBuildingContext);
34
    }
35
36
    public function getClassName(string $className) : ClassMetadataBuilder
37
    {
38
        $this->className = $className;
39
40
        return $this;
41
    }
42
43
    public function withParentMetadata(Mapping\ComponentMetadata $parentMetadata) : ClassMetadataBuilder
44
    {
45
        $this->parentMetadata = $parentMetadata;
46
47
        return $this;
48
    }
49
50
    public function withCacheAnnotation(?Annotation\Cache $cacheAnnotation) : ClassMetadataBuilder
51
    {
52
        $this->cacheAnnotation = $cacheAnnotation;
53
54
        if ($cacheAnnotation !== null) {
55
            $this->cacheMetadataBuilder->withCacheAnnotation($cacheAnnotation);
56
        }
57
58
        return $this;
59
    }
60
61
    public function build() : Mapping\ClassMetadata
62
    {
63
        assert($this->className !== null);
64
65
        $reflectionService = $this->metadataBuildingContext->getReflectionService();
66
        $reflectionClass   = $reflectionService->getClass($this->className);
67
        $className         = $reflectionClass ? $reflectionClass->getName() : $this->className;
68
69
        $classMetadata = new Mapping\ClassMetadata($className, $this->parentMetadata);
70
71
        $this->buildCache($classMetadata);
72
73
        return $classMetadata;
74
    }
75
76
    protected function buildCache(Mapping\ClassMetadata $classMetadata) : void
77
    {
78
        if ($this->cacheAnnotation !== null) {
79
            $classMetadata->setCache($this->cacheMetadataBuilder->build());
80
        }
81
    }
82
}
83