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

ApcuStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 6
c 1
b 0
f 1
dl 0
loc 22
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 3
A saveIfNotExists() 0 3 1
A saveCompareAndSwap() 0 3 1
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\RateLimiter\Storage;
6
7
use Yiisoft\Yii\RateLimiter\Exception\CannotUseException;
8
9
final class ApcuStorage implements StorageInterface
10
{
11
    public function __construct()
12
    {
13
        if (!extension_loaded('apcu') || ini_get('apc.enabled') === '0') {
14
            throw new CannotUseException('APCu extension is not loaded or not enabled.');
15
        }
16
    }
17
18
    public function saveIfNotExists(string $key, int $value, int $ttl): bool
19
    {
20
        return (bool)apcu_add($key, $value, $ttl);
21
    }
22
23
    public function saveCompareAndSwap(string $key, int $oldValue, int $newValue, int $ttl): bool
24
    {
25
        return  (bool)apcu_cas($key, $oldValue, $newValue);
26
    }
27
28
    public function get(string $key): mixed
29
    {
30
        return apcu_fetch($key);
31
    }
32
}
33