ReactLoopAdapter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A stop() 0 3 1
A delay() 0 3 1
A repeat() 0 3 1
A onSignal() 0 3 1
A __construct() 0 3 1
A cancel() 0 12 2
A autoStart() 0 2 1
A run() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Asynchronous\React;
6
7
use AlecRabbit\Spinner\Core\Loop\Contract\A\ALoopAdapter;
8
use AlecRabbit\Spinner\Exception\InvalidArgument;
9
use Closure;
10
use React\EventLoop\LoopInterface;
11
use React\EventLoop\TimerInterface;
12
13
/**
14
 * @codeCoverageIgnore
15
 */
16
final class ReactLoopAdapter extends ALoopAdapter
17
{
18
    public function __construct(
19
        private readonly LoopInterface $loop,
20
    ) {
21
    }
22
23
    public function autoStart(): void
24
    {
25
        // ReactPHP event loop is started by its library code.
26
    }
27
28
    public function repeat(float $interval, Closure $closure): TimerInterface
29
    {
30
        return $this->loop->addPeriodicTimer($interval, $closure);
31
    }
32
33
    public function delay(float $delay, Closure $closure): void
34
    {
35
        $this->loop->addTimer($delay, $closure);
36
    }
37
38
    public function run(): void
39
    {
40
        $this->loop->run();
41
    }
42
43
    public function stop(): void
44
    {
45
        $this->loop->stop();
46
    }
47
48
    public function cancel(mixed $timer): void
49
    {
50
        if (!$timer instanceof TimerInterface) {
51
            throw new InvalidArgument(
52
                sprintf(
53
                    'Invalid timer type: %s, expected %s',
54
                    gettype($timer),
55
                    TimerInterface::class
56
                )
57
            );
58
        }
59
        $this->loop->cancelTimer($timer);
60
    }
61
62
    public function onSignal(int $signal, Closure $closure): void
63
    {
64
        $this->loop->addSignal($signal, $closure);
65
    }
66
}
67