CertificateBundleData   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 16
c 2
b 1
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromResponse() 0 25 4
A __construct() 0 5 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
        public string $caBundle,
14
    ) {
15
    }
16
17
    public static function fromResponse(Response $response): CertificateBundleData
18
    {
19
        $certificate = '';
20
        $fullchain = '';
21
        $caBundle = '';
22
23
        if (preg_match_all(
24
            '~(-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----)~i',
25
            $response->getBody(),
26
            $matches
27
        )) {
28
            $certificate = $matches[0][0];
29
            $matchesCount = count($matches[0]);
30
31
            if ($matchesCount > 1) {
32
                $fullchain = $matches[0][0] . "\n";
33
34
                for ($i = 1; $i < $matchesCount; $i++) {
35
                    $caBundle .= $matches[0][$i] . "\n";
36
                    $fullchain .= $matches[0][$i] . "\n";
37
                }
38
            }
39
        }
40
41
        return new self(certificate: $certificate, fullchain: $fullchain, caBundle: $caBundle);
42
    }
43
}
44