Passed
Push — master ( 44fbc3...2b0c43 )
by Alec
02:46
created

Settings   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 12
Bugs 1 Features 0
Metric Value
wmc 31
eloc 65
c 12
b 1
f 0
dl 0
loc 201
ccs 79
cts 79
cp 1
rs 9.92

26 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 2
A getInterval() 0 4 1
A setInterval() 0 4 1
A __construct() 0 3 1
A isEnabled() 0 4 1
A getInitialPercent() 0 3 1
A setInitialPercent() 0 4 1
A getJugglersOrder() 0 3 1
A setFrames() 0 6 1
A setStyles() 0 4 1
A merge() 0 9 3
A getMessageSuffix() 0 4 1
A getInlinePaddingStr() 0 4 1
A getHideCursor() 0 4 1
A getMessage() 0 4 1
A setEnabled() 0 4 1
A setInlinePaddingStr() 0 4 1
A getFrames() 0 4 1
A setMessage() 0 10 3
A setMessageSuffix() 0 4 1
A getSpacer() 0 4 1
A setSpacer() 0 4 1
A getStyles() 0 4 1
A setJugglersOrder() 0 6 1
A getMessageErasingLength() 0 4 1
A setDoNotHideCursor() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Settings;
4
5
use AlecRabbit\Spinner\Core\Calculator;
6
use AlecRabbit\Spinner\Core\Sentinel;
7
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
8
use AlecRabbit\Spinner\Settings\Contracts\S;
9
use AlecRabbit\Spinner\Settings\Contracts\SettingsInterface;
10
11
class Settings implements SettingsInterface
12
{
13
    /** @var Property[] */
14
    protected $properties;
15
16 44
    public function __construct()
17
    {
18 44
        $this->properties = $this->initialize();
19 44
    }
20
21
    /**
22
     * @return Property[]
23
     */
24 44
    private function initialize(): array
25
    {
26 44
        $properties = [];
27 44
        foreach (Defaults::DEFAULT_SETTINGS as $name => $value) {
28 44
            $properties[$name] = new Property($value);
29
        }
30 44
        return $properties;
31
    }
32
33
    /** {@inheritDoc} */
34 29
    public function getInterval(): float
35
    {
36
        return
37 29
            $this->properties[S::INTERVAL]->getValue();
38
    }
39
40
    /** {@inheritDoc} */
41 27
    public function setInterval(float $interval): self
42
    {
43 27
        $this->properties[S::INTERVAL]->setValue($interval);
44 27
        return $this;
45
    }
46
47
    /** {@inheritDoc} */
48 27
    public function getHideCursor(): bool
49
    {
50
        return
51 27
            $this->properties[S::HIDE_CURSOR]->getValue();
52
    }
53
54
    /** {@inheritDoc} */
55 1
    public function setDoNotHideCursor(): self
56
    {
57 1
        $this->properties[S::HIDE_CURSOR]->setValue(false);
58 1
        return $this;
59
    }
60
61
    /** {@inheritDoc} */
62 27
    public function isEnabled(): bool
63
    {
64
        return
65 27
            $this->properties[S::ENABLED]->getValue();
66
    }
67
68
    /** {@inheritDoc} */
69 1
    public function setEnabled(bool $enabled = true): self
70
    {
71 1
        $this->properties[S::ENABLED]->setValue($enabled);
72 1
        return $this;
73
    }
74
75
    /** {@inheritDoc} */
76 30
    public function getMessage(): ?string
77
    {
78
        return
79 30
            $this->properties[S::MESSAGE]->getValue();
80
    }
81
82
    /** {@inheritDoc} */
83 25
    public function setMessage(?string $message): self
84
    {
85 25
        $this->properties[S::MESSAGE]->setValue($message);
86 25
        if (Defaults::EMPTY_STRING === $message || null === $message) {
87 4
            $erasingLength = 0;
88
        } else {
89 25
            $erasingLength = Calculator::computeErasingLength($message);
90
        }
91 25
        $this->properties[S::MESSAGE_ERASING_LENGTH]->setValue($erasingLength);
92 25
        return $this;
93
    }
94
95
    /** {@inheritDoc} */
96 2
    public function setMessageSuffix(string $suffix): self
97
    {
98 2
        $this->properties[S::MESSAGE_SUFFIX]->setValue($suffix);
99 2
        return $this;
100
    }
101
102
    /** {@inheritDoc} */
103 27
    public function getMessageSuffix(): string
104
    {
105
        return
106 27
            $this->properties[S::MESSAGE_SUFFIX]->getValue();
107
    }
108
109
    /** {@inheritDoc} */
110 29
    public function getInlinePaddingStr(): string
111
    {
112
        return
113 29
            $this->properties[S::INLINE_PADDING_STR]->getValue();
114
    }
115
116
    /** {@inheritDoc} */
117 3
    public function setInlinePaddingStr(string $inlinePaddingStr): self
118
    {
119 3
        $this->properties[S::INLINE_PADDING_STR]->setValue($inlinePaddingStr);
120 3
        return $this;
121
    }
122
123
    /** {@inheritDoc} */
124 29
    public function getFrames(): array
125
    {
126
        return
127 29
            $this->properties[S::FRAMES]->getValue();
128
    }
129
130
    /** {@inheritDoc} */
131 39
    public function setFrames(array $frames): self
132
    {
133 39
        Sentinel::assertFrames($frames);
134 27
        $this->properties[S::FRAMES]->setValue($frames);
135 27
        $this->properties[S::ERASING_SHIFT]->setValue(Calculator::computeErasingLengths($frames));
136 27
        return $this;
137
    }
138
139
    /** {@inheritDoc} */
140 29
    public function getStyles(): array
141
    {
142
        return
143 29
            $this->properties[S::STYLES]->getValue();
144
    }
145
146
    /** {@inheritDoc} */
147 27
    public function setStyles(array $styles): self
148
    {
149 27
        $this->properties[S::STYLES]->setValue($styles);
150 27
        return $this;
151
    }
152
153
    /** {@inheritDoc} */
154 3
    public function getMessageErasingLength(): int
155
    {
156
        return
157 3
            $this->properties[S::MESSAGE_ERASING_LENGTH]->getValue();
158
    }
159
160
    /** {@inheritDoc} */
161 3
    public function getSpacer(): string
162
    {
163
        return
164 3
            $this->properties[S::SPACER]->getValue();
165
    }
166
167
    /** {@inheritDoc} */
168 1
    public function setSpacer(string $spacer): self
169
    {
170 1
        $this->properties[S::SPACER]->setValue($spacer);
171 1
        return $this;
172
    }
173
174
    /** {@inheritDoc} */
175 6
    public function merge(self $settings): self
176
    {
177 6
        $keys = array_keys($this->properties);
178 6
        foreach ($keys as $key) {
179 6
            if ($settings->properties[$key]->isNotDefault()) {
180 5
                $this->properties[$key] = $settings->properties[$key];
181
            }
182
        }
183 6
        return $this;
184
    }
185
186
    /** {@inheritDoc} */
187 11
    public function getInitialPercent(): ?float
188
    {
189 11
        return $this->properties[S::INITIAL_PERCENT]->getValue();
190
    }
191
192
    /** {@inheritDoc} */
193 11
    public function setInitialPercent(?float $percent): self
194
    {
195 11
        $this->properties[S::INITIAL_PERCENT]->setValue($percent);
196 11
        return $this;
197
    }
198
199
    /** {@inheritDoc} */
200 27
    public function getJugglersOrder(): array
201
    {
202 27
        return $this->properties[S::JUGGLERS_ORDER]->getValue();
203
    }
204
205
    /** {@inheritDoc} */
206 3
    public function setJugglersOrder(array $order): self
207
    {
208 3
        $order = array_unique($order);
209 3
        Sentinel::assertJugglersOrder($order);
210 1
        $this->properties[S::JUGGLERS_ORDER]->setValue($order);
211 1
        return $this;
212
    }
213
214
}
215