ArrayCache::computeExpiryTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace BenTools\Currency\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
use Traversable;
7
8
class ArrayCache implements CacheInterface
9
{
10
11
    private $defaultTtl;
12
    private $storage = [];
13
    private $expiries = [];
14
15
    /**
16
     * ArrayCache constructor.
17
     * @param int|null $defaultTtl
18
     */
19
    public function __construct(int $defaultTtl = null)
20
    {
21
        $this->defaultTtl = $defaultTtl;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function get($key, $default = null)
28
    {
29
        if ($this->has($key)) {
30
            return $this->storage[$key];
31
        }
32
        if ($this->isExpired($key)) {
33
            $this->delete($key);
34
        }
35
        return $default;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function set($key, $value, $ttl = null)
42
    {
43
        $this->storage[$key] = $value;
44
        $this->expiries[$key] = $this->computeExpiryTime($ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl defined by parameter $ttl on line 41 can also be of type object<DateInterval>; however, BenTools\Currency\Cache\...he::computeExpiryTime() does only seem to accept null|integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function delete($key)
51
    {
52
        unset($this->storage[$key], $this->expiries[$key]);
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function clear()
59
    {
60
        $this->storage = [];
61
        $this->expiries = [];
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getMultiple($keys, $default = null)
68
    {
69
        foreach ($keys as $key) {
70
            yield $key => $this->get($key, $default);
71
        }
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function setMultiple($values, $ttl = null)
78
    {
79
        if (!$this->isIterable($values)) {
80
            throw new \InvalidArgumentException("Values must be array or Traversable");
81
        }
82
        foreach ($values as $key => $value) {
83
            $this->set($key, $value, $ttl);
84
        }
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function deleteMultiple($keys)
91
    {
92
        foreach ($keys as $key) {
93
            $this->delete($key);
94
        }
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function has($key)
101
    {
102
        return isset($this->storage[$key]) && !$this->isExpired($key);
103
    }
104
105
    /**
106
     * @param mixed $values
107
     * @return bool
108
     */
109
    private function isIterable($values): bool
110
    {
111
        return is_array($values) || $values instanceof Traversable;
112
    }
113
114
    /**
115
     * @param int|null $ttl
116
     * @return int|null
117
     */
118
    private function computeExpiryTime(int $ttl = null): ?int
119
    {
120
        $ttl = $ttl ?? $this->defaultTtl;
121
        if (null === $ttl) {
122
            return null;
123
        }
124
        return time() + $ttl;
125
    }
126
127
    /**
128
     * @param string $key
129
     * @return bool
130
     */
131
    private function isExpired($key): bool
132
    {
133
        if (isset($this->expiries[$key]) && null !== $this->expiries[$key]) {
134
            return time() >= $this->expiries[$key];
135
        }
136
        return false;
137
    }
138
}
139