KeyboardCancelMiddleware::invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\Loop\LoopMiddleware;
15
16
use PhpProfiler\Lib\Console\EchoBackCanceller;
17
use PhpProfiler\Lib\Loop\LoopMiddlewareInterface;
18
19
final class KeyboardCancelMiddleware implements LoopMiddlewareInterface
20
{
21
    /** @var resource */
22
    private $keyboard_input;
23
24
    public function __construct(
25
        private string $cancel_key,
26
        private EchoBackCanceller $echo_back_canceller,
27
        private LoopMiddlewareInterface $chain
28
    ) {
29
        $this->keyboard_input = fopen('php://stdin', 'r');
30
        stream_set_blocking($this->keyboard_input, false);
31
    }
32
33
    public function invoke(): bool
34
    {
35
        $key = fread($this->keyboard_input, 1);
36
        if ($key === $this->cancel_key) {
37
            return false;
38
        }
39
        return $this->chain->invoke();
40
    }
41
}
42