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

CertificateBundleData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 1
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromResponse() 0 23 4
A __construct() 0 4 1
1
<?php
2
3
namespace Rogierw\RwAcme\DTO;
4
5
use Rogierw\RwAcme\Http\Response;
6
use Spatie\LaravelData\Data;
7
8
class CertificateBundleData extends Data
9
{
10
    public function __construct(
11
        public string $certificate,
12
        public string $fullchain,
13
    ) {}
14
15
    public static function fromResponse(Response $response): CertificateBundleData
16
    {
17
        $certificate = '';
18
        $fullchain = '';
19
20
        if (preg_match_all(
21
            '~(-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----)~i',
22
            $response->getBody(),
23
            $matches
24
        )) {
25
            $certificate = $matches[0][0];
26
            $matchesCount = count($matches[0]);
27
28
            if ($matchesCount > 1) {
29
                $fullchain = $matches[0][0] . "\n";
30
31
                for ($i = 1; $i < $matchesCount; $i++) {
32
                    $fullchain .= $matches[0][$i] . "\n";
33
                }
34
            }
35
        }
36
37
        return new self(certificate: $certificate, fullchain: $fullchain);
38
    }
39
}
40