Passed
Push — master ( 246ca5...f72f04 )
by Caen
03:27 queued 14s
created

ValidationResult::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Support;
4
5
/**
6
 * @see \Hyde\Framework\Testing\Feature\Services\ValidationServiceTest
7
 * @see \Hyde\Framework\Testing\Feature\Commands\HydeValidateCommandTest
8
 */
9
class ValidationResult
10
{
11
    public string $message;
12
    public string $tip;
13
14
    public bool $passed;
15
    public bool $skipped = false;
16
17
    public function __construct(string $defaultMessage = 'Generic check')
18
    {
19
        $this->message = $defaultMessage;
20
    }
21
22
    public function pass(?string $withMessage = null): static
23
    {
24
        $this->passed = true;
25
        if ($withMessage) {
26
            $this->message = $withMessage;
27
        }
28
29
        return $this;
30
    }
31
32
    public function fail(?string $withMessage = null): static
33
    {
34
        $this->passed = false;
35
        if ($withMessage) {
36
            $this->message = $withMessage;
37
        }
38
39
        return $this;
40
    }
41
42
    public function skip(?string $withMessage = null): static
43
    {
44
        $this->skipped = true;
45
        if ($withMessage) {
46
            $this->message = $withMessage;
47
        }
48
49
        return $this;
50
    }
51
52
    public function withTip(string $withTip): static
53
    {
54
        $this->tip = $withTip;
55
56
        return $this;
57
    }
58
59
    public function tip(): string|false
60
    {
61
        return $this->tip ?? false;
62
    }
63
64
    public function skipped(): bool
65
    {
66
        return $this->skipped;
67
    }
68
69
    public function passed(): bool
70
    {
71
        return $this->passed;
72
    }
73
74
    public function failed(): bool
75
    {
76
        return ! $this->passed;
77
    }
78
79
    public function statusCode(): int
80
    {
81
        if ($this->skipped()) {
82
            return 1;
83
        }
84
        if ($this->passed()) {
85
            return 0;
86
        }
87
88
        return 2;
89
    }
90
91
    public function message(): string
92
    {
93
        return $this->message;
94
    }
95
96
    public function formattedMessage(?string $withTimeString = null): string
97
    {
98
        $string = '  '.$this->formatResult($this->message).$this->formatTimeString($withTimeString);
0 ignored issues
show
Bug introduced by
It seems like $withTimeString can also be of type null; however, parameter $time of Hyde\Framework\Models\Su...ult::formatTimeString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $string = '  '.$this->formatResult($this->message).$this->formatTimeString(/** @scrutinizer ignore-type */ $withTimeString);
Loading history...
99
        if ($this->tip()) {
100
            $string .= "\n".str_repeat(' ', 9).$this->formatTip($this->tip);
101
        }
102
103
        return $string;
104
    }
105
106
    protected function formatResult(string $message): string
107
    {
108
        return match ($this->statusCode()) {
109
            0 => $this->formatPassed($message),
110
            2 => $this->formatFailed($message),
111
            default => $this->formatSkipped($message),
112
        };
113
    }
114
115
    protected function formatPassed(string $message): string
116
    {
117
        return '<fg=white;bg=green> PASS <fg=green> '.$message.'</></>';
118
    }
119
120
    protected function formatFailed(string $message): string
121
    {
122
        return '<fg=gray;bg=yellow> FAIL <fg=yellow> '.$message.'</></>';
123
    }
124
125
    protected function formatSkipped(string $message): string
126
    {
127
        return '<fg=white;bg=gray> SKIP <fg=gray> '.$message.'</></>';
128
    }
129
130
    protected function formatTimeString(string $time): string
131
    {
132
        return '<fg=gray> ('.$time.'ms)</>';
133
    }
134
135
    protected function formatTip(string $tip): string
136
    {
137
        return '<fg=gray>'.$tip.'</>';
138
    }
139
}
140