Dir   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 36
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCEK() 0 15 4
A name() 0 4 1
A allowedKeyTypes() 0 4 1
A getKeyManagementMode() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption\Algorithm\KeyEncryption;
15
16
use Base64Url\Base64Url;
17
use InvalidArgumentException;
18
use Jose\Component\Core\JWK;
19
20
final class Dir implements DirectEncryption
21
{
22
    /**
23
     * @throws InvalidArgumentException if the key is invalid
24
     */
25
    public function getCEK(JWK $key): string
26
    {
27
        if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
28
            throw new InvalidArgumentException('Wrong key type.');
29
        }
30
        if (!$key->has('k')) {
31
            throw new InvalidArgumentException('The key parameter "k" is missing.');
32
        }
33
        $k = $key->get('k');
34
        if (!\is_string($k)) {
35
            throw new InvalidArgumentException('The key parameter "k" is invalid.');
36
        }
37
38
        return Base64Url::decode($k);
39
    }
40
41
    public function name(): string
42
    {
43
        return 'dir';
44
    }
45
46
    public function allowedKeyTypes(): array
47
    {
48
        return ['oct'];
49
    }
50
51
    public function getKeyManagementMode(): string
52
    {
53
        return self::MODE_DIRECT;
54
    }
55
}
56