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

SimpleCacheStorage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 11
c 1
b 0
f 0
dl 0
loc 29
ccs 6
cts 6
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A saveIfNotExists() 0 3 1
A __construct() 0 2 1
A saveCompareAndSwap() 0 3 1
B get() 0 13 7
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