Completed
Push — master ( 6593bf...678353 )
by Florent
02:22
created

HasKeyChecker::checkKeyUsage()   C

Complexity

Conditions 22
Paths 20

Size

Total Lines 58
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
rs 6.3229
cc 22
eloc 39
nc 20
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Behaviour;
13
14
use Jose\Object\JWKInterface;
15
16
trait HasKeyChecker
17
{
18
    /**
19
     * @param \Jose\Object\JWKInterface $key
20
     * @param string                    $usage
21
     *
22
     * @throws \InvalidArgumentException
23
     *
24
     * @return bool
25
     */
26
    private function checkKeyUsage(JWKInterface $key, $usage)
27
    {
28
        if (!$key->has('use') && !$key->has('key_ops')) {
29
            return true;
30
        }
31
        if ($key->has('use')) {
32
            $use = $key->get('use');
33
            switch ($usage) {
34
                case 'verification':
35
                case 'signature':
36
                    if ('sig' === $use) {
37
                        return true;
38
                    }
39
40
                    return false;
41
                case 'encryption':
42
                case 'decryption':
43
                    if ('enc' === $use) {
44
                        return true;
45
                    }
46
47
                    return false;
48
                default:
49
                    throw new \InvalidArgumentException('Unsupported key usage.');
50
            }
51
        } elseif ($key->has('key_ops') && is_array($ops = $key->get('key_ops'))) {
52
            switch ($usage) {
53
                case 'verification':
54
                    if (in_array('verify', $ops)) {
55
                        return true;
56
                    }
57
58
                    return false;
59
                case 'signature':
60
                    if (in_array('sign', $ops)) {
61
                        return true;
62
                    }
63
64
                    return false;
65
                case 'encryption':
66
                    if (in_array('encrypt', $ops) || in_array('wrapKey', $ops)) {
67
                        return true;
68
                    }
69
70
                    return false;
71
                case 'decryption':
72
                    if (in_array('decrypt', $ops) || in_array('unwrapKey', $ops)) {
73
                        return true;
74
                    }
75
76
                    return false;
77
                default:
78
                    throw new \InvalidArgumentException('Unsupported key usage.');
79
            }
80
        }
81
82
        return true;
83
    }
84
85
    /**
86
     * @param \Jose\Object\JWKInterface $key
87
     * @param string                    $algorithm
88
     *
89
     * @return bool
90
     */
91
    private function checkKeyAlgorithm(JWKInterface $key, $algorithm)
92
    {
93
        if (!$key->has('alg')) {
94
            return true;
95
        }
96
97
        return $key->get('alg') === $algorithm;
98
    }
99
}
100