Test Failed
Push — master ( 5c7057...3a5cce )
by Alec
02:34
created

Themes::assertArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
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
    public function __construct(?bool $colorize = null)
43
    {
44
        $this->color = new ConsoleColour();
45
        $this->doColorize = $this->refineColorize($colorize);
46
        $this->setThemes();
47
    }
48
49
    /**
50
     * @param null|bool $colorize
51
     * @return bool
52
     */
53
    protected function refineColorize(?bool $colorize): bool
54
    {
55
        if ($supported = $this->color->isSupported()) {
56
            return $colorize ?? $supported;
57
        }
58
        // @codeCoverageIgnoreStart
59
        return false;
60
        // @codeCoverageIgnoreEnd
61
    }
62
63
    /**
64
     * @throws InvalidStyleException
65
     */
66
    protected function setThemes(): void
67
    {
68
        foreach ($this->getThemes() as $name => $styles) {
69
            $this->color->addTheme($name, $styles);
70
        }
71
    }
72
73
    /**
74
     * @return array
75
     *
76
     * @psalm-suppress RedundantConditionGivenDocblockType
77
     */
78
    public function getThemes(): array
79
    {
80
        if (null !== $this->definedStyles) {
81
            return $this->definedStyles;
82
        }
83
        return
84
            $this->definedStyles = $this->prepareThemes();
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    protected function prepareThemes(): array
91
    {
92
        return static::STYLES;
93
    }
94
95
    /**
96
     * @param string $name
97
     * @param array $arguments
98
     * @return string
99
     * @throws \Throwable
100
     */
101
    public function __call(string $name, array $arguments): string
102
    {
103
        $this->assertMethodName($name);
104
        $this->assertArgs($name, $arguments);
105
106
        return
107
            $this->apply($this->definedStyles[$name], $arguments[0]);
108
    }
109
110
    /**
111
     * @param string $name
112
     */
113
    protected function assertMethodName(string $name): void
114
    {
115
        if (!\array_key_exists($name, $this->definedStyles)) {
116
            throw new \BadMethodCallException('Unknown method call [' . static::class . '::' . $name . '].');
117
        }
118
    }
119
120
    /**
121
     * @param string $name
122
     * @param array $arguments
123
     */
124
    protected function assertArgs(string $name, array $arguments): void
125
    {
126
        if (1 !== \count($arguments)) {
127
            throw new \ArgumentCountError(
128
                'Method [' . static::class . '::' . $name . '] accepts only one argument.'
129
            );
130
        }
131
    }
132
133
    /**
134
     * @param array|string $style
135
     * @param string $text
136
     * @return string
137
     * @throws \Throwable
138
     */
139
    protected function apply($style, $text): string
140
    {
141
        return
142
            $this->doColorize ? $this->color->apply($style, $text) : $text;
143
    }
144
}
145