Passed
Push — master ( 199cf7...129662 )
by Rogier
12:20
created

DomainValidationData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 6
1
<?php
2
3
namespace Rogierw\RwAcme\DTO;
4
5
use Rogierw\RwAcme\Enums\AuthorizationChallengeEnum;
6
use Rogierw\RwAcme\Http\Response;
7
use Rogierw\RwAcme\Support\Arr;
8
use Spatie\LaravelData\Data;
9
10
class DomainValidationData extends Data
11
{
12
    public function __construct(
13
        public array $identifier,
14
        public string $status,
15
        public string $expires,
16
        public array $file,
17
        public array $dns,
18
        public array $validationRecord,
19
    ) {}
20
21
    public static function fromResponse(Response $response): DomainValidationData
22
    {
23
        return new self(
24
            identifier: $response->getBody()['identifier'],
25
            status: $response->getBody()['status'],
26
            expires: $response->getBody()['expires'],
27
            file: self::getValidationByType($response->getBody()['challenges'], AuthorizationChallengeEnum::HTTP),
28
            dns: self::getValidationByType($response->getBody()['challenges'], AuthorizationChallengeEnum::DNS),
29
            validationRecord: Arr::get($response->getBody(), 'validationRecord', []),
30
        );
31
    }
32
33
    private static function getValidationByType(array $haystack, AuthorizationChallengeEnum $authChallenge): array
34
    {
35
        foreach ($haystack as $key => $data) {
36
            if ($data['type'] === $authChallenge->value) {
37
                return $data;
38
            }
39
        }
40
41
        return [];
42
    }
43
44
    public function isPending(): bool
45
    {
46
        return $this->status === 'pending';
47
    }
48
49
    public function isValid(): bool
50
    {
51
        return $this->status === 'valid';
52
    }
53
54
    public function isInvalid(): bool
55
    {
56
        return $this->status === 'invalid';
57
    }
58
59
    public function hasErrors(): bool
60
    {
61
        if (array_key_exists('error', $this->file) && !empty($this->file['error'])) {
62
            return true;
63
        }
64
65
        if (array_key_exists('error', $this->dns) && !empty($this->dns['error'])) {
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    public function getErrors(): array
73
    {
74
        if ($this->hasErrors()) {
75
            $data = [];
76
77
            $data[] = [
78
                'domainValidationType' => AuthorizationChallengeEnum::HTTP->value,
79
                'error' => Arr::get($this->file, 'error'),
80
            ];
81
82
            $data[] = [
83
                'domainValidationType' => AuthorizationChallengeEnum::DNS->value,
84
                'error' => Arr::get($this->dns, 'error'),
85
            ];
86
87
            return $data;
88
        }
89
90
        return [];
91
    }
92
}
93