Console::log()   C
last analyzed

Complexity

Conditions 8
Paths 23

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 17
nc 23
nop 4
1
<?php declare(strict_types = 1);
2
3
namespace Console;
4
5
use Console\Exception\Background as BackgroundException;
6
use Console\Exception\Option as OptionException;
7
use Console\Exception\Text as TextException;
8
use Console\Modifier\Background;
9
use Console\Modifier\Option;
10
use Console\Modifier\Text;
11
12
class Console
13
{
14
    /**
15
     * Methods outputs text to console using colors
16
     *
17
     * @param string      $message
18
     * @param string|null $color
19
     *
20
     * @see Text
21
     *
22
     * @param string|null $background
23
     *
24
     * @see Background
25
     *
26
     * @param string|null $option
27
     *
28
     * @see Option
29
     *
30
     * @throws \Console\Exception\Option
31
     * @throws \Console\Exception\Background
32
     * @throws \Console\Exception\Text
33
     */
34
    public static function log(
35
        string $message,
36
        string $color = null,
37
        string $background = null,
38
        string $option = null): void
39
    {
40
        $log = '';
41
42
        if (null !== $color) {
43
            if (!Text::isValid($color)) {
44
                throw new TextException('Invalid text color received');
45
            }
46
            $log .= "\033[" . Text::getSymbol($color) . 'm';
47
        }
48
49
        if (null !== $background) {
50
            if (!Background::isValid($background)) {
51
                throw new BackgroundException('Invalid background color received');
52
            }
53
            $log .= "\033[" . Background::getSymbol($background) . 'm';
54
        }
55
56
        if (null !== $option) {
57
            if (!Option::isValid($option)) {
58
                throw new OptionException('Invalid option received');
59
            }
60
            $log .= "\033[" . Option::getSymbol($option) . 'm';
61
        }
62
63
        if ('' !== $log) {
64
            echo $log . $message . "\033[0m";
65
66
            return;
67
        }
68
69
        echo $message;
70
    }
71
}