GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

PrivateKeyJWK   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 7
dl 0
loc 42
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromPrivateKeyInfo() 0 3 1
A fromPrivateKey() 0 9 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\Asymmetric;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\EC\ECPrivateKey;
9
use Sop\CryptoTypes\Asymmetric\PrivateKey;
10
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo;
11
use Sop\CryptoTypes\Asymmetric\RSA\RSAPrivateKey;
12
use Sop\JWX\JWK\EC\ECPrivateKeyJWK;
13
use Sop\JWX\JWK\JWK;
14
use Sop\JWX\JWK\RSA\RSAPrivateKeyJWK;
15
16
/**
17
 * Base class for JWK private keys of an asymmetric key pairs.
18
 */
19
abstract class PrivateKeyJWK extends JWK
20
{
21
    /**
22
     * Get the public key component of the asymmetric key pair.
23
     */
24
    abstract public function publicKey(): PublicKeyJWK;
25
26
    /**
27
     * Convert private key to PEM.
28
     */
29
    abstract public function toPEM(): PEM;
30
31
    /**
32
     * Initialize from a PrivateKey object.
33
     *
34
     * @param PrivateKey $priv_key Private key
35
     *
36
     * @throws \UnexpectedValueException
37
     *
38
     * @return self
39
     */
40 3
    public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK
41
    {
42 3
        if ($priv_key instanceof RSAPrivateKey) {
43 1
            return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key);
44
        }
45 2
        if ($priv_key instanceof ECPrivateKey) {
46 1
            return ECPrivateKeyJWK::fromECPrivateKey($priv_key);
47
        }
48 1
        throw new \UnexpectedValueException('Unsupported private key.');
49
    }
50
51
    /**
52
     * Initialize from a PrivateKeyInfo object.
53
     *
54
     * @param PrivateKeyInfo $pki PrivateKeyInfo
55
     *
56
     * @return self
57
     */
58 2
    public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK
59
    {
60 2
        return self::fromPrivateKey($pki->privateKey());
61
    }
62
}
63