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.

SignatureAlgorithmFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 78
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A _algoClassByName() 0 7 2
A algoByKey() 0 5 1
A algoByKeys() 0 10 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWS\Algorithm;
6
7
use Sop\JWX\JWA\JWA;
8
use Sop\JWX\JWK\JWK;
9
use Sop\JWX\JWK\JWKSet;
10
use Sop\JWX\JWS\SignatureAlgorithm;
11
use Sop\JWX\JWT\Header\Header;
12
13
/**
14
 * Factory class to construct signature algorithm instances.
15
 */
16
class SignatureAlgorithmFactory
17
{
18
    /**
19
     * Mapping from algorithm name to class name.
20
     *
21
     * @internal
22
     *
23
     * @var array
24
     */
25
    public const MAP_ALGO_TO_CLASS = [
26
        JWA::ALGO_HS256 => HS256Algorithm::class,
27
        JWA::ALGO_HS384 => HS384Algorithm::class,
28
        JWA::ALGO_HS512 => HS512Algorithm::class,
29
        JWA::ALGO_RS256 => RS256Algorithm::class,
30
        JWA::ALGO_RS384 => RS384Algorithm::class,
31
        JWA::ALGO_RS512 => RS512Algorithm::class,
32
        JWA::ALGO_ES256 => ES256Algorithm::class,
33
        JWA::ALGO_ES384 => ES384Algorithm::class,
34
        JWA::ALGO_ES512 => ES512Algorithm::class,
35
    ];
36
    /**
37
     * Header.
38
     *
39
     * @var Header
40
     */
41
    protected $_header;
42
43
    /**
44
     * Constructor.
45
     */
46 14
    public function __construct(Header $header)
47
    {
48 14
        $this->_header = $header;
49 14
    }
50
51
    /**
52
     * Get signature algorithm by given JWK.
53
     */
54 11
    public function algoByKey(JWK $jwk): SignatureAlgorithm
55
    {
56 11
        $alg = JWA::deriveAlgorithmName($this->_header, $jwk);
57 11
        $cls = self::_algoClassByName($alg);
58 10
        return $cls::fromJWK($jwk, $this->_header);
59
    }
60
61
    /**
62
     * Get signature algorithm using a matching key from given JWK set.
63
     *
64
     * @throws \UnexpectedValueException If a key cannot be found
65
     */
66 7
    public function algoByKeys(JWKSet $set): SignatureAlgorithm
67
    {
68 7
        if (!$this->_header->hasKeyID()) {
69 1
            throw new \UnexpectedValueException('No key ID paremeter.');
70
        }
71 6
        $id = $this->_header->keyID()->value();
72 6
        if (!$set->hasKeyID($id)) {
73 2
            throw new \UnexpectedValueException("No key for ID '{$id}'.");
74
        }
75 4
        return $this->algoByKey($set->keyByID($id));
76
    }
77
78
    /**
79
     * Get the algorithm implementation class name by an algorithm name.
80
     *
81
     * @param string $alg Algorithm name
82
     *
83
     * @throws \UnexpectedValueException
84
     *
85
     * @return string Class name
86
     */
87 11
    private static function _algoClassByName(string $alg): string
88
    {
89 11
        if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
90 1
            throw new \UnexpectedValueException(
91 1
                "Algorithm '{$alg}' not supported.");
92
        }
93 10
        return self::MAP_ALGO_TO_CLASS[$alg];
94
    }
95
}
96