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

Styling   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 68.08%

Importance

Changes 0
Metric Value
wmc 17
eloc 45
dl 0
loc 112
ccs 32
cts 47
cp 0.6808
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A circular256Color() 0 12 2
A circularNoColor() 0 3 1
A assertSymbols() 0 4 2
A message() 0 3 1
A circularColor() 0 12 2
A makeStyles() 0 9 3
A spinner() 0 3 1
A assertStyles() 0 7 3
A errorMsg() 0 3 1
A __construct() 0 7 1
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