Passed
Push — master ( 83a1d8...40412c )
by Alec
03:08
created

Style::mergeStyles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core;
4
5
use AlecRabbit\Accessories\Circular;
6
use AlecRabbit\Spinner\Core\Contracts\StylesInterface;
7
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
8
9
/**
10
 * Class Styles
11
 *
12
 * @internal
13
 */
14
class Style
15
{
16
    /** @var Circular */
17
    protected $spinnerStyles;
18
    /** @var Circular */
19
    protected $messageStyles;
20
    /** @var Circular */
21
    protected $percentStyles;
22
23
    /**
24
     * Styles constructor.
25
     * @param array $styles
26
     * @param mixed $colorSupport
27
     */
28 28
    public function __construct(array $styles, $colorSupport = null)
29
    {
30 28
        $styles = $this->mergeStyles($styles);
31 28
        $coloring = new Coloring($styles, $colorSupport);
32 27
        $this->spinnerStyles = $coloring->getSpinnerStyles();
33 27
        $this->messageStyles = $coloring->getMessageStyles();
34 27
        $this->percentStyles = $coloring->getPercentStyles();
35 27
    }
36
37
    /**
38
     * @param array $styles
39
     * @return array
40
     */
41 28
    protected function mergeStyles(array $styles): array
42
    {
43 28
        $defaultStyles = StylesInterface::DEFAULT_STYLES;
44 28
        foreach ($defaultStyles as $key => $defaults) {
45 28
            if (\array_key_exists($key, $styles)) {
46
                /** @noinspection SlowArrayOperationsInLoopInspection */
47 25
                $defaultStyles[$key] = array_merge(StylesInterface::DEFAULT_STYLES[$key], $styles[$key]);
48
            }
49
        }
50 28
        return $defaultStyles;
51
    }
52
53 18
    public function spinner(string $symbol): string
54
    {
55
        return
56 18
            sprintf((string)$this->spinnerStyles->value(), $symbol);
57
    }
58
59 18
    public function message(string $message): string
60
    {
61
        return
62 18
            sprintf((string)$this->messageStyles->value(), $message);
63
    }
64
65 18
    public function percent(string $percent): string
66
    {
67
        return
68 18
            sprintf((string)$this->percentStyles->value(), $percent);
69
    }
70
}
71