Passed
Push — develop ( b37e90...aa265d )
by Alec
02:38
created

AbstractSpinner::getStyle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 2
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.7998
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Spinner\Core;
4
5
use AlecRabbit\Accessories\Circular;
6
use AlecRabbit\ConsoleColour\Terminal;
7
use AlecRabbit\Tools\Contracts\Strings;
8
use AlecRabbit\Tools\Spinner\Contracts\SpinnerInterface;
9
10
abstract class AbstractSpinner implements SpinnerInterface
11
{
12
    protected const PADDING_STR = ' ';
13
14
    /** @var Circular */
15
    protected $spinnerSymbols;
16
    /** @var null|Circular */
17
    protected $styles;
18
    /** @var string */
19
    protected $message;
20
    /** @var string */
21
    protected $resetStr;
22
    /** @var \Closure */
23
    protected $style;
24
25
26 5
    public function __construct(
27
        string $message = SpinnerInterface::DEFAULT_MESSAGE,
28
        string $prefix = SpinnerInterface::DEFAULT_PREFIX,
29
        string $suffix = SpinnerInterface::DEFAULT_SUFFIX
30
    ) {
31 5
        $this->spinnerSymbols = $this->getSymbols();
32 5
        $this->styles = $this->getStyles();
33
34 5
        $this->message = $this->refineStr($message, $prefix, $suffix);
35 5
        $strLen = strlen($this->message . static::PADDING_STR) + 2;
36 5
        $this->resetStr = Strings::ESC . "[{$strLen}D";
37 5
        $this->style = $this->getStyle();
38 5
    }
39
40
    /**
41
     * @return Circular
42
     */
43
    abstract protected function getSymbols(): Circular;
44
45 3
    protected function getStyles(): ?Circular
46
    {
47 3
        $terminal = new Terminal();
48 3
        if ($terminal->supports256Color()) {
49
            return new Circular([
50
                '38;5;197',
51
                '38;5;198',
52
                '38;5;199',
53
                '38;5;200',
54
                '38;5;201',
55
                '38;5;202',
56
                '38;5;203',
57
                '38;5;204',
58
                '38;5;205',
59
            ]);
60
        }
61
        // @codeCoverageIgnoreStart
62
        if ($terminal->supportsColor()) {
63
            return new Circular([
64
                '96',
65
            ]);
66
        }
67
        return null;
68
        // @codeCoverageIgnoreEnd
69
    }
70
71
    /**
72
     * @param string $str
73
     * @param string $prefix
74
     * @param string $suffix
75
     * @return string
76
     */
77 5
    protected function refineStr(string $str, string $prefix, string $suffix): string
78
    {
79 5
        return $prefix . $str . $suffix;
80
    }
81
82
    /**
83
     * @return \Closure
84
     */
85 5
    protected function getStyle(): \Closure
86
    {
87 5
        if (null === $this->styles) {
88
            return
89
                function (): string {
90 2
                    $value = (string)$this->spinnerSymbols->value();
91 2
                    return static::PADDING_STR . $value;
92 2
                };
93
        }
94
        return
95
            function (): string {
96 3
                $symbol = (string)$this->spinnerSymbols->value();
97 3
                $style = $this->styles ? (string)$this->styles->value() : '';
98
                return
99 3
                    static::PADDING_STR .
100 3
                    Strings::ESC .
101 3
                    "[{$style}m{$symbol}" .
102 3
                    Strings::ESC . '[0m';
103 3
            };
104
    }
105
106
    /** {@inheritDoc} */
107 5
    public function begin(): string
108
    {
109 5
        return $this->work() . $this->resetStr;
110
    }
111
112 5
    protected function work(): string
113
    {
114 5
        return ($this->style)() . $this->message;
115
    }
116
117
    /** {@inheritDoc} */
118 5
    public function spin(): string
119
    {
120 5
        return $this->work() . $this->resetStr;
121
    }
122
123
    /** {@inheritDoc} */
124 5
    public function end(): string
125
    {
126 5
        return $this->resetStr . Strings::ESC . '[K';
127
    }
128
}
129