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

Api::getHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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