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.
Passed
Branch php72 (880eb0)
by Joni
05:58
created

RSAPublicKeyJWK::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 5
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\RSA;
6
7
use Sop\CryptoEncoding\PEM;
8
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;
9
use Sop\CryptoTypes\Asymmetric\RSA\RSAPublicKey;
10
use Sop\JWX\JWK\Asymmetric\PublicKeyJWK;
11
use Sop\JWX\JWK\Parameter\ExponentParameter;
12
use Sop\JWX\JWK\Parameter\JWKParameter;
13
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
14
use Sop\JWX\JWK\Parameter\ModulusParameter;
15
16
/**
17
 * Class representing RSA public key as a JWK.
18
 *
19
 * @see https://tools.ietf.org/html/rfc7517#section-4
20
 * @see https://tools.ietf.org/html/rfc7518#section-6.3
21
 * @see https://tools.ietf.org/html/rfc7518#section-6.3.1
22
 */
23
class RSAPublicKeyJWK extends PublicKeyJWK
24
{
25
    /**
26
     * Parameter names managed by this class.
27
     *
28
     * @internal
29
     *
30
     * @var string[]
31
     */
32
    const MANAGED_PARAMS = [
33
        JWKParameter::PARAM_KEY_TYPE,
34
        JWKParameter::PARAM_MODULUS,
35
        JWKParameter::PARAM_EXPONENT,
36
    ];
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param JWKParameter ...$params
42
     *
43
     * @throws \UnexpectedValueException If missing required parameter
44
     */
45 36
    public function __construct(JWKParameter ...$params)
46
    {
47 36
        parent::__construct(...$params);
48 36
        foreach (self::MANAGED_PARAMS as $name) {
49 36
            if (!$this->has($name)) {
50 2
                throw new \UnexpectedValueException(
51 36
                    "Missing '{$name}' parameter.");
52
            }
53
        }
54 34
        if (KeyTypeParameter::TYPE_RSA !== $this->keyTypeParameter()->value()) {
55 1
            throw new \UnexpectedValueException('Invalid key type.');
56
        }
57 33
    }
58
59
    /**
60
     * Initialize from RSAPublicKey.
61
     *
62
     * @param RSAPublicKey $pk
63
     *
64
     * @return self
65
     */
66 4
    public static function fromRSAPublicKey(RSAPublicKey $pk): self
67
    {
68 4
        $n = ModulusParameter::fromNumber($pk->modulus());
69 4
        $e = ExponentParameter::fromNumber($pk->publicExponent());
70 4
        $key_type = new KeyTypeParameter(KeyTypeParameter::TYPE_RSA);
71 4
        return new self($key_type, $n, $e);
72
    }
73
74
    /**
75
     * Initialize from PEM.
76
     *
77
     * @param PEM $pem
78
     *
79
     * @return self
80
     */
81 3
    public static function fromPEM(PEM $pem): self
82
    {
83 3
        return self::fromRSAPublicKey(RSAPublicKey::fromPEM($pem));
84
    }
85
86
    /**
87
     * Convert JWK to PEM.
88
     *
89
     * @return PEM
90
     */
91 16
    public function toPEM(): PEM
92
    {
93 16
        $n = $this->modulusParameter()->number()->base10();
94 16
        $e = $this->exponentParameter()->number()->base10();
95 16
        $pk = new RSAPublicKey($n, $e);
96 16
        return PublicKeyInfo::fromPublicKey($pk)->toPEM();
97
    }
98
}
99