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 ( 70b15e...4c8633 )
by Joni
04:39
created

JWK::withKeyID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JWX\JWK;
4
5
use JWX\JWK\Parameter\JWKParameter;
6
use JWX\JWK\Parameter\KeyIDParameter;
7
8
9
/**
10
 * Class to represent JWK structure.
11
 *
12
 * @link https://tools.ietf.org/html/rfc7517#section-4
13
 */
14
class JWK implements \Countable, \IteratorAggregate
15
{
16
	use TypedJWK;
17
	
18
	/**
19
	 * Parameters.
20
	 *
21
	 * @var JWKParameter[] $_parameters
22
	 */
23
	protected $_parameters;
24
	
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param JWKParameter ...$params
29
	 */
30 147
	public function __construct(JWKParameter ...$params) {
31 147
		$this->_parameters = array();
32 147
		foreach ($params as $param) {
33 135
			$this->_parameters[$param->name()] = $param;
34 147
		}
35 147
	}
36
	
37
	/**
38
	 * Initialize from an array representing a JSON object.
39
	 *
40
	 * @param array $members
41
	 * @return self
42
	 */
43 35
	public static function fromArray(array $members) {
44 35
		$params = array();
45 35
		foreach ($members as $name => $value) {
46 32
			$params[] = JWKParameter::fromNameAndValue($name, $value);
47 35
		}
48 35
		return new static(...$params);
49
	}
50
	
51
	/**
52
	 * Initialize from a JSON string.
53
	 *
54
	 * @param string $json
55
	 * @throws \UnexpectedValueException
56
	 * @return self
57
	 */
58 11 View Code Duplication
	public static function fromJSON($json) {
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...
59 11
		$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
60 11
		if (!is_array($members)) {
61 1
			throw new \UnexpectedValueException("Invalid JSON.");
62
		}
63 10
		return static::fromArray($members);
64
	}
65
	
66
	/**
67
	 * Initialize from another JWK.
68
	 *
69
	 * Allows casting to subclass by late static binding.
70
	 *
71
	 * @param JWK $jwk
72
	 * @return self
73
	 */
74 58
	public static function fromJWK(JWK $jwk) {
75 58
		return new static(...array_values($jwk->_parameters));
76
	}
77
	
78
	/**
79
	 * Get self with parameters added.
80
	 *
81
	 * @param JWKParameter ...$params
82
	 * @return self
83
	 */
84 10
	public function withParameters(JWKParameter ...$params) {
85 10
		$obj = clone $this;
86 10
		foreach ($params as $param) {
87 10
			$obj->_parameters[$param->name()] = $param;
88 10
		}
89 10
		return $obj;
90
	}
91
	
92
	/**
93
	 * Get all parameters.
94
	 *
95
	 * @return JWKParameter[]
96
	 */
97 1
	public function parameters() {
98 1
		return $this->_parameters;
99
	}
100
	
101
	/**
102
	 * Get self with given key ID added to parameters.
103
	 *
104
	 * @param string $id Key ID as a string
105
	 * @return self
106
	 */
107 4
	public function withKeyID($id) {
108 4
		return $this->withParameters(new KeyIDParameter($id));
109
	}
110
	
111
	/**
112
	 * Whether parameters are present.
113
	 *
114
	 * Returns false if any of the given parameters is not set.
115
	 *
116
	 * @param string ...$names Parameter names
117
	 * @return bool
118
	 */
119 219 View Code Duplication
	public function has(...$names) {
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...
120 219
		foreach ($names as $name) {
121 219
			if (!isset($this->_parameters[$name])) {
122 52
				return false;
123
			}
124 208
		}
125 207
		return true;
126
	}
127
	
128
	/**
129
	 * Get a parameter.
130
	 *
131
	 * @param string $name Parameter name
132
	 * @throws \LogicException
133
	 * @return JWKParameter
134
	 */
135 186
	public function get($name) {
136 186
		if (!$this->has($name)) {
137 1
			throw new \LogicException("Parameter $name doesn't exists.");
138
		}
139 185
		return $this->_parameters[$name];
140
	}
141
	
142
	/**
143
	 * Convert to array.
144
	 *
145
	 * @return array Parameter values keyed by parameter names
146
	 */
147 4
	public function toArray() {
148 4
		$a = array();
149 4
		foreach ($this->_parameters as $param) {
150 1
			$a[$param->name()] = $param->value();
151 4
		}
152 4
		return $a;
153
	}
154
	
155
	/**
156
	 * Convert to JSON.
157
	 *
158
	 * @return string
159
	 */
160 2
	public function toJSON() {
161 2
		$data = $this->toArray();
162 2
		if (empty($data)) {
163 1
			return "";
164
		}
165 1
		return json_encode((object) $data, JSON_UNESCAPED_SLASHES);
166
	}
167
	
168
	/**
169
	 * Get the number of parameters.
170
	 *
171
	 * @see Countable::count()
172
	 */
173 1
	public function count() {
174 1
		return count($this->_parameters);
175
	}
176
	
177
	/**
178
	 * Get iterator for the parameters.
179
	 *
180
	 * @see IteratorAggregate::getIterator()
181
	 * @return \ArrayIterator
182
	 */
183 1
	public function getIterator() {
184 1
		return new \ArrayIterator($this->_parameters);
185
	}
186
}
187