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.
Completed
Push — master ( 59f4b4...e271c1 )
by Joni
03:51
created

PrivateKeyJWK   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 7
dl 0
loc 46
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
     * @return PublicKeyJWK
25
     */
26
    abstract public function publicKey(): PublicKeyJWK;
27
28
    /**
29
     * Convert private key to PEM.
30
     *
31
     * @return PEM
32
     */
33
    abstract public function toPEM(): PEM;
34
35
    /**
36
     * Initialize from a PrivateKey object.
37
     *
38
     * @param PrivateKey $priv_key Private key
39
     *
40
     * @throws \UnexpectedValueException
41
     *
42
     * @return self
43
     */
44 3
    public static function fromPrivateKey(PrivateKey $priv_key): PrivateKeyJWK
45
    {
46 3
        if ($priv_key instanceof RSAPrivateKey) {
47 1
            return RSAPrivateKeyJWK::fromRSAPrivateKey($priv_key);
48
        }
49 2
        if ($priv_key instanceof ECPrivateKey) {
50 1
            return ECPrivateKeyJWK::fromECPrivateKey($priv_key);
51
        }
52 1
        throw new \UnexpectedValueException('Unsupported private key.');
53
    }
54
55
    /**
56
     * Initialize from a PrivateKeyInfo object.
57
     *
58
     * @param PrivateKeyInfo $pki PrivateKeyInfo
59
     *
60
     * @return self
61
     */
62 2
    public static function fromPrivateKeyInfo(PrivateKeyInfo $pki): PrivateKeyJWK
63
    {
64 2
        return self::fromPrivateKey($pki->privateKey());
65
    }
66
}
67