Passed
Push — master ( a71cb0...1d85b6 )
by Alec
02:27
created

Themes::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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