Passed
Push — master ( b9b37f...a27485 )
by Alec
02:44 queued 14s
created

OldStyleFrameCollectionRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
// 10.03.23
5
6
namespace AlecRabbit\Spinner\Core;
7
8
use AlecRabbit\Spinner\Contract\ColorMode;
9
use AlecRabbit\Spinner\Contract\IFrame;
10
use AlecRabbit\Spinner\Contract\IFrameCollectionRenderer;
11
use AlecRabbit\Spinner\Contract\IPattern;
12
use AlecRabbit\Spinner\Core\A\AFrameCollectionRenderer;
13
use AlecRabbit\Spinner\Core\Factory\FrameFactory;
14
use AlecRabbit\Spinner\Core\Pattern\Contract\IStylePattern;
15
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
16
use AlecRabbit\Spinner\Exception\LogicException;
17
18
final class OldStyleFrameCollectionRenderer extends AFrameCollectionRenderer
19
{
20
    private const FG = 'fg';
21
    private const BG = 'bg';
22
    private const COLOR_ARRAY_SIZE = 2;
23
24
    private ColorMode $patternColorMode = ColorMode::NONE;
25
    private ColorMode $terminalColorMode;
26
27
    public function __construct(?ColorMode $terminalColorMode = null) {
28
        $this->terminalColorMode = $terminalColorMode ?? self::getDefaults()->getTerminalSettings()->getColorMode();
29
    }
30
31
32
    /** @inheritdoc */
33
    public function pattern(IPattern $pattern): IFrameCollectionRenderer
34
    {
35
        if (!$pattern instanceof IStylePattern) {
36
            throw new InvalidArgumentException(
37
                sprintf(
38
                    'Pattern should be instance of %s, %s given.',
39
                    IStylePattern::class,
40
                    get_debug_type($pattern)
41
                )
42
            );
43
        }
44
45
        $clone = clone $this;
46
        $clone->pattern = $pattern;
47
        $clone->patternColorMode = $pattern->getColorMode();
48
        return $clone;
49
    }
50
51
52
    /**
53
     * @throws LogicException
54
     * @throws InvalidArgumentException
55
     */
56
    protected function createFrame(int|string $entry, bool $bg = false): IFrame
57
    {
58
        if ($this->terminalColorMode === ColorMode::NONE) {
59
            return FrameFactory::create('%s', 0);
60
        }
61
62
        $color = $this->patternColorMode->simplest($this->terminalColorMode)->ansiCode($entry);
63
64
        return
65
            FrameFactory::create(
66
                Sequencer::colorSequence(($bg ? '4' : '3') . $color . 'm%s'),
67
                0
68
            );
69
    }
70
71
    /**
72
     * @throws InvalidArgumentException
73
     * @throws LogicException
74
     */
75
    protected function createFromArray(array $entry): IFrame
76
    {
77
        $this->assertEntryArray($entry);
78
79
        if ($this->terminalColorMode === ColorMode::NONE) {
80
            return FrameFactory::create('%s', 0);
81
        }
82
83
        $fgColor = $this->patternColorMode->simplest($this->terminalColorMode)->ansiCode((string)$entry[self::FG]);
84
        $bgColor = $this->patternColorMode->simplest($this->terminalColorMode)->ansiCode((string)$entry[self::BG]);
85
86
        return
87
            FrameFactory::create(
88
                Sequencer::colorSequence('3' . $fgColor . ';4' . $bgColor . 'm%s'),
89
                0
90
            );
91
    }
92
93
    /**
94
     * @throws InvalidArgumentException
95
     */
96
    private function assertEntryArray(array $entry): void
97
    {
98
        $size = count($entry);
99
        $expectedSize = 2;
100
        if (self::COLOR_ARRAY_SIZE !== $size) {
101
            throw new InvalidArgumentException(
102
                sprintf(
103
                    'Array should contain %d elements, %d given.',
104
                    $expectedSize,
105
                    $size
106
                )
107
            );
108
        }
109
        if (!array_key_exists(self::FG, $entry) || !array_key_exists(self::BG, $entry)) {
110
            throw new InvalidArgumentException(
111
                sprintf(
112
                    'Array should contain keys "%s" and "%s", keys ["%s"] given.',
113
                    self::FG,
114
                    self::BG,
115
                    implode('", "', array_keys($entry))
116
                )
117
            );
118
        }
119
    }
120
}
121