Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

LoopSetup::setup()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 8
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlecRabbit\Spinner\Core;
7
8
use AlecRabbit\Spinner\Core\Contract\IDriver;
9
use AlecRabbit\Spinner\Core\Contract\ILoopSetup;
10
use AlecRabbit\Spinner\Core\Contract\Loop\Contract\ILoop;
11
use AlecRabbit\Spinner\Core\Settings\Contract\ILoopSettings;
12
use Traversable;
13
14
final class LoopSetup implements ILoopSetup
15
{
16
    public function __construct(
17
        protected ILoop $loop,
18
        protected ILoopSettings $settings,
19
    ) {
20
    }
21
22
    public function setup(IDriver $driver): void
23
    {
24
        if ($this->settings->isLoopAvailable()) {
25
            if ($this->settings->isAutoStartEnabled()) {
26
                $this->registerAutoStart();
27
            }
28
            if ($this->settings->isSignalProcessingAvailable() && $this->settings->isAttachHandlersEnabled()) {
29
                $this->registerSignalHandlers($driver);
30
            }
31
        }
32
    }
33
34
    private function registerAutoStart(): void
35
    {
36
        $this->loop->autoStart();
37
    }
38
39
    private function registerSignalHandlers(IDriver $driver): void
40
    {
41
        foreach ($this->signalHandlers($driver) as $signal => $handler) {
42
            $this->loop->onSignal($signal, $handler);
0 ignored issues
show
Bug introduced by
$handler of type array<integer,callable> is incompatible with the type Closure expected by parameter $closure of AlecRabbit\Spinner\Core\...tract\ILoop::onSignal(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $this->loop->onSignal($signal, /** @scrutinizer ignore-type */ $handler);
Loading history...
43
        }
44
    }
45
46
    private function signalHandlers(IDriver $driver): Traversable
47
    {
48
        yield from [
49
            // @codeCoverageIgnoreStart
50
            SIGINT => function () use ($driver): void {
51
                $driver->interrupt(PHP_EOL . 'SIGINT' . PHP_EOL); // todo: test
52
                $this->loop->stop();
53
            },
54
            // @codeCoverageIgnoreEnd
55
        ];
56
    }
57
}
58