Test Failed
Push — master ( 16186d...411172 )
by Alec
02:49
created

AbstractThemes::refineEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\ConsoleColour\Core;
4
5
use AlecRabbit\ConsoleColour\ConsoleColor;
6
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException;
7
8
abstract class AbstractThemes
9
{
10
    /** @var array */
11
    protected $definedThemes;
12
13
    /** @var bool */
14
    protected $enabled = false;
15
16
    /** @var ConsoleColor */
17
    protected $color;
18
19
    /**
20
     * Themed constructor.
21
     * @param null|bool|resource $stream
22
     * @param null|int $colorLevel
23
     * @param null|bool $enabled
24
     * @throws InvalidStyleException
25
     */
26 7
    public function __construct($stream = null, ?int $colorLevel = null, ?bool $enabled = null)
27
    {
28 7
        $this->color = new ConsoleColor($stream, $colorLevel);
29 7
        $this->enabled = $this->refineEnabled($enabled);
30 7
        $this->initializeThemes();
31 7
    }
32 7
33
    /**
34
     * @param null|bool $colorize
35
     * @return bool
36
     */
37
    protected function refineEnabled(?bool $colorize): bool
38 7
    {
39
        if ($supported = $this->color->isApplicable()) {
40 7
            return $colorize ?? $supported;
41 7
        }
42
        // @codeCoverageIgnoreStart
43
        return false;
44
        // @codeCoverageIgnoreEnd
45
    }
46
47
    /**
48
     * @param bool $override
49
     * @throws InvalidStyleException
50
     */
51
    protected function initializeThemes(bool $override = false): void
52 7
    {
53
        foreach ($this->getThemes() as $name => $styles) {
54 7
            $this->color->addTheme($name, $styles, $override);
55 7
        }
56
    }
57 7
58
//    /**
59
//     * @param array $themes
60
//     * @param bool $override
61
//     * @throws InvalidStyleException
62
//     */
63
//    public function setThemes(array $themes, bool $override = true): void {
64 7
//        foreach ($themes as $name => $styles) {
65
//            $this->color->addTheme($name, $styles, $override);
66 7
//        }
67 1
//        $this->definedThemes = $themes;
68
//    }
69
//
70 7
    /**
71
     * @return array
72
     *
73
     * @psalm-suppress RedundantConditionGivenDocblockType
74
     */
75
    public function getThemes(): array
76
    {
77
        if (null !== $this->definedThemes) {
78
            return $this->definedThemes;
79
        }
80
        return
81
            $this->definedThemes = $this->prepareThemes();
82
    }
83
84 6
    /**
85
     * @return array
86 6
     */
87 4
    abstract protected function prepareThemes(): array;
88
89
    /**
90 3
     * @param string $name
91
     * @param array $arguments
92
     * @return string
93
     * @throws \Throwable
94
     */
95
    public function __call(string $name, array $arguments): string
96 6
    {
97
        $this->assertMethodName($name);
98 6
        $this->assertArgs($name, $arguments);
99 2
100
        return
101 4
            $this->apply($this->definedThemes[$name], $arguments[0]);
102
    }
103
104
    /**
105
     * @param string $name
106
     */
107 4
    protected function assertMethodName(string $name): void
108
    {
109 4
        if (!\array_key_exists($name, $this->definedThemes)) {
110 2
            throw new \BadMethodCallException('Unknown method call [' . static::class . '::' . $name . '].');
111 2
        }
112
    }
113
114 3
    /**
115
     * @param string $name
116
     * @param array $arguments
117
     */
118
    protected function assertArgs(string $name, array $arguments): void
119
    {
120
        if (1 !== \count($arguments)) {
121
            throw new \ArgumentCountError(
122 3
                'Method [' . static::class . '::' . $name . '] accepts only one argument.'
123
            );
124
        }
125 3
    }
126
127
    /**
128
     * @param array|string $style
129
     * @param string $text
130
     * @return string
131
     * @throws \Throwable
132
     */
133
    protected function apply($style, $text): string
134
    {
135
        return
136
            $this->enabled ? $this->color->apply($style, $text) : $text;
137
    }
138
}
139