Passed
Push — master ( 79167b...c79db4 )
by Vermeulen
46s queued 10s
created

BasicMsg::flush()   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 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace bultonFr\Utils\Cli;
6
7
class BasicMsg
8
{
9
    public static function displayMsg(
10
        string $msg,
11
        string $txtColor = 'white',
12
        string $txtStyle = 'normal'
13
    ) {
14 1
        $nbArgs = func_num_args();
15
16 1
        if ($nbArgs === 1) {
17 1
            echo $msg;
18 1
            static::flush();
19
20 1
            return;
21
        }
22
        
23 1
        $txtStyleCode = static::obtainTxtStyleCode($txtStyle);
24 1
        $txtColorCode = static::obtainTxtColorCode($txtColor);
25
        
26 1
        echo "\033[".$txtStyleCode.";".$txtColorCode."m".$msg."\033[0m";
27 1
        static::flush();
28 1
    }
29
    
30
    public static function displayMsgNL(
31
        string $msg,
32
        string $txtColor = 'white',
33
        string $txtStyle = 'normal'
34
    ) {
35 1
        $nbArgs = func_num_args();
36 1
        if ($nbArgs === 1) {
37 1
            static::displayMsg($msg);
38 1
            echo "\n";
39 1
            static::flush();
40 1
            return;
41
        }
42
        
43 1
        static::displayMsg($msg, $txtColor, $txtStyle);
44 1
        echo "\n";
45 1
        static::flush();
46 1
    }
47
48
    protected static function flush()
49
    {
50 1
        if (ob_get_status() !== []) {
51 1
            ob_flush();
52
        }
53 1
    }
54
    
55
    protected static function obtainTxtColorCode(string $txtColor): int
56
    {
57 1
        if ($txtColor === 'red') {
58 1
            return 31;
59 1
        } elseif ($txtColor === 'green') {
60 1
            return 32;
61 1
        } elseif ($txtColor === 'yellow') {
62 1
            return 33;
63
        }
64
        
65 1
        return 37; //white
66
    }
67
    
68
    protected static function obtainTxtStyleCode(string $txtStyle): int
69
    {
70 1
        if ($txtStyle === 'bold') {
71 1
            return 1;
72
        }
73
        
74 1
        return 0; //normal
75
    }
76
}
77