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

Order   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 47
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A finalize() 0 34 4
A get() 0 13 1
A new() 0 35 4
1
<?php
2
3
namespace Rogierw\RwAcme\Endpoints;
4
5
use Rogierw\RwAcme\DTO\AccountData;
6
use Rogierw\RwAcme\DTO\OrderData;
7
use Rogierw\RwAcme\Support\Base64;
8
use RuntimeException;
9
10
class Order extends Endpoint
11
{
12
    public function new(AccountData $accountData, array $domains): OrderData
13
    {
14
        $identifiers = [];
15
        foreach ($domains as $domain) {
16
            if (preg_match_all('~(\*\.)~', $domain) > 1) {
17
                throw new RuntimeException('Cannot create orders with multiple wildcards in one domain.');
18
            }
19
20
            $identifiers[] = [
21
                'type'  => 'dns',
22
                'value' => $domain,
23
            ];
24
        }
25
26
        $payload = [
27
            'identifiers' => $identifiers,
28
            'notBefore'   => '',
29
            'notAfter'    => '',
30
        ];
31
32
        $newOrderUrl = $this->client->directory()->newOrder();
33
34
        $keyId = $this->createKeyId(
35
            $accountData->url,
36
            $this->client->directory()->newOrder(),
37
            $payload
38
        );
39
40
        $response = $this->client->getHttpClient()->post($newOrderUrl, $keyId);
41
42
        if ($response->getHttpResponseCode() !== 201) {
43
            throw new RuntimeException('Creating new order failed; bad response code.');
44
        }
45
46
        return OrderData::fromResponse($response, $accountData->url);
47
    }
48
49
    public function get(string $id): OrderData
50
    {
51
        $account = $this->client->account()->get();
52
53
        $orderUrl = vsprintf('%s%s/%s', [
54
            $this->client->directory()->getOrder(),
55
            $account->id,
56
            $id,
57
        ]);
58
59
        $response = $this->client->getHttpClient()->get($orderUrl);
60
61
        return OrderData::fromResponse($response, $account->url);
62
    }
63
64
    public function finalize(OrderData $orderData, string $csr): bool
65
    {
66
        if (!$orderData->isReady()) {
67
            $this->client->logger(
68
                'error',
69
                "Order status for {$orderData->id} is {$orderData->status}. Cannot finalize order."
70
            );
71
72
            return false;
73
        }
74
75
        if (preg_match('~-----BEGIN\sCERTIFICATE\sREQUEST-----(.*)-----END\sCERTIFICATE\sREQUEST-----~s', $csr, $matches)) {
76
            $csr = $matches[1];
77
        }
78
79
        $csr = trim(Base64::urlSafeEncode(base64_decode($csr)));
80
81
        $signedPayload = $this->createKeyId(
82
            $orderData->accountUrl,
83
            $orderData->finalizeUrl,
84
            compact('csr')
85
        );
86
87
        $response = $this->client->getHttpClient()->post($orderData->finalizeUrl, $signedPayload);
88
89
        if ($response->getHttpResponseCode() === 200) {
90
            $orderData->setCertificateUrl($response->getBody()['certificate']);
91
92
            return true;
93
        }
94
95
        $this->client->logger('error', 'Finalize order: ' . json_encode($response->getBody()));
96
97
        return false;
98
    }
99
}
100