Test Failed
Pull Request — master (#43)
by
unknown
02:22
created

SimpleCacheStorage::saveCompareAndSwap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\RateLimiter\Storage;
6
7
use InvalidArgumentException;
8
use Psr\SimpleCache\CacheInterface;
9
10
final class SimpleCacheStorage implements StorageInterface
11 10
{
12
    public function __construct(private CacheInterface $cache)
13 10
    {
14
    }
15 6
16
    public function saveIfNotExists(string $key, mixed $value, int $ttl): bool
17 6
    {
18
        return $this->cache->set($key, $value, $ttl);
19
    }
20 7
21
    public function saveCompareAndSwap(string $key, mixed $oldValue, mixed $newValue, int $ttl): bool
22 7
    {
23
        return $this->cache->set($key, $newValue, $ttl);
24
    }
25
26
    public function get(string $key): ?float
27
    {
28
        $value = $this->cache->get($key);
29
        if (!is_int($value) && !is_float($value) && $value !== false && $value !== null) {
30
            throw new InvalidArgumentException('The value is not supported by SimpleCacheStorage, it must be int, float or null.');
31
        }
32
33
        if ($value === false || $value === null) {
34
            $value = null;
35
        } else {
36
            $value = (float)$value;
37
        }
38
        return $value;
39
    }
40
}
41