Passed
Push — master ( 49e901...aed7cd )
by Alec
02:33
created

Settings   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 31
eloc 69
c 5
b 0
f 0
dl 0
loc 223
ccs 79
cts 79
cp 1
rs 9.92

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getInterval() 0 3 1
A initializeWithDefaults() 0 12 1
A __construct() 0 3 1
A setInterval() 0 4 1
A setStyles() 0 4 1
A setMessage() 0 10 2
A mergeStyles() 0 9 3
A getMessage() 0 3 1
A getInlinePaddingStr() 0 3 1
A setInlinePaddingStr() 0 4 1
A compErasingLen() 0 13 3
A refineErasingLen() 0 6 2
A setMessagePrefix() 0 4 1
A getErasingShift() 0 3 1
A getMessageErasingLen() 0 3 1
A getSymbols() 0 3 1
A getMessagePrefix() 0 3 1
A computeErasingLen() 0 6 2
A setMessageSuffix() 0 4 1
A setSymbols() 0 10 3
A getMessageSuffix() 0 3 1
A getStyles() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Spinner\Core\Contracts\SettingsInterface;
6
use AlecRabbit\Spinner\Core\Contracts\StylesInterface;
7
8
/**
9
 * Class Settings
10
 *
11
 * @psalm-suppress PropertyNotSetInConstructor
12
 */
13
class Settings implements SettingsInterface
14
{
15
    /** @var float */
16
    protected $interval;
17
    /** @var int */
18
    protected $erasingShift;
19
    /** @var string */
20
    protected $message;
21
    /** @var string */
22
    protected $messagePrefix;
23
    /** @var string */
24
    protected $messageSuffix;
25
    /** @var string */
26
    protected $inlinePaddingStr;
27
    /** @var array */
28
    protected $symbols;
29
    /** @var array */
30
    protected $styles;
31
    /** @var int */
32
    protected $messageErasingLen;
33
34
    /**
35
     * Settings constructor.
36
     */
37 23
    public function __construct()
38
    {
39 23
        $this->initializeWithDefaults();
40 23
    }
41
42 23
    protected function initializeWithDefaults(): SettingsInterface
43
    {
44
        return
45
            $this
46 23
                ->setMessageSuffix(null)
47 23
                ->setSymbols(null)
48 23
                ->setStyles(null)
49 23
                ->setMessage(null)
50 23
                ->setMessagePrefix(null)
51 23
                ->setInterval(null)
52
//                ->setErasingShift(null)
53 23
                ->setInlinePaddingStr(null);
54
    }
55
56
    /** {@inheritDoc} */
57 22
    public function getInterval(): float
58
    {
59 22
        return $this->interval;
60
    }
61
62
    /** {@inheritDoc} */
63 23
    public function setInterval(?float $interval): SettingsInterface
64
    {
65 23
        $this->interval = $interval ?? SettingsInterface::DEFAULT_INTERVAL;
66 23
        return $this;
67
    }
68
//
69
70
    /** {@inheritDoc} */
71 22
    public function getErasingShift(): int
72
    {
73 22
        return $this->erasingShift;
74
    }
75
76
//    /** {@inheritDoc} */
77
//    public function setErasingShift(?int $erasingShift): SettingsInterface
78
//    {
79
//        $this->erasingShift = $erasingShift ?? SettingsInterface::DEFAULT_ERASING_SHIFT;
80
//        return $this;
81
//    }
82
//
83
    /** {@inheritDoc} */
84 22
    public function getMessage(): string
85
    {
86 22
        return $this->message;
87
    }
88
89
    /** {@inheritDoc} */
90 23
    public function setMessage(?string $message, ?int $erasingLen = null): SettingsInterface
91
    {
92 23
        $this->message = $message ?? SettingsInterface::EMPTY;
93 23
        $this->messageErasingLen = $this->refineErasingLen($message, $erasingLen);
94 23
        if (SettingsInterface::EMPTY === $this->message) {
95 23
            $this->setMessageSuffix(SettingsInterface::EMPTY);
96
        } else {
97 18
            $this->setMessageSuffix(SettingsInterface::DEFAULT_SUFFIX);
98
        }
99 23
        return $this;
100
    }
101
102
    /**
103
     * @param null|string $string
104
     * @param null|int $erasingLen
105
     * @return int
106
     */
107 23
    protected function refineErasingLen(?string $string, ?int $erasingLen): int
108
    {
109 23
        if (null === $erasingLen) {
110 23
            return $this->computeErasingLen([$string]);
111
        }
112 1
        return $erasingLen;
113
    }
114
115
    /**
116
     * @param array $strings
117
     * @return int
118
     */
119 23
    protected function computeErasingLen(array $strings): int
120
    {
121 23
        if (empty($strings)) {
122 23
            return 0;
123
        }
124 23
        return $this->compErasingLen($strings);
125
    }
126
127 23
    private function compErasingLen(array $strings): int
128
    {
129
        // TODO check if all elements have the same erasingLen
130
131 23
        if (null === $symbol = $strings[0]) {
132 23
            return 0;
133
        }
134 19
        $mbSymbolLen = mb_strlen($symbol);
135 19
        $oneCharLen = strlen($symbol) / $mbSymbolLen;
136 19
        if (4 === $oneCharLen) {
137 3
            return 2 * $mbSymbolLen;
138
        }
139 19
        return 1 * $mbSymbolLen;
140
    }
141
142
    /** {@inheritDoc} */
143 22
    public function getMessagePrefix(): string
144
    {
145 22
        return $this->messagePrefix;
146
    }
147
148
    /** {@inheritDoc} */
149 23
    public function setMessagePrefix(?string $messagePrefix): SettingsInterface
150
    {
151 23
        $this->messagePrefix = $messagePrefix ?? SettingsInterface::ONE_SPACE_SYMBOL;
152 23
        return $this;
153
    }
154
155
    /** {@inheritDoc} */
156 22
    public function getMessageSuffix(): string
157
    {
158 22
        return $this->messageSuffix;
159
    }
160
161
    /** {@inheritDoc} */
162 23
    public function setMessageSuffix(?string $messageSuffix): SettingsInterface
163
    {
164 23
        $this->messageSuffix = $messageSuffix ?? SettingsInterface::DEFAULT_SUFFIX;
165 23
        return $this;
166
    }
167
168
    /** {@inheritDoc} */
169 22
    public function getInlinePaddingStr(): string
170
    {
171 22
        return $this->inlinePaddingStr;
172
    }
173
174
    /** {@inheritDoc} */
175 23
    public function setInlinePaddingStr(?string $inlinePaddingStr): SettingsInterface
176
    {
177 23
        $this->inlinePaddingStr = $inlinePaddingStr ?? SettingsInterface::EMPTY;
178 23
        return $this;
179
    }
180
181
    /** {@inheritDoc} */
182 22
    public function getSymbols(): array
183
    {
184 22
        return $this->symbols;
185
    }
186
187
    /** {@inheritDoc} */
188 23
    public function setSymbols(?array $symbols): SettingsInterface
189
    {
190 23
        if (null !== $symbols && count($symbols) > SettingsInterface::MAX_SYMBOLS_COUNT) {
191 1
            throw new \InvalidArgumentException(
192 1
                sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded.', SettingsInterface::MAX_SYMBOLS_COUNT)
193
            );
194
        }
195 23
        $this->symbols = $symbols ?? static::DEFAULT_SYMBOLS;
196 23
        $this->erasingShift = $this->computeErasingLen($this->symbols);
197 23
        return $this;
198
    }
199
200
    /** {@inheritDoc} */
201 22
    public function getStyles(): array
202
    {
203 22
        return $this->styles;
204
    }
205
206
    /** {@inheritDoc} */
207 23
    public function setStyles(?array $styles): SettingsInterface
208
    {
209 23
        $this->styles = $this->mergeStyles(StylesInterface::DEFAULT_STYLES, $styles ?? []);
210 23
        return $this;
211
    }
212
213
    /**
214
     * @param array $defaultStyles
215
     * @param array $styles
216
     * @return array
217
     * todo move to another class?
218
     */
219 23
    protected function mergeStyles(array $defaultStyles, array $styles): array
220
    {
221 23
        foreach ($defaultStyles as $key => $defaults) {
222 23
            if (\array_key_exists($key, $styles)) {
223
                /** @noinspection SlowArrayOperationsInLoopInspection */
224 20
                $defaultStyles[$key] = array_merge($defaultStyles[$key], $styles[$key]);
225
            }
226
        }
227 23
        return $defaultStyles;
228
    }
229
230
    /**
231
     * @return int
232
     */
233 22
    public function getMessageErasingLen(): int
234
    {
235 22
        return $this->messageErasingLen;
236
    }
237
}
238