DomainValidationData::isInvalid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
22
    public static function fromResponse(Response $response): DomainValidationData
23
    {
24
        return new self(
25
            identifier: $response->getBody()['identifier'],
26
            status: $response->getBody()['status'],
27
            expires: $response->getBody()['expires'],
28
            file: self::getValidationByType($response->getBody()['challenges'], AuthorizationChallengeEnum::HTTP),
29
            dns: self::getValidationByType($response->getBody()['challenges'], AuthorizationChallengeEnum::DNS),
30
            validationRecord: Arr::get($response->getBody(), 'validationRecord', []),
31
        );
32
    }
33
34
    private static function getValidationByType(array $haystack, AuthorizationChallengeEnum $authChallenge): array
35
    {
36
        foreach ($haystack as $key => $data) {
37
            if ($data['type'] === $authChallenge->value) {
38
                return $data;
39
            }
40
        }
41
42
        return [];
43
    }
44
45
    public function isPending(): bool
46
    {
47
        return $this->status === 'pending';
48
    }
49
50
    public function isValid(): bool
51
    {
52
        return $this->status === 'valid';
53
    }
54
55
    public function isInvalid(): bool
56
    {
57
        return $this->status === 'invalid';
58
    }
59
60
    public function hasErrors(): bool
61
    {
62
        if (array_key_exists('error', $this->file) && !empty($this->file['error'])) {
63
            return true;
64
        }
65
66
        if (array_key_exists('error', $this->dns) && !empty($this->dns['error'])) {
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    public function getErrors(): array
74
    {
75
        if ($this->hasErrors()) {
76
            $data = [];
77
78
            $data[] = [
79
                'domainValidationType' => AuthorizationChallengeEnum::HTTP->value,
80
                'error' => Arr::get($this->file, 'error'),
81
            ];
82
83
            $data[] = [
84
                'domainValidationType' => AuthorizationChallengeEnum::DNS->value,
85
                'error' => Arr::get($this->dns, 'error'),
86
            ];
87
88
            return $data;
89
        }
90
91
        return [];
92
    }
93
}
94