GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Memory::acquire()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 16
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WyriHaximus\React\Mutex;
6
7
use React\Cache\ArrayCache;
8
use React\Cache\CacheInterface;
9
use React\Promise\PromiseInterface;
10
use WyriHaximus\React\Mutex\Contracts\LockInterface;
11
use WyriHaximus\React\Mutex\Contracts\MutexInterface;
12 1
13
use function bin2hex;
14 1
use function random_bytes;
15 1
use function React\Promise\resolve;
16
use function WyriHaximus\React\timedPromise;
17
18 1
final class Memory implements MutexInterface
19 1
{
20
    private const NO_MORE_ATTEMPTS_LEFT = 0;
21 1
    private const RANDOM_BYTES_LENGTH   = 13;
22
    private CacheInterface $locks;
23
24
    public function __construct()
25
    {
26
        $this->locks = new ArrayCache();
27
    }
28
29
    public function acquire(string $key, float $ttl): PromiseInterface
30
    {
31
        /**
32
         * @phpstan-ignore-next-line
33
         * @psalm-suppress TooManyTemplateParams
34
         */
35
        return $this->locks->has($key)->then(function (bool $has) use ($key, $ttl): ?Lock {
36
            if ($has) {
37
                return null;
38
            }
39
40
            $rng  = bin2hex(random_bytes(self::RANDOM_BYTES_LENGTH));
41
            $lock = new Lock($key, $rng);
42
            $this->locks->set($key, $lock, $ttl);
43
44
            return $lock;
45
        });
46
    }
47
48
    public function spin(string $key, float $ttl, int $attempts, float $interval): PromiseInterface
49
    {
50
        /**
51
         * @phpstan-ignore-next-line
52
         */
53
        return $this->acquire($key, $ttl)->then(function (?LockInterface $lock) use ($key, $ttl, $attempts, $interval): PromiseInterface {
54
            if ($lock instanceof LockInterface || $attempts === self::NO_MORE_ATTEMPTS_LEFT) {
55
                return resolve($lock);
56
            }
57
58
            return timedPromise($interval, [$key, $ttl, --$attempts, $interval])->then(function (array $args): PromiseInterface {
59
                return $this->spin(...$args);
60
            });
61
        });
62
    }
63
64
    public function release(LockInterface $lock): PromiseInterface
65
    {
66
        /**
67
         * @psalm-suppress TooManyTemplateParams
68
         */
69
        return $this->locks->has($lock->key())->then(function (bool $has) use ($lock) {
70
            return $has ? $this->locks->get($lock->key()) : $has;
71
        })->then(
72
            /** @phpstan-ignore-next-line */
73
            function (?Lock $storedLock) use ($lock) {
74
                if ($storedLock instanceof Lock && $storedLock->rng() === $lock->rng()) {
75
                    return $this->locks->delete($lock->key());
76
                }
77
78
                return ! ($storedLock instanceof Lock) || $storedLock->rng() === $lock->rng();
79
            }
80
        );
81
    }
82
}
83