Failed Conditions
Push — master ( c35142...f185a6 )
by Guilherme
08:59
created

CacheMetadataBuilder::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
use function constant;
11
use function sprintf;
12
use function str_replace;
13
use function strtolower;
14
use function strtoupper;
15
16
class CacheMetadataBuilder
17
{
18
    /** @var Mapping\ClassMetadataBuildingContext */
19
    private $metadataBuildingContext;
20
21
    /** @var Mapping\ClassMetadata */
22
    private $componentMetadata;
23
24
    /** @var string|null */
25
    private $fieldName;
26
27
    /** @var Annotation\Cache */
28
    private $cacheAnnotation;
29
30 20
    public function __construct(Mapping\ClassMetadataBuildingContext $metadataBuildingContext)
31
    {
32 20
        $this->metadataBuildingContext = $metadataBuildingContext;
33 20
    }
34
35 20
    public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : CacheMetadataBuilder
36
    {
37 20
        $this->componentMetadata = $componentMetadata;
38
39 20
        return $this;
40
    }
41
42 15
    public function withFieldName(?string $fieldName) : CacheMetadataBuilder
43
    {
44 15
        $this->fieldName = $fieldName;
45
46 15
        return $this;
47
    }
48
49 20
    public function withCacheAnnotation(Annotation\Cache $cacheAnnotation) : CacheMetadataBuilder
50
    {
51 20
        $this->cacheAnnotation = $cacheAnnotation;
52
53 20
        return $this;
54
    }
55
56 20
    public function build() : Mapping\CacheMetadata
57
    {
58
        // Validate required fields
59 20
        assert($this->componentMetadata !== null);
60 20
        assert($this->cacheAnnotation !== null);
61
62 20
        $componentName = $this->componentMetadata->getRootClassName();
63 20
        $baseRegion    = strtolower(str_replace('\\', '_', $componentName));
64 20
        $defaultRegion = $baseRegion . ($this->fieldName ? '__' . $this->fieldName : '');
65
66 20
        $usage  = constant(sprintf('%s::%s', Mapping\CacheUsage::class, strtoupper($this->cacheAnnotation->usage)));
67 20
        $region = $this->cacheAnnotation->region ?: $defaultRegion;
68
69 20
        return new Mapping\CacheMetadata($usage, $region);
70
    }
71
}