| Total Complexity | 8 |
| Total Lines | 65 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 |
||
| 85 | } |
||
| 86 | } |
||
| 87 |