Completed
Push — master ( 9d2605...9c3b37 )
by Alec
02:41
created

Theme::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports\Formatters\Colour;
4
5
use AlecRabbit\ConsoleColour;
6
use AlecRabbit\Exception\ColorException;
7
use AlecRabbit\Exception\InvalidStyleException;
8
use AlecRabbit\Tools\Reports\Formatters\Contracts\DefaultThemes;
9
10
/**
11
 * @method italic(string $text)
12
 * @method dark(string $text)
13
 * @method darkItalic(string $text)
14
 * @method white(string $text)
15
 * @method whiteBold(string $text)
16
 * @method comment(string $text)
17
 * @method yellow(string $text)
18
 * @method error(string $text)
19
 * @method red(string $text)
20
 * @method green(string $text)
21
 * @method info(string $text)
22
 * @method underline(string $text)
23
 * @method underlineItalic(string $text)
24
 */
25
class Theme implements DefaultThemes
26
{
27
    /** @var bool */
28
    protected $doColorize;
29
30
    /** @var ConsoleColour */
31
    protected $color;
32
33
    /**
34
     * Themed constructor.
35
     * @param bool $colorize
36
     * @throws InvalidStyleException
37
     */
38
    public function __construct(bool $colorize = true)
39
    {
40
        $this->doColorize = $colorize;
41
        $this->color = new ConsoleColour();
42
        $this->setThemes();
43
    }
44
45
    /**
46
     * @throws InvalidStyleException
47
     *
48
     */
49
    protected function setThemes(): void
50
    {
51
        foreach (static::THEMES as $name => $styles) {
52
            $this->color->addTheme($name, $styles);
53
        }
54
    }
55
56
    /**
57
     * @param string $name
58
     * @param array $arguments
59
     * @return string
60
     * @throws \Throwable
61
     */
62
    public function __call(string $name, array $arguments): string
63
    {
64
        $this->assertMethodName($name);
65
        $this->assertArgs($name, $arguments);
66
67
        return
68
            $this->apply(static::THEMES[$name], $arguments[0]);
69
    }
70
71
    /**
72
     * @param array|string $style
73
     * @param string $text
74
     * @return string
75
     * @throws \Throwable
76
     */
77
    protected function apply($style, $text): string
78
    {
79
        try {
80
            return
81
                $this->doColorize ? $this->color->apply($style, $text) : $text;
82
        } catch (ColorException $e) {
83
            // do nothing
84
        }
85
        return $text;
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param array $arguments
91
     */
92
    protected function assertArgs(string $name, array $arguments): void
93
    {
94
        if (1 !== \count($arguments)) {
95
            throw new \ArgumentCountError('Method [' . $name . '] accepts only one argument.');
96
        }
97
    }
98
99
    /**
100
     * @param string $name
101
     */
102
    protected function assertMethodName(string $name): void
103
    {
104
        if (!\array_key_exists($name, static::THEMES)) {
105
            throw new \BadMethodCallException('Unknown method call [' . $name . '] in [' . static::class . '].');
106
        }
107
    }
108
}
109