Passed
Push — master ( b9880b...e1bb9e )
by Guilherme
09:04
created

CacheMetadataBuilder::__construct()   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 $entityClassMetadata;
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
34 20
        return $this;
35
    }
36
37 20
    public function withEntityClassMetadata(Mapping\ClassMetadata $entityClassMetadata) : CacheMetadataBuilder
38
    {
39 20
        $this->entityClassMetadata = $entityClassMetadata;
40
41 20
        return $this;
42
    }
43
44 15
    public function withFieldName(?string $fieldName) : CacheMetadataBuilder
45
    {
46 15
        $this->fieldName = $fieldName;
47
48 15
        return $this;
49
    }
50
51 20
    public function withCacheAnnotation(Annotation\Cache $cacheAnnotation) : CacheMetadataBuilder
52
    {
53 20
        $this->cacheAnnotation = $cacheAnnotation;
54
55 20
        return $this;
56
    }
57
58 20
    public function build() : Mapping\CacheMetadata
59
    {
60
        // Validate required fields
61 20
        assert($this->entityClassMetadata !== null);
62 20
        assert($this->cacheAnnotation !== null);
63
64 20
        $entityClassName = $this->entityClassMetadata->getRootClassName();
65 20
        $baseRegion      = strtolower(str_replace('\\', '_', $entityClassName));
66 20
        $defaultRegion   = $baseRegion . ($this->fieldName ? '__' . $this->fieldName : '');
67
68 20
        $usage  = constant(sprintf('%s::%s', Mapping\CacheUsage::class, strtoupper($this->cacheAnnotation->usage)));
69 20
        $region = $this->cacheAnnotation->region ?: $defaultRegion;
70
71 20
        return new Mapping\CacheMetadata($usage, $region);
72
    }
73
}