Passed
Push — master ( 14dff7...0081d1 )
by Alec
02:35
created

Settings::setJugglersOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 isEnabled(): bool
49
    {
50
        return
51 27
            $this->properties[S::ENABLED]->getValue();
52
    }
53
54
    /** {@inheritDoc} */
55 1
    public function setEnabled(bool $enabled = true): self
56
    {
57 1
        $this->properties[S::ENABLED]->setValue($enabled);
58 1
        return $this;
59
    }
60
61
//    /** {@inheritDoc} */
62
//    public function enable(): self
63
//    {
64
//        $this->properties[S::ENABLED]->setValue(true);
65
//        return $this;
66
//    }
67
//
68
    /** {@inheritDoc} */
69 30
    public function getMessage(): ?string
70
    {
71
        return
72 30
            $this->properties[S::MESSAGE]->getValue();
73
    }
74
75
    /** {@inheritDoc} */
76 25
    public function setMessage(?string $message): self
77
    {
78 25
        $this->properties[S::MESSAGE]->setValue($message);
79 25
        if (Defaults::EMPTY_STRING === $message || null === $message) {
80 4
            $erasingLength = 0;
81
        } else {
82 25
            $erasingLength = Calculator::computeErasingLength($message);
83
        }
84 25
        $this->properties[S::MESSAGE_ERASING_LENGTH]->setValue($erasingLength);
85 25
        return $this;
86
    }
87
88
    /** {@inheritDoc} */
89 2
    public function setMessageSuffix(string $suffix): self
90
    {
91 2
        $this->properties[S::MESSAGE_SUFFIX]->setValue($suffix);
92 2
        return $this;
93
    }
94
95
    /** {@inheritDoc} */
96 27
    public function getMessageSuffix(): string
97
    {
98
        return
99 27
            $this->properties[S::MESSAGE_SUFFIX]->getValue();
100
    }
101
102
    /** {@inheritDoc} */
103 29
    public function getInlinePaddingStr(): string
104
    {
105
        return
106 29
            $this->properties[S::INLINE_PADDING_STR]->getValue();
107
    }
108
109
    /** {@inheritDoc} */
110 3
    public function setInlinePaddingStr(string $inlinePaddingStr): self
111
    {
112 3
        $this->properties[S::INLINE_PADDING_STR]->setValue($inlinePaddingStr);
113 3
        return $this;
114
    }
115
116
    /** {@inheritDoc} */
117 29
    public function getFrames(): array
118
    {
119
        return
120 29
            $this->properties[S::FRAMES]->getValue();
121
    }
122
123
    /** {@inheritDoc} */
124 39
    public function setFrames(array $frames): self
125
    {
126 39
        Sentinel::assertFrames($frames);
127 27
        $this->properties[S::FRAMES]->setValue($frames);
128 27
        $this->properties[S::ERASING_SHIFT]->setValue(Calculator::computeErasingLengths($frames));
129 27
        return $this;
130
    }
131
132
    /** {@inheritDoc} */
133 29
    public function getStyles(): array
134
    {
135
        return
136 29
            $this->properties[S::STYLES]->getValue();
137
    }
138
139
    /** {@inheritDoc} */
140 27
    public function setStyles(array $styles): self
141
    {
142 27
        $this->properties[S::STYLES]->setValue($styles);
143 27
        return $this;
144
    }
145
146
    /** {@inheritDoc} */
147 3
    public function getMessageErasingLength(): int
148
    {
149
        return
150 3
            $this->properties[S::MESSAGE_ERASING_LENGTH]->getValue();
151
    }
152
153
    /** {@inheritDoc} */
154 3
    public function getSpacer(): string
155
    {
156
        return
157 3
            $this->properties[S::SPACER]->getValue();
158
    }
159
160
    /** {@inheritDoc} */
161 1
    public function setSpacer(string $spacer): self
162
    {
163 1
        $this->properties[S::SPACER]->setValue($spacer);
164 1
        return $this;
165
    }
166
167
    /** {@inheritDoc} */
168 6
    public function merge(self $settings): self
169
    {
170 6
        $keys = array_keys($this->properties);
171 6
        foreach ($keys as $key) {
172 6
            if ($settings->properties[$key]->isNotDefault()) {
173 5
                $this->properties[$key] = $settings->properties[$key];
174
            }
175
        }
176 6
        return $this;
177
    }
178
179
    /** {@inheritDoc} */
180 11
    public function getInitialPercent(): ?float
181
    {
182 11
        return $this->properties[S::INITIAL_PERCENT]->getValue();
183
    }
184
185
    /** {@inheritDoc} */
186 11
    public function setInitialPercent(?float $percent): self
187
    {
188 11
        $this->properties[S::INITIAL_PERCENT]->setValue($percent);
189 11
        return $this;
190
    }
191
192
    /** {@inheritDoc} */
193 27
    public function getJugglersOrder(): array
194
    {
195 27
        return $this->properties[S::JUGGLERS_ORDER]->getValue();
196
    }
197
198
    /** {@inheritDoc} */
199 3
    public function setJugglersOrder(array $order): self
200
    {
201 3
        $order = array_unique($order);
202 3
        Sentinel::assertJugglersOrder($order);
203 1
        $this->properties[S::JUGGLERS_ORDER]->setValue($order);
204 1
        return $this;
205
    }
206
}
207