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.

SymmetricKeyJWK   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 54
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A key() 0 3 1
A fromKey() 0 5 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWK\Symmetric;
6
7
use Sop\JWX\JWK\JWK;
8
use Sop\JWX\JWK\Parameter\JWKParameter;
9
use Sop\JWX\JWK\Parameter\KeyTypeParameter;
10
use Sop\JWX\JWK\Parameter\KeyValueParameter;
11
use Sop\JWX\Util\Base64;
12
13
/**
14
 * JWK containing a symmetric key.
15
 *
16
 * @see http://tools.ietf.org/html/rfc7518#section-6.4
17
 */
18
class SymmetricKeyJWK extends JWK
19
{
20
    /**
21
     * Parameter names managed by this class.
22
     *
23
     * @internal
24
     *
25
     * @var string[]
26
     */
27
    public const MANAGED_PARAMS = [
28
        JWKParameter::PARAM_KEY_TYPE,
29
        JWKParameter::PARAM_KEY_VALUE,
30
    ];
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param JWKParameter ...$params
36
     *
37
     * @throws \UnexpectedValueException If missing required parameter
38
     */
39 69
    public function __construct(JWKParameter ...$params)
40
    {
41 69
        parent::__construct(...$params);
42 69
        foreach (self::MANAGED_PARAMS as $name) {
43 69
            if (!$this->has($name)) {
44 1
                throw new \UnexpectedValueException(
45 69
                    "Missing '{$name}' parameter.");
46
            }
47
        }
48 68
        if (KeyTypeParameter::TYPE_OCT !== $this->keyTypeParameter()->value()) {
49 1
            throw new \UnexpectedValueException('Invalid key type.');
50
        }
51 67
    }
52
53
    /**
54
     * Initialize from a key string.
55
     *
56
     * @param string       $key       Symmetric key
57
     * @param JWKParameter ...$params Optional additional parameters
58
     */
59 39
    public static function fromKey(string $key, JWKParameter ...$params): self
60
    {
61 39
        $params[] = new KeyTypeParameter(KeyTypeParameter::TYPE_OCT);
62 39
        $params[] = KeyValueParameter::fromString($key);
63 39
        return new self(...$params);
64
    }
65
66
    /**
67
     * Get the symmetric key.
68
     */
69 39
    public function key(): string
70
    {
71 39
        return Base64::urlDecode($this->keyValueParameter()->value());
72
    }
73
}
74