Passed
Push — master ( d08e35...139360 )
by Alec
02:47
created

Settings::compErasingLen()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3

1 Method

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