Passed
Push — master ( fdbeea...8d24ca )
by Alec
01:54
created

Driver::moveBackSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Snake\Core;
6
7
use AlecRabbit\Snake\Contracts\Color;
8
9
class Driver
10
{
11
    public const HIDE_CURSOR_SEQ = "\033[?25l";
12
    public const SHOW_CURSOR_SEQ = "\033[?25h";
13
14
    /** @var false|resource */
15
    private $stream = STDERR;
16
17
    /** @var int */
18
    private $colorLevel;
19
20 2
    public function __construct(int $colorLevel)
21
    {
22 2
        $this->setColorLevel($colorLevel);
23 1
    }
24
25
    /**
26
     * @param int $colorLevel
27
     */
28 2
    public function setColorLevel(int $colorLevel): void
29
    {
30 2
        if (!in_array($colorLevel, Color::ALLOWED, true)) {
31 1
            throw new \InvalidArgumentException('Unknown color level.');
32
        }
33 1
        $this->colorLevel = $colorLevel;
34 1
    }
35
36 1
    public function moveBackSequence(): string
37
    {
38 1
        return "\033[1D";
39
    }
40
41 1
    public function eraseSequence(): string
42
    {
43 1
        return "\033[1X";
44
    }
45
46 1
    public function frameSequence(int $fg, string $char): string
47
    {
48 1
        if (Color::COLOR_256 === $this->colorLevel) {
49
            return "\033[38;5;{$fg}m{$char}\033[0m";
50
        }
51 1
        if (Color::COLOR_16 === $this->colorLevel) {
52
            return "\033[96m{$char}\033[0m";
53
        }
54 1
        return $char;
55
    }
56
57 1
    public function hideCursor(): void
58
    {
59 1
        $this->write(self::HIDE_CURSOR_SEQ);
60 1
    }
61
62
    /**
63
     * @codeCoverageIgnore
64
     *
65
     * @param string ...$text
66
     */
67
    public function write(string ...$text): void
68
    {
69
        foreach ($text as $s) {
70
            if (false === $this->stream) {
71
                echo $s;
72
            } elseif (false === @fwrite($this->stream, $s)) {
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type true; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

72
            } elseif (false === @fwrite(/** @scrutinizer ignore-type */ $this->stream, $s)) {
Loading history...
73
                // should never happen
74
                throw new \RuntimeException('Unable to write stream.');
75
            }
76
        }
77
        if (false !== $this->stream) {
78
            fflush($this->stream);
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type true; however, parameter $handle of fflush() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

78
            fflush(/** @scrutinizer ignore-type */ $this->stream);
Loading history...
79
        }
80
    }
81
82 1
    public function showCursor(): void
83
    {
84 1
        $this->write(self::SHOW_CURSOR_SEQ);
85 1
    }
86
87
    /**
88
     * @codeCoverageIgnore
89
     */
90
    public function disableStdErr(): void
91
    {
92
        $this->stream = false;
93
    }
94
}
95