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 ( 62af7f...70b15e )
by Joni
04:01
created

SignatureAlgorithmFactory::algoByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JWX\JWS\Algorithm;
4
5
use JWX\JWA\JWA;
6
use JWX\JWK\JWK;
7
use JWX\JWK\JWKSet;
8
use JWX\JWS\SignatureAlgorithm;
9
use JWX\JWT\Header\Header;
10
11
12
/**
13
 * Factory class to construct signature algorithm instances.
14
 */
15
class SignatureAlgorithmFactory
16
{
17
	/**
18
	 * Header.
19
	 *
20
	 * @var Header $_header
21
	 */
22
	protected $_header;
23
	
24
	/**
25
	 * Mapping from algorithm name to class name.
26
	 *
27
	 * @internal
28
	 *
29
	 * @var array
30
	 */
31
	const MAP_ALGO_TO_CLASS = array(
32
		/* @formatter:off */
33
		JWA::ALGO_HS256 => HS256Algorithm::class,
34
		JWA::ALGO_HS384 => HS384Algorithm::class,
35
		JWA::ALGO_HS512 => HS512Algorithm::class,
36
		JWA::ALGO_RS256 => RS256Algorithm::class,
37
		JWA::ALGO_RS384 => RS384Algorithm::class,
38
		JWA::ALGO_RS512 => RS512Algorithm::class,
39
		JWA::ALGO_ES256 => ES256Algorithm::class,
40
		JWA::ALGO_ES384 => ES384Algorithm::class,
41
		JWA::ALGO_ES512 => ES512Algorithm::class
42
		/* @formatter:on */
43
	);
44
	
45
	/**
46
	 * Constructor.
47
	 *
48
	 * @param Header $header
49
	 */
50 7
	public function __construct(Header $header) {
51 7
		$this->_header = $header;
52 7
	}
53
	
54
	/**
55
	 * Get signature algorithm by given JWK.
56
	 *
57
	 * @param JWK $jwk
58
	 * @return SignatureAlgorithm
59
	 */
60 5
	public function algoByKey(JWK $jwk) {
61 5
		$alg = JWA::deriveAlgorithmName($this->_header, $jwk);
62 5
		$cls = self::_algoClassByName($alg);
63 4
		return $cls::fromJWK($jwk, $this->_header);
64
	}
65
	
66
	/**
67
	 * Get signature algorithm using a matching key from given JWK set.
68
	 *
69
	 * @param JWKSet $set
70
	 * @throws \UnexpectedValueException If a key cannot be found
71
	 * @return SignatureAlgorithm
72
	 */
73 3 View Code Duplication
	public function algoByKeys(JWKSet $set) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 3
		if (!$this->_header->hasKeyID()) {
75 1
			throw new \UnexpectedValueException("No key ID paremeter.");
76
		}
77 2
		$id = $this->_header->keyID()->value();
78 2
		if (!$set->hasKeyID($id)) {
79 1
			throw new \UnexpectedValueException("No key for ID '$id'.");
80
		}
81 1
		return $this->algoByKey($set->keyByID($id));
82
	}
83
	
84
	/**
85
	 * Get the algorithm implementation class name by an algorithm name.
86
	 *
87
	 * @param string $alg Algorithm name
88
	 * @throws \UnexpectedValueException
89
	 * @return string Class name
90
	 */
91 5
	private static function _algoClassByName($alg) {
92 5
		if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
93 1
			throw new \UnexpectedValueException(
94 1
				"Algorithm '$alg' not supported.");
95
		}
96 4
		return self::MAP_ALGO_TO_CLASS[$alg];
97
	}
98
}
99