Passed
Pull Request — master (#1)
by Alexandr
03:34
created

Certificate::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the LetsEncrypt ACME client.
7
 *
8
 * @author    Ivanov Aleksandr <[email protected]>
9
 * @copyright 2019
10
 * @license   https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License
11
 */
12
13
namespace LetsEncrypt\Certificate;
14
15
use LetsEncrypt\Enum\ECKeyAlgorithm;
16
use LetsEncrypt\Enum\KeyType;
17
use LetsEncrypt\Enum\RSAKeyLength;
18
use LetsEncrypt\Helper\KeyGenerator;
19
20
final class Certificate
21
{
22
    /**
23
     * @var Key
24
     */
25
    private $key;
26
27
    /**
28
     * @var \DateTimeImmutable|null
29
     */
30
    private $notBefore;
31
32
    /**
33
     * @var \DateTimeImmutable|null
34
     */
35
    private $notAfter;
36
37
    private function __construct(Key $key, ?string $notBefore, ?string $notAfter)
38
    {
39
        $this->key = $key;
40
        $this->notBefore = $notBefore !== null ? new \DateTimeImmutable($notBefore) : null;
41
        $this->notAfter = $notAfter !== null ? new \DateTimeImmutable($notAfter) : null;
42
    }
43
44
    public static function createWithRSAKey(
45
        RSAKeyLength $keyLength,
46
        string $notBefore = null,
47
        string $notAfter = null
48
    ): self {
49
        return new static(Key::rsa($keyLength), $notBefore, $notAfter);
50
    }
51
52
    public static function createWithECKey(
53
        ECKeyAlgorithm $algorithm,
54
        string $notBefore = null,
55
        string $notAfter = null
56
    ): self {
57
        return new static(Key::ec($algorithm), $notBefore, $notAfter);
58
    }
59
60
    public function getNotBefore(): string
61
    {
62
        return $this->notBefore !== null ? $this->notBefore->format(DATE_RFC3339) : '';
63
    }
64
65
    public function getNotAfter(): string
66
    {
67
        return $this->notAfter !== null ? $this->notAfter->format(DATE_RFC3339) : '';
68
    }
69
70
    public function getKeyType(): KeyType
71
    {
72
        return $this->key->getType();
73
    }
74
75
    public function generate(KeyGenerator $keyGenerator, string $privateKeyPath, string $publicKeyPath): void
76
    {
77
        $this->key->generate($keyGenerator, $privateKeyPath, $publicKeyPath);
78
    }
79
}
80