Completed
Pull Request — master (#925)
by
unknown
01:37
created

CryptTrait::encrypt()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 3
cts 6
cp 0.5
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 4.125
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 25
    protected function encrypt($unencryptedData)
34
    {
35
        try {
36 25
            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
                return Crypto::encrypt($unencryptedData, $this->encryptionKey);
38
            }
39
40 25
            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 24
    protected function decrypt($encryptedData)
56
    {
57
        try {
58 24
            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
                return Crypto::decrypt($encryptedData, $this->encryptionKey);
60
            }
61
62 24
            return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
63 2
        } catch (\Exception $e) {
64 2
            throw new \LogicException($e->getMessage());
65
        }
66
    }
67
68
    /**
69
     * Set the encryption key
70
     *
71
     * @param string|Key $key
72
     */
73 71
    public function setEncryptionKey($key = null)
74
    {
75 71
        $this->encryptionKey = $key;
76 71
    }
77
}
78