Completed
Push — master ( ad7ee3...c08a34 )
by Rogier
29s queued 12s
created

Api::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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\Http\Client;
13
use Rogierw\RwAcme\Support\Str;
14
15
class Api
16
{
17
    const PRODUCTION_URL = 'https://acme-v02.api.letsencrypt.org';
18
    const STAGING_URL = 'https://acme-staging-v02.api.letsencrypt.org';
19
20
    private string $baseUrl;
21
    private Client $httpClient;
22
23
    public function __construct(
24
        private readonly string $accountEmail,
25
        private string $accountKeysPath,
26
        bool $staging = false,
27
        private ?LoggerInterface $logger = null
28
    ) {
29
        $this->baseUrl = $staging ? self::STAGING_URL : self::PRODUCTION_URL;
30
        $this->httpClient = new Client();
31
    }
32
33
    public function directory(): Directory
34
    {
35
        return new Directory($this);
36
    }
37
38
    public function nonce(): Nonce
39
    {
40
        return new Nonce($this);
41
    }
42
43
    public function account(): Account
44
    {
45
        return new Account($this);
46
    }
47
48
    public function order(): Order
49
    {
50
        return new Order($this);
51
    }
52
53
    public function domainValidation(): DomainValidation
54
    {
55
        return new DomainValidation($this);
56
    }
57
58
    public function certificate(): Certificate
59
    {
60
        return new Certificate($this);
61
    }
62
63
    public function getAccountEmail(): string
64
    {
65
        return $this->accountEmail;
66
    }
67
68
    public function getAccountKeysPath(): string
69
    {
70
        if (!Str::endsWith($this->accountKeysPath, '/')) {
71
            $this->accountKeysPath .= '/';
72
        }
73
74
        if (!is_dir($this->accountKeysPath)) {
75
            mkdir($this->accountKeysPath, 0755, true);
76
        }
77
78
        return $this->accountKeysPath;
79
    }
80
81
    public function getBaseUrl(): string
82
    {
83
        return $this->baseUrl;
84
    }
85
86
    public function getHttpClient(): Client
87
    {
88
        return $this->httpClient;
89
    }
90
91
    public function setLogger(LoggerInterface $logger): self
92
    {
93
        $this->logger = $logger;
94
95
        return $this;
96
    }
97
98
    public function logger(string $level, string $message): void
99
    {
100
        if ($this->logger instanceof LoggerInterface) {
101
            $this->logger->log($level, $message);
102
        }
103
    }
104
}
105