Passed
Pull Request — master (#30)
by Alexander
01:23
created

ArrayCache::normalizeTtl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Cache;
4
5
use DateInterval;
6
use DateTime;
7
use Exception;
8
use Psr\SimpleCache\CacheInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Cache\CacheInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
/**
11
 * ArrayCache provides caching for the current request only by storing the values in an array.
12
 *
13
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ArrayCache supports.
14
 */
15
final class ArrayCache implements CacheInterface
16
{
17
    public const EXPIRATION_INFINITY = 0;
18 6
19
    private $cache = [];
20 6
21
    public function get($key, $default = null)
22
    {
23 19
        if (isset($this->cache[$key]) && !$this->isExpired($key)) {
24
            $value = $this->cache[$key][0];
25 19
            if (is_object($value)) {
26 17
                $value = clone $value;
27
            }
28
29 13
            return $value;
30
        }
31
32 19
        return $default;
33
    }
34 19
35 19
    public function set($key, $value, $ttl = null): bool
36
    {
37
        $expiration = $this->ttlToExpiration($ttl);
38 2
        if ($expiration < 0) {
39
            return $this->delete($key);
40 2
        }
41 2
        if (is_object($value)) {
42
            $value = clone $value;
43
        }
44 19
        $this->cache[$key] = [$value, $expiration];
45
        return true;
46 19
    }
47 19
48
    public function delete($key): bool
49
    {
50
        unset($this->cache[$key]);
51
        return true;
52
    }
53
54
    public function clear(): bool
55
    {
56
        $this->cache = [];
57
        return true;
58
    }
59
60
    public function getMultiple($keys, $default = null)
61
    {
62
        $results = [];
63
        foreach ($keys as $key) {
64
            $value = $this->get($key, $default);
65
            $results[$key] = $value;
66
        }
67
        return $results;
68
    }
69
70
    public function setMultiple($values, $ttl = null): bool
71
    {
72
        foreach ($values as $key => $value) {
73
            $this->set($key, $value, $ttl);
74
        }
75
        return true;
76
    }
77
78
    public function deleteMultiple($keys): bool
79
    {
80
        foreach ($keys as $key) {
81
            $this->delete($key);
82
        }
83
        return true;
84
    }
85
86
    public function has($key): bool
87
    {
88
        return isset($this->cache[$key]) && !$this->isExpired($key);
89
    }
90
91
    /**
92
     * Checks whether item is expired or not
93
     * @param $key
94
     * @return bool
95
     */
96
    private function isExpired($key): bool
97
    {
98
        return $this->cache[$key][1] !== 0 && $this->cache[$key][1] <= time();
99
    }
100
101
    /**
102
     * Converts TTL to expiration
103
     * @param $ttl
104
     * @return int
105
     */
106
    protected function ttlToExpiration($ttl): int
107
    {
108
        $ttl = $this->normalizeTtl($ttl);
109
110
        if ($ttl === null) {
111
            $expiration = static::EXPIRATION_INFINITY;
112
        } elseif ($ttl <= 0) {
113
            $expiration = -1;
114
        } else {
115
            $expiration = $ttl + time();
116
        }
117
118
        return $expiration;
119
    }
120
121
    /**
122
     * Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
123
     * @param int|DateInterval|null $ttl raw TTL.
124
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
125
     */
126
    protected function normalizeTtl($ttl): ?int
127
    {
128
        if ($ttl instanceof DateInterval) {
129
            try {
130
                return (new DateTime('@0'))->add($ttl)->getTimestamp();
131
            } catch (Exception $e) {
132
                return null;
133
            }
134
        }
135
136
        return $ttl;
137
    }
138
}
139