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

ApcuStorage::saveIfNotExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
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