Passed
Push — master ( dbaa53...0b91ad )
by Alec
02:46
created

Styling   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 71.6%

Importance

Changes 0
Metric Value
wmc 30
eloc 75
dl 0
loc 178
c 0
b 0
f 0
ccs 58
cts 81
cp 0.716
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A circular256Color() 0 9 1
A circularNoColor() 0 3 1
A message() 0 6 1
A assertSymbols() 0 5 2
A percentStyles() 0 13 4
A symbolStyles() 0 14 5
A circularColor() 0 9 1
A percent() 0 6 1
A spinner() 0 3 1
B assertStyles() 0 22 7
A errorMsg() 0 3 1
A __construct() 0 9 1
A messageStyles() 0 13 4
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
use AlecRabbit\Spinner\Contracts\StylesInterface;
9
10
/**
11
 * Class Styling
12
 *
13
 * @internal
14
 */
15
class Styling
16
{
17
    public const MAX_SYMBOLS_COUNT = 50;
18
19
    /** @var Circular */
20
    protected $symbolStyles;
21
    /** @var Circular */
22
    protected $messageStyles;
23
    /** @var Circular */
24
    protected $percentStyles;
25
    /** @var Circular */
26
    protected $symbols;
27
28 10
    public function __construct(array $symbols, array $styles)
29
    {
30
//        dump($symbols, $styles);
31 10
        $this->assertSymbols($symbols);
32 10
        $this->assertStyles($styles);
33 10
        $this->symbols = new Circular($symbols);
34 10
        $this->symbolStyles = $this->symbolStyles($styles);
35 10
        $this->messageStyles = $this->messageStyles($styles);
36 10
        $this->percentStyles = $this->percentStyles($styles);
37 10
    }
38
39 10
    protected function assertSymbols(array $symbols): void
40
    {
41 10
        if (self::MAX_SYMBOLS_COUNT < count($symbols)) {
42
            throw new \InvalidArgumentException(
43
                'Symbols array is too big [limit: ' . self::MAX_SYMBOLS_COUNT . '].'
44
            );
45
        }
46 10
    }
47
48 10
    protected function assertStyles(array $styles): void
49
    {
50 10
        if (!\array_key_exists(StylesInterface::COLOR256_SPINNER_STYLES, $styles)) {
51
            throw new \InvalidArgumentException(
52
                $this->errorMsg('Styles array does not have', 'COLOR256_STYLES')
53
            );
54
        }
55 10
        $value = $styles[StylesInterface::COLOR256_SPINNER_STYLES];
56 10
        if (!\is_array($value) && null !== $value) {
57
            throw new \InvalidArgumentException(
58
                $this->errorMsg('Styles should be type of array or NULL in', 'COLOR256_STYLES')
59
            );
60
        }
61 10
        if (!\array_key_exists(StylesInterface::COLOR_SPINNER_STYLES, $styles)) {
62
            throw new \InvalidArgumentException(
63
                $this->errorMsg('Styles array does not have', 'COLOR_STYLES')
64
            );
65
        }
66 10
        $value = $styles[StylesInterface::COLOR_SPINNER_STYLES];
67 10
        if (!is_array($value) && null !== $value) {
68
            throw new \InvalidArgumentException(
69
                $this->errorMsg('Styles should be type of array or NULL in', 'COLOR_SPINNER_STYLES')
70
            );
71
        }
72 10
    }
73
74
    /**
75
     * @param string $str
76
     * @param string $constant
77
     * @return string
78
     */
79
    private function errorMsg(string $str, string $constant): string
80
    {
81
        return $str . ' ' . $constant . ' key.';
82
    }
83
84
    /**
85
     * @param array $styles
86
     * @return Circular
87
     */
88 10
    protected function symbolStyles(array $styles): Circular
89
    {
90 10
        $value = $styles[StylesInterface::COLOR256_SPINNER_STYLES];
91
        /** @noinspection NotOptimalIfConditionsInspection */
92 10
        if (($terminal = new Terminal())->supports256Color() && null !== $value) {
93
            return $this->circular256Color($value);
94
        }
95
96 10
        $value = $styles[StylesInterface::COLOR_SPINNER_STYLES];
97
        /** @noinspection NotOptimalIfConditionsInspection */
98 10
        if ($terminal->supportsColor() && null !== $value) {
99 6
            return $this->circularColor($value);
100
        }
101 4
        return $this->circularNoColor();
102
    }
103
104
    protected function circular256Color(array $styles): Circular
105
    {
106
        return
107
            new Circular(
108
                array_map(
109
                    static function (string $value): string {
110
                        return ConsoleColor::ESC_CHAR . "[38;5;{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
111
                    },
112
                    $styles
113
                )
114
            );
115
    }
116
117 9
    protected function circularColor(array $styles): Circular
118
    {
119
        return
120 9
            new Circular(
121 9
                array_map(
122
                    static function (string $value): string {
123 9
                        return ConsoleColor::ESC_CHAR . "[{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
124 9
                    },
125 9
                    $styles
126
                )
127
            );
128
    }
129
130 4
    protected function circularNoColor(): Circular
131
    {
132 4
        return new Circular(['%s',]);
133
    }
134
135
    /**
136
     * @param array $styles
137
     * @return Circular
138
     */
139 10
    protected function messageStyles(array $styles): Circular
140
    {
141 10
        if (!\array_key_exists(StylesInterface::COLOR_MESSAGE_STYLES, $styles)) {
142
            $styles[StylesInterface::COLOR_MESSAGE_STYLES] = StylesInterface::DEFAULT_MESSAGE_STYLES;
143
        }
144 10
        if ((new Terminal())->supportsColor()) {
145 10
            $value = $styles[StylesInterface::COLOR_MESSAGE_STYLES];
146 10
            if (null === $value) {
147 1
                return $this->circularNoColor();
148
            }
149 9
            return $this->circularColor($value);
150
        }
151
        return $this->circularNoColor();
152
    }
153
154
    /**
155
     * @param array $styles
156
     * @return Circular
157
     */
158 10
    protected function percentStyles(array $styles): Circular
159
    {
160 10
        if (!\array_key_exists(StylesInterface::COLOR_PERCENT_STYLES, $styles)) {
161
            $styles[StylesInterface::COLOR_PERCENT_STYLES] = StylesInterface::DEFAULT_PERCENT_STYLES;
162
        }
163 10
        if ((new Terminal())->supportsColor()) {
164 10
            $value = $styles[StylesInterface::COLOR_PERCENT_STYLES];
165 10
            if (null === $value) {
166 1
                return $this->circularNoColor();
167
            }
168 9
            return $this->circularColor($value);
169
        }
170
        return $this->circularNoColor();
171
    }
172
173 9
    public function spinner(): string
174
    {
175 9
        return sprintf((string)$this->symbolStyles->value(), (string)$this->symbols->value());
176
    }
177
178 9
    public function message(string $message): string
179
    {
180
        return
181 9
            sprintf(
182 9
                (string)$this->messageStyles->value(),
183 9
                $message
184
            );
185
    }
186
187 9
    public function percent(string $percent): string
188
    {
189
        return
190 9
            sprintf(
191 9
                (string)$this->percentStyles->value(),
192 9
                $percent
193
            );
194
    }
195
}
196