Passed
Push — master ( c37c01...0b6156 )
by Alexandr
03:26
created

Key   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 65
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setAlgorithm() 0 5 1
A __construct() 0 3 1
A setLength() 0 5 1
A ec() 0 5 1
A rsa() 0 5 1
A generate() 0 13 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 Key
21
{
22
    /**
23
     * @var KeyType
24
     */
25
    private $type;
26
27
    /**
28
     * @var RSAKeyLength
29
     */
30
    private $length;
31
32
    /**
33
     * @var ECKeyAlgorithm
34
     */
35
    private $algorithm;
36
37
    private function __construct(KeyType $type)
38
    {
39
        $this->type = $type;
40
    }
41
42
    public static function rsa(RSAKeyLength $length): self
43
    {
44
        $key = new static(KeyType::rsa());
45
46
        return $key->setLength($length);
47
    }
48
49
    public static function ec(ECKeyAlgorithm $algorithm): self
50
    {
51
        $key = new static(KeyType::ec());
52
53
        return $key->setAlgorithm($algorithm);
54
    }
55
56
    public function generate(KeyGenerator $keyGenerator, string $privateKeyPath, string $publicKeyPath): void
57
    {
58
        if ($this->type->isEqual('rsa')) {
59
            $keyGenerator->rsa(
60
                $privateKeyPath,
61
                $publicKeyPath,
62
                $this->length
63
            );
64
        } elseif ($this->type->isEqual('ec')) {
65
            $keyGenerator->ec(
66
                $privateKeyPath,
67
                $publicKeyPath,
68
                $this->algorithm
69
            );
70
        }
71
    }
72
73
    private function setLength(RSAKeyLength $length): self
74
    {
75
        $this->length = $length;
76
77
        return $this;
78
    }
79
80
    private function setAlgorithm(ECKeyAlgorithm $algorithm): self
81
    {
82
        $this->algorithm = $algorithm;
83
84
        return $this;
85
    }
86
}
87