Passed
Push — master ( 9a8f24...ec7c88 )
by Alec
02:07
created

Terminal::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
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 AlecRabbit\Cli\Tools;
4
5
use AlecRabbit\Cli\Tools\Core\Contracts\TerminalInterface;
6
use AlecRabbit\Cli\Tools\Core\TerminalStatic;
7
use const AlecRabbit\ALLOWED_COLOR_TERMINAL;
8
9
class Terminal implements TerminalInterface
10
{
11
    protected const MIN_WIDTH = 20;
12
    protected const MAX_WIDTH = 280;
13
    protected const MIN_HEIGHT = 20;
14
    protected const MAX_HEIGHT = 80;
15
16
    /** @var int */
17
    protected $width;
18
    /** @var int */
19
    protected $height;
20
    /** @var int */
21
    protected $color;
22
23 14
    public function __construct(int $width = null, int $height = null, int $colorSupport = null)
24
    {
25 14
        $this->width = $this->refineWidth($width);
26 13
        $this->height = $this->refineHeight($height);
27 12
        $this->color = $this->refineColorSupport($colorSupport);
28 11
    }
29
30
    /**
31
     * @param int $width
32
     * @return int
33
     */
34 14
    protected function refineWidth(?int $width): int
35
    {
36 14
        $this->assertWidth($width);
37 13
        return $width ?? TerminalStatic::width();
38
    }
39
40 14
    protected function assertWidth(?int $width): void
41
    {
42 14
        if (null !== $width && !$this->inRange($width, static::MIN_WIDTH, static::MAX_WIDTH)) {
43 1
            throw new \RuntimeException('Terminal size bounds exceeded.');
44
        }
45 13
    }
46
47
    /**
48
     * @param int $height
49
     * @return int
50
     */
51 13
    protected function refineHeight(?int $height): int
52
    {
53 13
        $this->assertHeight($height);
54 12
        return $height ?? TerminalStatic::height();
55
    }
56
57 13
    protected function assertHeight(?int $height): void
58
    {
59 13
        if (null !== $height && !$this->inRange($height, static::MIN_HEIGHT, static::MAX_HEIGHT)) {
60 1
            throw new \RuntimeException('Terminal size bounds exceeded.');
61
        }
62 12
    }
63
64
    /**
65
     * @param int $colorSupport
66
     * @return int
67
     */
68 12
    protected function refineColorSupport(?int $colorSupport): int
69
    {
70 12
        $this->assertColorSupport($colorSupport);
71 11
        return $colorSupport ?? TerminalStatic::colorSupport();
72
    }
73
74
    /**
75
     * @param int $colorSupport
76
     */
77 12
    protected function assertColorSupport(?int $colorSupport): void
78
    {
79 12
        if (null !== $colorSupport && !\in_array($colorSupport, ALLOWED_COLOR_TERMINAL, true)) {
80 1
            throw new \RuntimeException('Unknown color support level.');
81
        }
82 11
    }
83
84
    /**
85
     * @return int
86
     */
87 4
    public function width(): int
88
    {
89 4
        return $this->width;
90
    }
91
92
    /**
93
     * @return int
94
     */
95 4
    public function height(): int
96
    {
97 4
        return $this->height;
98
    }
99
100
    /**
101
     * @return int
102
     */
103 4
    public function color(): int
104
    {
105 4
        return $this->color;
106
    }
107
108
    /**
109
     * @param int $value
110
     * @param int $min
111
     * @param int $max
112
     * @return bool
113
     */
114 13
    protected function inRange(int $value, int $min, int $max): bool
115
    {
116 13
        if ($min > $max) {
117 1
            [$min, $max] = [$max, $min];
118
        }
119 13
        return ($min <= $value && $value <= $max);
120
    }
121
}
122