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
|
|
|
|