Passed
Push — master ( 8b63d4...cbdc22 )
by Alec
02:08
created

Styling::assertSymbols()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.1481
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Accessories\Circular;
6
use AlecRabbit\ConsoleColour\ConsoleColor;
7
use AlecRabbit\ConsoleColour\Terminal;
8
9
/**
10
 * Class Styling
11
 */
12
class Styling
13
{
14
    public const COLOR256_SPINNER_STYLES = '256_color_spinner_styles';
15
    public const COLOR_SPINNER_STYLES = 'color_spinner_styles';
16
17
    public const COLOR256_MESSAGE_STYLES = '256_color_message_styles';
18
    public const COLOR_MESSAGE_STYLES = 'color_message_styles';
19
20
    public const MAX_SYMBOLS_COUNT = 50;
21
22
    /** @var Circular */
23
    protected $styles;
24
    /** @var string */
25
    private $message;
26
    /** @var Circular */
27
    private $symbols;
28
29 9
    public function __construct(array $symbols, array $styles, string $message)
30
    {
31 9
        $this->assertSymbols($symbols);
32 9
        $this->assertStyles($styles);
33 9
        $this->symbols = new Circular($symbols);
34 9
        $this->styles = $this->makeStyles($styles);
35 9
        $this->message = $message;
36 9
    }
37
38 9
    protected function assertSymbols(array $symbols): void
39
    {
40 9
        if (self::MAX_SYMBOLS_COUNT < count($symbols)) {
41
            throw new \InvalidArgumentException('Symbols array is too big.');
42
        }
43 9
    }
44
45 9
    protected function assertStyles(array $styles): void
46
    {
47 9
        if (!\array_key_exists(self::COLOR256_SPINNER_STYLES, $styles)) {
48
            throw new \InvalidArgumentException($this->errorMsg('COLOR256_STYLES'));
49
        }
50 9
        if (!\array_key_exists(self::COLOR_SPINNER_STYLES, $styles)) {
51
            throw new \InvalidArgumentException($this->errorMsg('COLOR_STYLES'));
52
        }
53 9
    }
54
55
    /**
56
     * @param string $constant
57
     * @return string
58
     */
59
    private function errorMsg(string $constant): string
60
    {
61
        return 'Styles array does not have ' . static::class . '::' . $constant . 'key.';
62
    }
63
64
    /**
65
     * @param array $styles
66
     * @return Circular
67
     */
68 9
    protected function makeStyles(array $styles): Circular
69
    {
70 9
        if (($terminal = new Terminal())->supports256Color()) {
71
            return $this->circular256Color($styles);
72
        }
73 9
        if ($terminal->supportsColor()) {
74 9
            return $this->circularColor($styles);
75
        }
76
        return $this->circularNoColor();
77
    }
78
79
    protected function circular256Color(array $styles): Circular
80
    {
81
        if (null === $value = $styles[self::COLOR256_SPINNER_STYLES]) {
82
            return $this->circularColor($styles);
83
        }
84
        return
85
            new Circular(
86
                array_map(
87
                    static function (string $value): string {
88
                        return ConsoleColor::ESC_CHAR . "[38;5;{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
89
                    },
90
                    $value
91
                )
92
            );
93
    }
94
95 9
    protected function circularColor(array $styles): Circular
96
    {
97 9
        if (null === $value = $styles[self::COLOR_SPINNER_STYLES]) {
98 3
            return $this->circularNoColor();
99
        }
100
        return
101 6
            new Circular(
102 6
                array_map(
103
                    static function (string $value): string {
104 6
                        return ConsoleColor::ESC_CHAR . "[{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
105 6
                    },
106 6
                    $value
107
                )
108
            );
109
    }
110
111 3
    protected function circularNoColor(): Circular
112
    {
113 3
        return new Circular(['%s',]);
114
    }
115
116 8
    public function spinner(): string
117
    {
118 8
        return sprintf((string)$this->styles->value(), (string)$this->symbols->value());
119
    }
120
121 8
    public function message(): string
122
    {
123 8
        return $this->message;
124
    }
125
}
126