Api   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
eloc 26
c 1
b 0
f 1
dl 0
loc 96
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A account() 0 3 1
A getBaseUrl() 0 3 1
A getHttpClient() 0 8 2
A logger() 0 4 2
A nonce() 0 3 1
A setLocalAccount() 0 5 1
A order() 0 3 1
A setHttpClient() 0 5 1
A domainValidation() 0 3 1
A certificate() 0 3 1
A localAccount() 0 7 2
A setLogger() 0 5 1
A directory() 0 3 1
A __construct() 0 9 3
1
<?php
2
3
namespace Rogierw\RwAcme;
4
5
use Psr\Log\LoggerInterface;
6
use Rogierw\RwAcme\Endpoints\Account;
7
use Rogierw\RwAcme\Endpoints\Certificate;
8
use Rogierw\RwAcme\Endpoints\Directory;
9
use Rogierw\RwAcme\Endpoints\DomainValidation;
10
use Rogierw\RwAcme\Endpoints\Nonce;
11
use Rogierw\RwAcme\Endpoints\Order;
12
use Rogierw\RwAcme\Exceptions\LetsEncryptClientException;
13
use Rogierw\RwAcme\Http\Client;
14
use Rogierw\RwAcme\Interfaces\AcmeAccountInterface;
15
use Rogierw\RwAcme\Interfaces\HttpClientInterface;
16
17
class Api
18
{
19
    private const PRODUCTION_URL = 'https://acme-v02.api.letsencrypt.org';
20
    private const STAGING_URL = 'https://acme-staging-v02.api.letsencrypt.org';
21
22
    public function __construct(
23
        bool $staging = false,
24
        private ?AcmeAccountInterface $localAccount = null,
25
        private ?LoggerInterface $logger = null,
26
        private ?HttpClientInterface $httpClient = null,
27
        private ?string $baseUrl = null,
28
    ) {
29
        if (empty($this->baseUrl)) {
30
            $this->baseUrl = $staging ? self::STAGING_URL : self::PRODUCTION_URL;
31
        }
32
    }
33
34
    public function setLocalAccount(AcmeAccountInterface $account): self
35
    {
36
        $this->localAccount = $account;
37
38
        return $this;
39
    }
40
41
    public function localAccount(): AcmeAccountInterface
42
    {
43
        if ($this->localAccount === null) {
44
            throw new LetsEncryptClientException('No account set.');
45
        }
46
47
        return $this->localAccount;
48
    }
49
50
    public function directory(): Directory
51
    {
52
        return new Directory($this);
53
    }
54
55
    public function nonce(): Nonce
56
    {
57
        return new Nonce($this);
58
    }
59
60
    public function account(): Account
61
    {
62
        return new Account($this);
63
    }
64
65
    public function order(): Order
66
    {
67
        return new Order($this);
68
    }
69
70
    public function domainValidation(): DomainValidation
71
    {
72
        return new DomainValidation($this);
73
    }
74
75
    public function certificate(): Certificate
76
    {
77
        return new Certificate($this);
78
    }
79
80
    public function getBaseUrl(): string
81
    {
82
        return $this->baseUrl;
83
    }
84
85
    public function getHttpClient(): HttpClientInterface
86
    {
87
        // Create a default client if none is set.
88
        if ($this->httpClient === null) {
89
            $this->httpClient = new Client();
90
        }
91
92
        return $this->httpClient;
93
    }
94
95
    public function setHttpClient(HttpClientInterface $httpClient): self
96
    {
97
        $this->httpClient = $httpClient;
98
99
        return $this;
100
    }
101
102
    public function setLogger(LoggerInterface $logger): self
103
    {
104
        $this->logger = $logger;
105
106
        return $this;
107
    }
108
109
    public function logger(string $level, string $message, array $context = []): void
110
    {
111
        if ($this->logger instanceof LoggerInterface) {
112
            $this->logger->log($level, $message, $context);
113
        }
114
    }
115
}
116