ApcQueryLocator::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Koriym\QueryLocator;
6
7
use Koriym\QueryLocator\Exception\ReadOnlyException;
8
use ReturnTypeWillChange;
9
use function is_string;
10
11
final class ApcQueryLocator implements QueryLocatorInterface
12
{
13
    /**
14
     * @var QueryLocator
15
     */
16
    private $query;
17
18
    /**
19
     * Cache prefix
20
     *
21
     * @var string
22
     */
23
    private $nameSpace;
24
25
        public function __construct(string $sqlDir, string $nameSpace)
26
    {
27
        $this->nameSpace = $nameSpace . '-';
28
        $this->query = new QueryLocator($sqlDir);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function get(string $queryName) : string
35
    {
36
        $sqlId = $this->nameSpace . $queryName;
37
        /** @var ?string $sql */
38
        $sql = apcu_fetch($sqlId);
39
        if (is_string($sql)) {
40
            return $sql;
41
        }
42
        $sql = $this->query->get($queryName);
43
        apcu_store($sqlId, $sql);
44
45
        return $sql;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getCountQuery(string $queryName) : string
52
    {
53
        $sqlId = $this->nameSpace . $queryName;
54
        /** @var ?string $sql */
55
        $sql = apcu_fetch($sqlId);
56
        if (is_string($sql)) {
57
            return $sql;
58
        }
59
        $sql = $this->query->getCountQuery($queryName);
60
        apcu_store($sqlId, $sql);
61
62
        return $sql;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    #[ReturnTypeWillChange]
69
    public function offsetExists($offset)
70
    {
71
        assert(is_string($offset));
72
        return (bool) $this->get($offset);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    #[ReturnTypeWillChange]
79
    public function offsetGet($offset)
80
    {
81
        assert(is_string($offset));
82
        return $this->get($offset);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    #[ReturnTypeWillChange]
89
    public function offsetSet($offset, $value)
90
    {
91
        throw new ReadOnlyException('not supported');
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    #[ReturnTypeWillChange]
98
    public function offsetUnset($offset)
99
    {
100
        throw new ReadOnlyException('not supported');
101
    }
102
}
103