Passed
Push — master ( 0658f3...f968a7 )
by y
02:18
created

SimpleCachePool::_has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Helix\Asana\Api;
4
5
use Helix\Asana\Api;
6
use Helix\Asana\Base\AbstractEntity;
7
use Helix\Asana\Base\AbstractEntity\ImmutableInterface;
8
use Helix\Asana\Base\Data;
9
use Psr\SimpleCache\CacheException;
10
use Psr\SimpleCache\CacheInterface as PSR16;
11
12
/**
13
 * Adapts a `PSR-16 SimpleCache` instance to the runtime entity pool.
14
 *
15
 * Concurrency locks can be implemented by extending this class.
16
 *
17
 * @see https://www.php-fig.org/psr/psr-16/
18
 *
19
 * @see FileCache
20
 */
21
class SimpleCachePool extends Pool {
22
23
    const IMMUTABLE_TTL = 3153600000;
24
25
    /**
26
     * @var PSR16
27
     */
28
    protected $psr;
29
30
    /**
31
     * @var int
32
     */
33
    protected $ttl = 3600;
34
35
    /**
36
     * @param PSR16 $psr
37
     */
38
    public function __construct (PSR16 $psr) {
39
        $this->psr = $psr;
40
    }
41
42
    /**
43
     * @param AbstractEntity $entity
44
     * @throws CacheException
45
     */
46
    protected function _add (AbstractEntity $entity): void {
47
        $this->psr->set("asana/{$entity->getGid()}", $entity, $this->_getTtl($entity));
48
        parent::_add($entity);
49
    }
50
51
    /**
52
     * @param AbstractEntity $entity
53
     * @param string[] $keys
54
     * @throws CacheException
55
     */
56
    protected function _addKeys (AbstractEntity $entity, ...$keys): void {
57
        $gid = $entity->getGid();
58
        $ttl = $this->_getTtl($entity);
59
        foreach ($keys as $key) {
60
            if ($key !== $gid) {
61
                $this->psr->set("asana/{$key}", $gid, $ttl);
62
            }
63
        }
64
        parent::_addKeys($entity, ...$keys);
65
    }
66
67
    /**
68
     * @param string $key
69
     * @param Api|Data $caller
70
     * @return null|AbstractEntity
71
     * @throws CacheException
72
     */
73
    protected function _get (string $key, $caller) {
74
        /** @var AbstractEntity $entity */
75
        if (!$entity = parent::_get($key, $caller) and $entity = $this->psr->get("asana/{$key}")) {
76
            if (is_string($entity)) { // gid ref
0 ignored issues
show
introduced by
The condition is_string($entity) is always false.
Loading history...
77
                if (!$entity = $this->_get($entity, $caller)) {
78
                    $this->psr->delete("asana/{$key}"); // bad ref
79
                }
80
                return $entity;
81
            }
82
            parent::_add($entity); // pool before hydration to make circular references safe.
83
            $entity->__construct($caller, $entity->toArray()); // hydrate
84
            parent::_addKeys($entity, $key, ...$entity->getPoolKeys());
85
        }
86
        return $entity;
87
    }
88
89
    protected function _getTtl (AbstractEntity $entity): int {
90
        if ($entity instanceof ImmutableInterface) {
91
            return self::IMMUTABLE_TTL;
92
        }
93
        return $this->ttl;
94
    }
95
96
    /**
97
     * @param string $key
98
     * @return bool
99
     * @throws CacheException
100
     */
101
    protected function _has (string $key): bool {
102
        return parent::_has($key) or $this->psr->has("asana/{$key}");
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getTtl (): int {
109
        return $this->ttl;
110
    }
111
112
    /**
113
     * @param string[] $keys
114
     * @throws CacheException
115
     */
116
    public function remove (array $keys): void {
117
        parent::remove($keys);
118
        foreach ($keys as $key) {
119
            $this->psr->delete("asana/{$key}");
120
        }
121
    }
122
123
    /**
124
     * @param int $ttl
125
     * @return $this
126
     */
127
    public function setTtl (int $ttl) {
128
        $this->ttl = $ttl;
129
        return $this;
130
    }
131
}