Completed
Pull Request — master (#941)
by Eric
02:24
created

CryptTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 69
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 12 3
A decrypt() 0 20 4
A setEncryptionKey() 0 4 1
1
<?php
2
/**
3
 * Encrypt/decrypt with encryptionKey.
4
 *
5
 * @author      Alex Bilbie <[email protected]>
6
 * @copyright   Copyright (c) Alex Bilbie
7
 * @license     http://mit-license.org/
8
 *
9
 * @link        https://github.com/thephpleague/oauth2-server
10
 */
11
12
namespace League\OAuth2\Server;
13
14
use Defuse\Crypto\Crypto;
15
use Defuse\Crypto\Key;
16
17
trait CryptTrait
18
{
19
    /**
20
     * @var string|Key
21
     */
22
    protected $encryptionKey;
23
24
    /**
25
     * Encrypt data with encryptionKey.
26
     *
27
     * @param string $unencryptedData
28
     *
29
     * @throws \LogicException
30
     *
31
     * @return string
32
     */
33 34
    protected function encrypt($unencryptedData)
34
    {
35
        try {
36 34
            if ($this->encryptionKey instanceof Key) {
0 ignored issues
show
Bug introduced by
The class Defuse\Crypto\Key does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37 1
                return Crypto::encrypt($unencryptedData, $this->encryptionKey);
38
            }
39
40 33
            return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
41
        } catch (\Exception $e) {
42
            throw new \LogicException($e->getMessage());
43
        }
44
    }
45
46
    /**
47
     * Decrypt data with encryptionKey.
48
     *
49
     * @param string $encryptedData
50
     *
51
     * @throws \LogicException
52
     *
53
     * @return string
54
     */
55 28
    protected function decrypt($encryptedData)
56
    {
57
        try {
58 28
            if ($this->encryptionKey instanceof Key) {
0 ignored issues
show
Bug introduced by
The class Defuse\Crypto\Key does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
59
                try {
60 3
                    return Crypto::decrypt($encryptedData, $this->encryptionKey);
61 2
                } catch (\Exception $e) {
62
                    // This may have been encrypted with encryptWithPassword(), pull
63
                    // the data out of the key and use that to decrypt.
64
                    // $this->encryptionKey should not be assigned to the raw bytes so
65
                    // subsequent calls to decrypt can work with the key object.
66 2
                    return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey->getRawBytes());
67
                }
68
            }
69
70 25
            return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
71 3
        } catch (\Exception $e) {
72 3
            throw new \LogicException($e->getMessage());
73
        }
74
    }
75
76
    /**
77
     * Set the encryption key
78
     *
79
     * @param string|Key $key
80
     */
81 84
    public function setEncryptionKey($key = null)
82
    {
83 84
        $this->encryptionKey = $key;
84 84
    }
85
}
86