Passed
Push — master ( 03dd19...3b2ead )
by Rogier
02:29
created

DomainValidationData::isInvalid()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Rogierw\Letsencrypt\DTO;
4
5
use Rogierw\Letsencrypt\Endpoints\DomainValidation;
6
use Rogierw\Letsencrypt\Http\Response;
7
use Rogierw\Letsencrypt\Support\Arr;
8
use Spatie\DataTransferObject\DataTransferObject;
9
10
class DomainValidationData extends DataTransferObject
11
{
12
    public $identifier;
13
    public $status;
14
    public $expires;
15
    public $file;
16
    public $dns;
17
    public $validationRecord;
18
19
    public static function fromResponse(Response $response): self
20
    {
21
        return new self([
22
            'identifier'       => $response->getBody()['identifier'],
23
            'status'           => $response->getBody()['status'],
24
            'expires'          => $response->getBody()['expires'],
25
            'file'             => self::getValidationByType($response->getBody()['challenges'], DomainValidation::TYPE_HTTP),
26
            'dns'              => self::getValidationByType($response->getBody()['challenges'], DomainValidation::TYPE_DNS),
27
            'validationRecord' => Arr::get($response->getBody(), 'validationRecord', []),
28
        ]);
29
    }
30
31
    private static function getValidationByType(array $haystack, string $type): array
32
    {
33
        foreach ($haystack as $key => $data) {
34
            if ($data['type'] === $type) {
35
                return $haystack[$key];
36
            }
37
        }
38
39
        return [];
40
    }
41
42
    public function isPending(): bool
43
    {
44
        return $this->status === 'pending';
45
    }
46
47
    public function isValid(): bool
48
    {
49
        return $this->status === 'valid';
50
    }
51
52
    public function isInvalid(): bool
53
    {
54
        return $this->status === 'invalid';
55
    }
56
}
57