Passed
Push — master ( 96250d...20e888 )
by Alec
02:06
created

Styles::messageStyles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 1
crap 12
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 Styles
16
{
17
    /** @var Circular */
18
    protected $symbolStyles;
19
    /** @var Circular */
20
    protected $messageStyles;
21
    /** @var Circular */
22
    protected $percentStyles;
23
24 10
    public function __construct(array $styles)
25
    {
26 10
        $prototype = new Prototype($styles);
27 10
        $this->symbolStyles = $prototype->getSpinnerStyles();
28 10
        $this->messageStyles = $prototype->getMessageStyles();
29 10
        $this->percentStyles = $prototype->getPercentStyles();
30
//        $this->symbolStyles = $this->symbolStyles($styles);
31
//        $this->messageStyles = $this->messageStyles($styles);
32
//        $this->percentStyles = $this->percentStyles($styles);
33 10
    }
34
35 9
    public function spinner(string $symbol): string
36
    {
37 9
        return sprintf((string)$this->symbolStyles->value(), $symbol);
38
//        return sprintf((string)$this->symbolStyles->value(), (string)$this->symbols->value());
39
    }
40
41 9
    public function message(string $message): string
42
    {
43
        return
44 9
            sprintf(
45 9
                (string)$this->messageStyles->value(),
46 9
                $message
47
            );
48
    }
49
50 9
    public function percent(string $percent): string
51
    {
52
        return
53 9
            sprintf(
54 9
                (string)$this->percentStyles->value(),
55 9
                $percent
56
            );
57
    }
58
59
    /**
60
     * @param array $styles
61
     * @return Circular
62
     */
63
    protected function symbolStyles(array $styles): Circular
64
    {
65
        $value = $styles[StylesInterface::COLOR256_SPINNER_STYLES];
66
        /** @noinspection NotOptimalIfConditionsInspection */
67
        if (($terminal = new Terminal())->supports256Color() && null !== $value) {
68
            return $this->circular256Color($value);
69
        }
70
71
        $value = $styles[StylesInterface::COLOR_SPINNER_STYLES];
72
        /** @noinspection NotOptimalIfConditionsInspection */
73
        if ($terminal->supportsColor() && null !== $value) {
74
            return $this->circularColor($value);
75
        }
76
        return $this->circularNoColor();
77
    }
78
79
    protected function circular256Color(array $styles): Circular
80
    {
81
        return
82
            new Circular(
83
                array_map(
84
                    static function (string $value): string {
85
                        return ConsoleColor::ESC_CHAR . "[38;5;{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
86
                    },
87
                    $styles
88
                )
89
            );
90
    }
91
92
    protected function circularColor(array $styles): Circular
93
    {
94
        return
95
            new Circular(
96
                array_map(
97
                    static function (string $value): string {
98
                        return ConsoleColor::ESC_CHAR . "[{$value}m%s" . ConsoleColor::ESC_CHAR . '[0m';
99
                    },
100
                    $styles
101
                )
102
            );
103
    }
104
105
    protected function circularNoColor(): Circular
106
    {
107
        return new Circular(['%s',]);
108
    }
109
110
    /**
111
     * @param array $styles
112
     * @return Circular
113
     */
114
    protected function messageStyles(array $styles): Circular
115
    {
116
//        if (!\array_key_exists(StylesInterface::COLOR_MESSAGE_STYLES, $styles)) {
117
//            $styles[StylesInterface::COLOR_MESSAGE_STYLES] = StylesInterface::DEFAULT_MESSAGE_STYLES;
118
//        }
119
        if ((new Terminal())->supportsColor()) {
120
            $value = $styles[StylesInterface::COLOR_MESSAGE_STYLES];
121
            if (null === $value) {
122
                return $this->circularNoColor();
123
            }
124
            return $this->circularColor($value);
125
        }
126
        return $this->circularNoColor();
127
    }
128
129
    /**
130
     * @param array $styles
131
     * @return Circular
132
     */
133
    protected function percentStyles(array $styles): Circular
134
    {
135
//        if (!\array_key_exists(StylesInterface::COLOR_PERCENT_STYLES, $styles)) {
136
//            $styles[StylesInterface::COLOR_PERCENT_STYLES] = StylesInterface::DEFAULT_PERCENT_STYLES;
137
//        }
138
        if ((new Terminal())->supportsColor()) {
139
            $value = $styles[StylesInterface::COLOR_PERCENT_STYLES];
140
            if (null === $value) {
141
                return $this->circularNoColor();
142
            }
143
            return $this->circularColor($value);
144
        }
145
        return $this->circularNoColor();
146
    }
147
}
148