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

SimpleCacheStorage::get()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 8.8333
cc 7
nc 3
nop 1
crap 56
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