Completed
Push — develop ( f5c859...666980 )
by Alec
02:44
created

AbstractSpinner::getStyles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 22
ccs 0
cts 9
cp 0
crap 12
rs 9.7333
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 $str;
20
    /** @var string */
21
    protected $resetStr;
22
    /** @var \Closure */
23
    protected $style;
24
25
26
    public function __construct(string $str = '', string $prefix = ' ', string $suffix = '...')
27
    {
28
        $this->spinnerSymbols = $this->getSymbols();
29
        $this->styles = $this->getStyles();
30
31
        $this->str = $this->refineStr($str, $prefix, $suffix);
32
        $strLen = strlen($this->str . static::PADDING_STR) + 2;
33
        $this->resetStr = Strings::ESC . "[{$strLen}D";
34
        $this->style = $this->getStyle();
35
    }
36
37
    /**
38
     * @return Circular
39
     */
40
    abstract protected function getSymbols(): Circular;
41
42
    protected function getStyles(): ?Circular
43
    {
44
        $terminal = new Terminal();
45
        if ($terminal->supports256Color()) {
46
            return new Circular([
47
                '38;5;197',
48
                '38;5;198',
49
                '38;5;199',
50
                '38;5;200',
51
                '38;5;201',
52
                '38;5;202',
53
                '38;5;203',
54
                '38;5;204',
55
                '38;5;205',
56
            ]);
57
        }
58
        if ($terminal->supportsColor()) {
59
            return new Circular([
60
                '96',
61
            ]);
62
        }
63
        return null;
64
    }
65
66
    /**
67
     * @param string $str
68
     * @param string $prefix
69
     * @param string $suffix
70
     * @return string
71
     */
72
    protected function refineStr(string $str, string $prefix, string $suffix): string
73
    {
74
        return $prefix . $str . $suffix;
75
    }
76
77
    /**
78
     * @return \Closure
79
     */
80
    protected function getStyle(): \Closure
81
    {
82
        if (null === $this->styles) {
83
            return
84
                function (): string {
85
                    return static::PADDING_STR . $this->spinnerSymbols->value();
1 ignored issue
show
Bug introduced by
Are you sure $this->spinnerSymbols->value() of type AlecRabbit\Accessories\Rewindable|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                    return static::PADDING_STR . /** @scrutinizer ignore-type */ $this->spinnerSymbols->value();
Loading history...
86
                };
87
        } else {
88
            return
89
                function (): string {
90
                    return
91
                        static::PADDING_STR .
92
                        Strings::ESC .
93
                        "[{$this->styles->value()}m{$this->spinnerSymbols->value()}" .
1 ignored issue
show
Bug introduced by
The method value() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
                        "[{$this->styles->/** @scrutinizer ignore-call */ value()}m{$this->spinnerSymbols->value()}" .

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
                        Strings::ESC . '[0m';
95
                };
96
        }
97
    }
98
99
    /** {@inheritDoc} */
100
    public function begin(): string
101
    {
102
        return $this->work() . $this->resetStr;
103
    }
104
105
    protected function work(): string
106
    {
107
        return ($this->style)() . $this->str;
108
    }
109
110
    /** {@inheritDoc} */
111
    public function spin(): string
112
    {
113
        return $this->work() . $this->resetStr;
114
    }
115
116
    /** {@inheritDoc} */
117
    public function end(): string
118
    {
119
        return $this->resetStr . Strings::ESC . '[K';
120
    }
121
}
122