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   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 61
ccs 3
cts 8
cp 0.375
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A release() 0 15 5
A acquire() 0 16 2
A __construct() 0 3 1
A spin() 0 12 3
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