Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

CertificateBundleData::fromResponse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace Rogierw\RwAcme\DTO;
4
5
use Rogierw\RwAcme\Http\Response;
6
use Spatie\DataTransferObject\DataTransferObject;
7
8
class CertificateBundleData extends DataTransferObject
9
{
10
    public $certificate;
11
    public $fullchain;
12
13
    public static function fromResponse(Response $response): self
14
    {
15
        $certificate = '';
16
        $fullchain = '';
17
18
        if (preg_match_all('~(-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----)~i', $response->getBody(), $matches)) {
19
            $certificate = $matches[0][0];
20
21
            if (count($matches[0]) > 1) {
22
                $fullchain = $matches[0][0] . "\n";
23
24
                for ($i = 1; $i < count($matches[0]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
25
                    $fullchain .= $matches[0][$i] . "\n";
26
                }
27
            }
28
        }
29
30
        return new self(compact('certificate', 'fullchain'));
31
    }
32
}
33