Passed
Push — master ( 3d3552...475338 )
by Alec
02:11
created

AbstractSpinner::setFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Accessories\Pretty;
6
use AlecRabbit\ConsoleColour\ConsoleColor;
7
use AlecRabbit\Spinner\Contracts\SpinnerInterface;
8
9
abstract class AbstractSpinner implements SpinnerInterface
10
{
11
    protected const ERASING_SHIFT = 1;
12
    protected const INTERVAL = 0.1;
13
14
    /** @var string */
15
    protected $messageStr;
16
    /** @var string */
17
    protected $percentStr = '';
18
    /** @var string */
19
    protected $moveBackSequenceStr;
20
    /** @var string */
21
    protected $paddingStr;
22
    /** @var string */
23
    protected $eraseBySpacesStr;
24
    /** @var Styling */
25
    protected $styled;
26
27 9
    public function __construct(
28
        ?string $message = null,
29
        ?string $prefix = null,
30
        ?string $suffix = null,
31
        ?string $paddingStr = null
32
    ) {
33 9
        $this->paddingStr = $paddingStr ?? SpinnerInterface::PADDING_EMPTY;
34 9
        $this->messageStr = $this->refineMessage($message, $prefix, $suffix);
35 9
        $this->setFields();
36 9
        $this->styled = new Styling($this->getSymbols(), $this->getStyles());
37 9
    }
38
39
    /**
40
     * @param null|string $message
41
     * @param null|string $prefix
42
     * @param null|string $suffix
43
     * @return string
44
     */
45 9
    protected function refineMessage(?string $message, ?string $prefix, ?string $suffix): string
46
    {
47 9
        $message = ucfirst($message ?? SpinnerInterface::DEFAULT_MESSAGE);
48 9
        $prefix = empty($message) ? '' : $prefix ?? SpinnerInterface::DEFAULT_PREFIX;
49 9
        $suffix = $suffix ?? (empty($message) ? '' : SpinnerInterface::DEFAULT_SUFFIX);
50 9
        return $prefix . $message . $suffix;
51
    }
52
53 9
    protected function setFields(): void
54
    {
55
        $strLen =
56 9
            strlen($this->message()) + strlen($this->percent()) + strlen($this->paddingStr) + static::ERASING_SHIFT;
57 9
        $this->moveBackSequenceStr = ConsoleColor::ESC_CHAR . "[{$strLen}D";
58 9
        $this->eraseBySpacesStr = str_repeat(' ', $strLen);
59 9
    }
60
61
    /**
62
     * @return string
63
     */
64 9
    protected function message(): string
65
    {
66 9
        return $this->messageStr;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 9
    protected function percent(): string
73
    {
74 9
        return $this->percentStr;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    abstract protected function getSymbols(): array;
81
82
    public function interval(): float
83
    {
84
       return static::INTERVAL;
85
    }
86
87 3
    protected function getStyles(): array
88
    {
89
        return [
90 3
            Styling::COLOR256_SPINNER_STYLES => [
91
                '203',
92
                '209',
93
                '215',
94
                '221',
95
                '227',
96
                '191',
97
                '155',
98
                '119',
99
                '83',
100
                '84',
101
                '85',
102
                '86',
103
                '87',
104
                '81',
105
                '75',
106
                '69',
107
                '63',
108
                '99',
109
                '135',
110
                '171',
111
                '207',
112
                '206',
113
                '205',
114
                '204',
115
            ],
116 3
            Styling::COLOR_SPINNER_STYLES => ['96'],
117
        ];
118
    }
119
120 2
    public function inline(bool $inline): SpinnerInterface
121
    {
122 2
        $this->paddingStr = $inline ? SpinnerInterface::PADDING_SPACE_SYMBOL : SpinnerInterface::PADDING_EMPTY;
123 2
        $this->setFields();
124 2
        return $this;
125
    }
126
127
    /** {@inheritDoc} */
128 8
    public function begin(): string
129
    {
130 8
        return $this->spin();
131
    }
132
133
    /** {@inheritDoc} */
134 8
    public function spin(?float $percent = null): string
135
    {
136 8
        if (null !== $percent) {
137
            $this->updatePercent($percent);
138
        }
139
        return
140 8
            $this->paddingStr .
141 8
            $this->styled->spinner() .
142 8
            $this->styled->message(
143 8
                $this->message()
144
            ) .
145 8
            $this->styled->percent(
146 8
                $this->percent()
147
            ) .
148 8
            $this->moveBackSequenceStr;
149
    }
150
151
    /**
152
     * @param float $percent
153
     */
154
    protected function updatePercent(float $percent): void
155
    {
156
        if (0 === (int)($percent * 1000) % 10) {
157
            $this->percentStr = Pretty::percent($percent, 0, ' ');
158
            $this->setFields();
159
        }
160
    }
161
162
    /** {@inheritDoc} */
163 8
    public function end(): string
164
    {
165 8
        return $this->erase();
166
    }
167
168
    /** {@inheritDoc} */
169 8
    public function erase(): string
170
    {
171 8
        return $this->eraseBySpacesStr . $this->moveBackSequenceStr;
172
    }
173
}
174