Passed
Push — master ( f46582...923fc3 )
by Alec
02:06
created

AbstractSpinner::spin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

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