Certificate::revoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 31
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 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\Exceptions\LetsEncryptClientException;
8
use Rogierw\RwAcme\Support\Base64;
9
10
class Certificate extends Endpoint
11
{
12
    public function getBundle(OrderData $orderData): CertificateBundleData
13
    {
14
        $signedPayload = $this->createKeyId($orderData->accountUrl, $orderData->certificateUrl);
0 ignored issues
show
Bug introduced by
It seems like $orderData->certificateUrl can also be of type null; however, parameter $url of Rogierw\RwAcme\Endpoints\Endpoint::createKeyId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
        $signedPayload = $this->createKeyId($orderData->accountUrl, /** @scrutinizer ignore-type */ $orderData->certificateUrl);
Loading history...
15
16
        $response = $this->client->getHttpClient()->post($orderData->certificateUrl, $signedPayload);
0 ignored issues
show
Bug introduced by
It seems like $orderData->certificateUrl can also be of type null; however, parameter $url of Rogierw\RwAcme\Interface...ClientInterface::post() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
        $response = $this->client->getHttpClient()->post(/** @scrutinizer ignore-type */ $orderData->certificateUrl, $signedPayload);
Loading history...
17
18
        if ($response->getHttpResponseCode() !== 200) {
19
            $this->logResponse('error', 'Failed to fetch certificate', $response);
20
21
            throw new LetsEncryptClientException('Failed to fetch certificate.');
22
        }
23
24
        return CertificateBundleData::fromResponse($response);
25
    }
26
27
    public function revoke(string $pem, int $reason = 0): bool
28
    {
29
        if (($data = openssl_x509_read($pem)) === false) {
30
            throw new LetsEncryptClientException('Could not parse the certificate.');
31
        }
32
33
        if (openssl_x509_export($data, $certificate) === false) {
34
            throw new LetsEncryptClientException('Could not export the certificate.');
35
        }
36
37
        preg_match('~-----BEGIN\sCERTIFICATE-----(.*)-----END\sCERTIFICATE-----~s', $certificate, $matches);
38
        $certificate = trim(Base64::urlSafeEncode(base64_decode(trim($matches[1]))));
39
40
        $revokeUrl = $this->client->directory()->revoke();
41
42
        $signedPayload = $this->createKeyId(
43
            $this->client->account()->get()->url,
44
            $revokeUrl,
45
            [
46
                'certificate' => $certificate,
47
                'reason' => $reason,
48
            ]
49
        );
50
51
        $response = $this->client->getHttpClient()->post($revokeUrl, $signedPayload);
52
53
        if ($response->getHttpResponseCode() !== 200) {
54
            $this->logResponse('error', 'Failed to revoke certificate', $response);
55
        }
56
57
        return $response->getHttpResponseCode() === 200;
58
    }
59
}
60