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 ( e7c776...4b2105 )
by Joni
05:35
created

KeyAlgorithmFactory::_algoClassByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JWX\JWE\KeyAlgorithm;
4
5
use JWX\JWA\JWA;
6
use JWX\JWE\KeyManagementAlgorithm;
7
use JWX\JWK\JWK;
8
use JWX\JWK\JWKSet;
9
use JWX\JWT\Header\Header;
10
11
12
/**
13
 * Factory class to construct key management algorithm instances.
14
 */
15
class KeyAlgorithmFactory
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_A128KW => A128KWAlgorithm::class,
34
		JWA::ALGO_A192KW => A192KWAlgorithm::class,
35
		JWA::ALGO_A128KW => A256KWAlgorithm::class,
36
		JWA::ALGO_A128GCMKW => A128GCMKWAlgorithm::class,
37
		JWA::ALGO_A192GCMKW => A192GCMKWAlgorithm::class,
38
		JWA::ALGO_A256GCMKW => A256GCMKWAlgorithm::class,
39
		JWA::ALGO_PBES2_HS256_A128KW => PBES2HS256A128KWAlgorithm::class,
40
		JWA::ALGO_PBES2_HS384_A192KW => PBES2HS384A192KWAlgorithm::class,
41
		JWA::ALGO_PBES2_HS512_A256KW => PBES2HS512A256KWAlgorithm::class,
42
		JWA::ALGO_DIR => DirectCEKAlgorithm::class,
43
		JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class,
44
		JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class
45
		/* @formatter:on */
46
	);
47
	
48
	/**
49
	 * Constructor.
50
	 *
51
	 * @param Header $header
52
	 */
53 6
	public function __construct(Header $header) {
54 6
		$this->_header = $header;
55 6
	}
56
	
57
	/**
58
	 * Get key management algorithm by given JWK.
59
	 *
60
	 * @param JWK $jwk
61
	 * @return KeyManagementAlgorithm
62
	 */
63 4
	public function algoByKey(JWK $jwk) {
64 4
		$alg = JWA::deriveAlgorithmName($this->_header, $jwk);
65 4
		$cls = self::_algoClassByName($alg);
66 3
		return $cls::fromJWK($jwk, $this->_header);
67
	}
68
	
69
	/**
70
	 * Get key management algorithm using a matching key from given JWK set.
71
	 *
72
	 * @param JWKSet $set
73
	 * @throws \UnexpectedValueException If a key cannot be found
74
	 * @return KeyManagementAlgorithm
75
	 */
76 3
	public function algoByKeys(JWKSet $set) {
77 3
		if (!$this->_header->hasKeyID()) {
78 1
			throw new \UnexpectedValueException("No key ID paremeter.");
79
		}
80 2
		$id = $this->_header->keyID()->value();
81 2
		if (!$set->hasKeyID($id)) {
82 1
			throw new \UnexpectedValueException("No key for ID '$id'.");
83
		}
84 1
		return $this->algoByKey($set->keyByID($id));
85
	}
86
	
87
	/**
88
	 * Get algorithm implementation class name by algorithm name.
89
	 *
90
	 * @param string $alg Algorithm name
91
	 * @throws \UnexpectedValueException
92
	 * @return string Class name
93
	 */
94 4 View Code Duplication
	private static function _algoClassByName($alg) {
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...
95 4
		if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
96 1
			throw new \UnexpectedValueException(
97 1
				"Algorithm '$alg' not supported.");
98
		}
99 3
		return self::MAP_ALGO_TO_CLASS[$alg];
100
	}
101
}
102