AssociationCache::setRegion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Relations;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use InvalidArgumentException;
8
use LaravelDoctrine\Fluent\Buildable;
9
10
class AssociationCache implements Buildable
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $region;
16
17
    /**
18
     * @var string
19
     */
20
    protected $usage;
21
22
    /**
23
     * @var array
24
     */
25
    protected $usages = [
26
        'READ_ONLY'            => ClassMetadataInfo::CACHE_USAGE_READ_ONLY,
27
        'NONSTRICT_READ_WRITE' => ClassMetadataInfo::CACHE_USAGE_NONSTRICT_READ_WRITE,
28
        'READ_WRITE'           => ClassMetadataInfo::CACHE_USAGE_READ_WRITE,
29
    ];
30
31
    /**
32
     * @var string
33
     */
34
    protected $field;
35
36
    /**
37
     * @var ClassMetadata
38
     */
39
    protected $metadata;
40
41
    /**
42
     * @param ClassMetadata $metadata
43
     * @param string        $field
44
     * @param string|int    $usage
45
     * @param string|null   $region
46
     */
47 23
    public function __construct(ClassMetadata $metadata, $field, $usage = 'READ_ONLY', $region = null)
48
    {
49 23
        $this->field    = $field;
50 23
        $this->metadata = $metadata;
51 23
        $this->setRegion($region);
52 23
        $this->setUsage($usage);
53 18
    }
54
55
    /**
56
     * @return string
57
     */
58 17
    public function getUsage()
59
    {
60 17
        return $this->usage;
61
    }
62
63
    /**
64
     * @param string $usage
65
     *
66
     * @throws InvalidArgumentException
67
     * @return AssociationCache
68
     */
69 23
    public function setUsage($usage)
70
    {
71 23
        if (is_int($usage)) {
72 10
            $this->validate($usage, $this->usages);
73 10
        } else {
74 13
            $this->validate($usage, array_keys($this->usages));
75 8
            $usage = $this->usages[$usage];
76
        }
77
78 18
        $this->usage = $usage;
79
80 18
        return $this;
81
    }
82
83
    /**
84
     * @return string
85
     */
86 16
    public function getRegion()
87
    {
88 16
        return $this->region;
89
    }
90
91
    /**
92
     * @param string $region
93
     *
94
     * @return AssociationCache
95
     */
96 23
    public function setRegion($region)
97
    {
98 23
        $this->region = $region;
99
100 23
        return $this;
101
    }
102
103
    /**
104
     * Execute the build process
105
     */
106 15
    public function build()
107
    {
108 15
        $this->metadata->enableAssociationCache($this->field, [
109 15
            'usage'  => $this->getUsage(),
110 15
            'region' => $this->getRegion(),
111 15
        ]);
112 15
    }
113
114
    /**
115
     * @param string|int $usage
116
     * @param array      $usages
117
     *
118
     * @return mixed
119
     */
120 23
    protected function validate($usage, array $usages)
121
    {
122 23
        if (!in_array($usage, $usages)) {
123 5
            throw new InvalidArgumentException(
124 5
                '[' . $usage . '] is not a valid cache usage. Available: ' . implode(', ', array_keys($this->usages))
125 5
            );
126
        }
127
128 18
        return $usage;
129
    }
130
}
131