Completed
Push — master ( 899eaf...7feae9 )
by Dave
29s queued 14s
created

SeverityReader::getOptionalSeverity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Severity;
8
9
class SeverityReader
10
{
11
    /**
12
     * @param array<mixed> $data
13
     *
14
     * @throws ArrayParseException
15
     */
16
    public static function getOptionalSeverity(array $data, string $key): Severity
17
    {
18
        $severityAsStringOrNull = ArrayUtils::getOptionalStringValue($data, $key);
19
        if (null === $severityAsStringOrNull) {
20
            return Severity::error();
21
        }
22
23
        return self::getMandatorySeverity($data, $key);
24
    }
25
26
    /**
27
     * @param array<mixed> $data
28
     *
29
     * @throws ArrayParseException
30
     */
31
    public static function getMandatorySeverity(array $data, string $key): Severity
32
    {
33
        $severityAsString = ArrayUtils::getStringValue($data, $key);
34
        $lowerCaseSeverity = strtolower($severityAsString);
35
        if (!Severity::isValueValid($lowerCaseSeverity)) {
36
            throw ArrayParseException::invalidValue($key, $severityAsString);
37
        }
38
39
        return Severity::fromStringOrNull($lowerCaseSeverity);
40
    }
41
}
42