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
|
|
|
private string $baseUrl; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
bool $staging = false, |
26
|
|
|
private ?AcmeAccountInterface $localAccount = null, |
27
|
|
|
private ?LoggerInterface $logger = null, |
28
|
|
|
private HttpClientInterface|null $httpClient = null, |
29
|
|
|
) { |
30
|
|
|
$this->baseUrl = $staging ? self::STAGING_URL : self::PRODUCTION_URL; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setLocalAccount(AcmeAccountInterface $account): self |
34
|
|
|
{ |
35
|
|
|
$this->localAccount = $account; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function localAccount(): AcmeAccountInterface |
41
|
|
|
{ |
42
|
|
|
if ($this->localAccount === null) { |
43
|
|
|
throw new LetsEncryptClientException('No account set.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->localAccount; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function directory(): Directory |
50
|
|
|
{ |
51
|
|
|
return new Directory($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function nonce(): Nonce |
55
|
|
|
{ |
56
|
|
|
return new Nonce($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function account(): Account |
60
|
|
|
{ |
61
|
|
|
return new Account($this); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function order(): Order |
65
|
|
|
{ |
66
|
|
|
return new Order($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function domainValidation(): DomainValidation |
70
|
|
|
{ |
71
|
|
|
return new DomainValidation($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function certificate(): Certificate |
75
|
|
|
{ |
76
|
|
|
return new Certificate($this); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getBaseUrl(): string |
80
|
|
|
{ |
81
|
|
|
return $this->baseUrl; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getHttpClient(): HttpClientInterface |
85
|
|
|
{ |
86
|
|
|
// Create a default client if none is set. |
87
|
|
|
if ($this->httpClient === null) { |
88
|
|
|
$this->httpClient = new Client(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->httpClient; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setHttpClient(HttpClientInterface $httpClient): self |
95
|
|
|
{ |
96
|
|
|
$this->httpClient = $httpClient; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setLogger(LoggerInterface $logger): self |
102
|
|
|
{ |
103
|
|
|
$this->logger = $logger; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function logger(string $level, string $message, array $context = []): void |
109
|
|
|
{ |
110
|
|
|
if ($this->logger instanceof LoggerInterface) { |
111
|
|
|
$this->logger->log($level, $message, $context); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|