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.
Completed
Push — master ( edb7c5...140456 )
by Cees-Jan
05:19
created

Cron::createWithMutex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React;
4
5
use React\EventLoop\LoopInterface;
6
use WyriHaximus\React\Mutex\Memory;
7
use WyriHaximus\React\Mutex\MutexInterface;
8
9
final class Cron
10
{
11
    /** @var ActionInterface[] */
12
    private $actions;
13
14
    /** @var MutexInterface */
15
    private $mutex;
16
17
    /**
18
     * @param SchedulerInterface $scheduler
19
     * @param MutexInterface     $mutex
20
     * @param ActionInterface[]  $actions
21
     */
22 8
    private function __construct(SchedulerInterface $scheduler, MutexInterface $mutex, ActionInterface ...$actions)
23
    {
24 8
        $this->actions = $actions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $actions of type array<integer,array<inte...eact\ActionInterface>>> is incompatible with the declared type array<integer,object<Wyr...React\ActionInterface>> of property $actions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25 8
        $this->mutex = $mutex;
26
27
        $scheduler->schedule(function (): void {
28 8
            $this->tick();
29 8
        });
30 8
    }
31
32 2
    public static function create(LoopInterface $loop, ActionInterface ...$actions)
33
    {
34 2
        return new self(new Scheduler($loop), new Memory(), ...$actions);
35
    }
36
37 2
    public static function createHighPrecision(LoopInterface $loop, ActionInterface ...$actions)
38
    {
39 2
        return new self(new HighPrecisionScheduler($loop), new Memory(), ...$actions);
40
    }
41
42 2
    public static function createWithMutex(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions)
43
    {
44 2
        return new self(new Scheduler($loop), $mutex, ...$actions);
45
    }
46
47 2
    public static function createHighPrecisionWithMutex(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions)
48
    {
49 2
        return new self(new HighPrecisionScheduler($loop), $mutex, ...$actions);
50
    }
51
52 8
    private function tick(): void
53
    {
54 8
        foreach ($this->actions as $action) {
55 8
            $this->perform($action);
56
        }
57 8
    }
58
59 8
    private function perform(ActionInterface $action): void
60
    {
61 8
        if ($action->isDue() === false) {
62
            return;
63
        }
64
65
        $this->mutex->acquire($action->getKey())->then(function ($lock) use ($action) {
66 8
            if ($lock === false) {
67
                return;
68
            }
69
70
            return $action->perform()->then(function () use ($action, $lock) {
71 8
                return $this->mutex->release($lock);
72 8
            });
73 8
        })->done();
74 8
    }
75
}
76