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

Certificate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 21
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A revoke() 0 31 4
A getBundle() 0 9 2
1
<?php
2
3
namespace Rogierw\RwAcme\Endpoints;
4
5
use Rogierw\RwAcme\DTO\CertificateBundleData;
6
use Rogierw\RwAcme\DTO\OrderData;
7
use Rogierw\RwAcme\Support\Base64;
8
use RuntimeException;
9
10
class Certificate extends Endpoint
11
{
12
    public function getBundle(OrderData $orderData): CertificateBundleData
13
    {
14
        $response = $this->client->getHttpClient()->get($orderData->certificateUrl);
15
16
        if ($response->getHttpResponseCode() !== 200) {
17
            throw new RuntimeException('Failed to fetch certificate.');
18
        }
19
20
        return CertificateBundleData::fromResponse($response);
21
    }
22
23
    public function revoke(string $pem, int $reason = 0): bool
24
    {
25
        if (($data = openssl_x509_read($pem)) === false) {
26
            throw new RuntimeException('Could not parse the certificate.');
27
        }
28
29
        if (openssl_x509_export($data, $certificate) === false) {
30
            throw new RuntimeException('Could not export the certificate.');
31
        }
32
33
        preg_match('~-----BEGIN\sCERTIFICATE-----(.*)-----END\sCERTIFICATE-----~s', $certificate, $matches);
34
        $certificate = trim(Base64::urlSafeEncode(base64_decode(trim($matches[1]))));
35
36
        $revokeUrl = $this->client->directory()->revoke();
37
38
        $signedPayload = $this->createKeyId(
39
            $this->client->account()->get()->url,
40
            $revokeUrl,
41
            [
42
                'certificate' => $certificate,
43
                'reason'      => $reason,
44
            ]
45
        );
46
47
        $response = $this->client->getHttpClient()->post($revokeUrl, $signedPayload);
48
49
        if ($response->getHttpResponseCode() === 200) {
50
            return true;
51
        }
52
53
        return false;
54
    }
55
}
56