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.

CurveParameter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\Parameter;
6
7
use Sop\JWX\Parameter\Feature\StringParameterValue;
8
9
/**
10
 * Implements 'Curve' parameter.
11
 *
12
 * @see https://tools.ietf.org/html/rfc7518#section-6.2.1.1
13
 */
14
class CurveParameter extends JWKParameter
15
{
16
    use StringParameterValue;
17
18
    /**
19
     * P-256 Curve.
20
     *
21
     * @var string
22
     */
23
    public const CURVE_P256 = 'P-256';
24
25
    /**
26
     * P-384 Curve.
27
     *
28
     * @var string
29
     */
30
    public const CURVE_P384 = 'P-384';
31
32
    /**
33
     * P-521 Curve.
34
     *
35
     * @var string
36
     */
37
    public const CURVE_P521 = 'P-521';
38
39
    /**
40
     * Mapping from curve OID to curve name.
41
     *
42
     * @internal
43
     *
44
     * @var array
45
     */
46
    public const MAP_OID_TO_CURVE = [
47
        '1.2.840.10045.3.1.7' => self::CURVE_P256,
48
        '1.3.132.0.34' => self::CURVE_P384,
49
        '1.3.132.0.35' => self::CURVE_P521,
50
    ];
51
52
    /**
53
     * Mapping from curve name to bit size.
54
     *
55
     * @internal
56
     *
57
     * @var array
58
     */
59
    public const MAP_CURVE_TO_SIZE = [
60
        self::CURVE_P256 => 256,
61
        self::CURVE_P384 => 384,
62
        self::CURVE_P521 => 521,
63
    ];
64
65
    /**
66
     * Constructor.
67
     *
68
     * @param string $curve Curve name
69
     */
70 17
    public function __construct(string $curve)
71
    {
72 17
        parent::__construct(self::PARAM_CURVE, $curve);
73 17
    }
74
75
    /**
76
     * Initialize from curve OID.
77
     *
78
     * @param string $oid Object identifier in dotted format
79
     *
80
     * @throws \UnexpectedValueException If the curve is not supported
81
     */
82 7
    public static function fromOID(string $oid): self
83
    {
84 7
        if (!array_key_exists($oid, self::MAP_OID_TO_CURVE)) {
85 1
            throw new \UnexpectedValueException("OID {$oid} not supported.");
86
        }
87 6
        $curve = self::MAP_OID_TO_CURVE[$oid];
88 6
        return new self($curve);
89
    }
90
91
    /**
92
     * Get key size in bits for the curve.
93
     *
94
     * @throws \UnexpectedValueException
95
     */
96 11
    public function keySizeBits(): int
97
    {
98 11
        if (!array_key_exists($this->_value, self::MAP_CURVE_TO_SIZE)) {
99 1
            throw new \UnexpectedValueException(
100 1
                'Curve ' . $this->_value . ' not supported.');
101
        }
102 10
        return self::MAP_CURVE_TO_SIZE[$this->_value];
103
    }
104
105
    /**
106
     * Get the curve OID by curve name.
107
     *
108
     * @param string $name Curve parameter name
109
     *
110
     * @throws \UnexpectedValueException If the curve is not supported
111
     *
112
     * @return string OID in dotted format
113
     */
114 12
    public static function nameToOID(string $name): string
115
    {
116 12
        static $reverseMap;
117 12
        if (!isset($reverseMap)) {
118 1
            $reverseMap = array_flip(self::MAP_OID_TO_CURVE);
119
        }
120 12
        if (!isset($reverseMap[$name])) {
121 1
            throw new \UnexpectedValueException("Curve {$name} not supported.");
122
        }
123 11
        return $reverseMap[$name];
124
    }
125
}
126