Passed
Push — master ( 391c9f...ea11ea )
by Laurens
08:43
created

AbstractFactory::extractDateTimeImmutableOrNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\__shared;
6
7
use DateTimeImmutable;
8
9
abstract class AbstractFactory
10
{
11 9
    protected function extractString(string $key, array $data): ?string
12
    {
13 9
        return $data[$key];
14
    }
15
16 3
    protected function extractStringOrNull(string $key, array $data): ?string
17
    {
18 3
        return $this->extractOrNull($key, $data);
19
    }
20
21 2
    protected function extractInteger(string $key, array $data): int
22
    {
23 2
        return (int)$data[$key];
24
    }
25
26
    protected function extractIntegerOrNull(string $key, array $data): ?int
27
    {
28
        return (int)$this->extractOrNull($key, $data) ?? null;
29
    }
30
31 2
    protected function extractFloat(string $key, array $data): float
32
    {
33 2
        return (float)$data[$key];
34
    }
35
36
    protected function extractFloatOrNull(string $key, array $data): ?float
37
    {
38
        return (float)$this->extractOrNull($key, $data) ?? null;
39
    }
40
41 9
    protected function extractBoolean(string $key, array $data): bool
42
    {
43 9
        return (bool)$this->extractOrNull($key, $data);
44
    }
45
46 6
    protected function extractArrayOrNull(string $key, array $data): ?array
47
    {
48 6
        return $this->extractOrNull($key, $data);
49
    }
50
51 3
    protected function extractDateTimeImmutableOrNull(string $key, array $data): ?DateTimeImmutable
52
    {
53 3
        $dateTime = $this->extractStringOrNull($key, $data);
54
55 3
        if ($dateTime) {
56 1
            return new DateTimeImmutable($dateTime);
57
        }
58
59 3
        return null;
60
    }
61
62 9
    private function extractOrNull(string $key, array $data)
63
    {
64 9
        if (array_key_exists($key, $data)) {
65 9
            return $data[$key];
66
        }
67
68 7
        return null;
69
    }
70
}
71