Passed
Push — master ( 6d1ceb...0474c6 )
by Alec
02:33 queued 48s
created

Settings::defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.9332
cc 1
nc 1
nop 0
crap 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 $prefix;
23
    /** @var string */
24
    protected $suffix;
25
    /** @var string */
26
    protected $inlinePaddingStr;
27
    /** @var array */
28
    protected $symbols;
29
    /** @var array */
30
    protected $styles;
31
32
    /**
33
     * Settings constructor.
34
     */
35 19
    public function __construct()
36
    {
37 19
        $this->initializeWithDefaults();
38 19
    }
39
40 19
    protected function initializeWithDefaults(): SettingsInterface
41
    {
42
        return
43
            $this
44 19
                ->setSuffix(null)
45 19
                ->setSymbols(null)
46 19
                ->setStyles(null)
47 19
                ->setMessage(null)
48 19
                ->setPrefix(null)
49 19
                ->setInterval(null)
50 19
                ->setErasingShift(null)
51 19
                ->setInlinePaddingStr(null);
52
    }
53
54 18
    public function getInterval(): float
55
    {
56 18
        return $this->interval;
57
    }
58
59 19
    public function setInterval(?float $interval): SettingsInterface
60
    {
61 19
        $this->interval = $interval ?? SettingsInterface::DEFAULT_INTERVAL;
62 19
        return $this;
63
    }
64
65 18
    public function getErasingShift(): int
66
    {
67 18
        return $this->erasingShift;
68
    }
69
70 19
    public function setErasingShift(?int $erasingShift): SettingsInterface
71
    {
72 19
        $this->erasingShift = $erasingShift ?? SettingsInterface::DEFAULT_ERASING_SHIFT;
73 19
        return $this;
74
    }
75
76 18
    public function getMessage(): string
77
    {
78 18
        return $this->message;
79
    }
80
81 19
    public function setMessage(?string $string): SettingsInterface
82
    {
83 19
        $this->message = $string ?? SettingsInterface::EMPTY;
84 19
        if (SettingsInterface::EMPTY === $this->message) {
85 19
            $this->setSuffix(SettingsInterface::EMPTY);
86
        } else {
87 15
            $this->setSuffix(SettingsInterface::DEFAULT_SUFFIX);
88
        }
89 19
        return $this;
90
    }
91
92 18
    public function getPrefix(): string
93
    {
94 18
        return $this->prefix;
95
    }
96
97 19
    public function setPrefix(?string $prefix): SettingsInterface
98
    {
99 19
        $this->prefix = $prefix ?? SettingsInterface::ONE_SPACE_SYMBOL;
100 19
        return $this;
101
    }
102
103 18
    public function getSuffix(): string
104
    {
105 18
        return $this->suffix;
106
    }
107
108 19
    public function setSuffix(?string $suffix): SettingsInterface
109
    {
110 19
        $this->suffix = $suffix ?? SettingsInterface::DEFAULT_SUFFIX;
111 19
        return $this;
112
    }
113
114 18
    public function getInlinePaddingStr(): string
115
    {
116 18
        return $this->inlinePaddingStr;
117
    }
118
119 19
    public function setInlinePaddingStr(?string $inlinePaddingStr): SettingsInterface
120
    {
121 19
        $this->inlinePaddingStr = $inlinePaddingStr ?? SettingsInterface::EMPTY;
122 19
        return $this;
123
    }
124
125 18
    public function getSymbols(): array
126
    {
127 18
        return $this->symbols;
128
    }
129
130 19
    public function setSymbols(?array $symbols): SettingsInterface
131
    {
132 19
        if (null !== $symbols && count($symbols) > SettingsInterface::MAX_SYMBOLS_COUNT) {
133 1
            throw new \InvalidArgumentException(
134 1
                sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded.', SettingsInterface::MAX_SYMBOLS_COUNT)
135
            );
136
        }
137 19
        $this->symbols = $symbols ?? static::DEFAULT_SYMBOLS;
138 19
        return $this;
139
    }
140
141 18
    public function getStyles(): array
142
    {
143 18
        return $this->styles;
144
    }
145
146 19
    public function setStyles(?array $styles): SettingsInterface
147
    {
148 19
        $this->styles = $this->mergeStyles(StylesInterface::DEFAULT_STYLES, $styles ?? []);
149 19
        return $this;
150
    }
151
152 19
    protected function mergeStyles(array $default_styles, array $styles): array
153
    {
154 19
        foreach ($default_styles as $key => $defaults) {
155 19
            if (\array_key_exists($key, $styles)) {
156
                /** @noinspection SlowArrayOperationsInLoopInspection */
157 18
                $default_styles[$key] = array_merge($default_styles[$key], $styles[$key]);
158
            }
159
        }
160 19
        return $default_styles;
161
    }
162
}
163