Completed
Pull Request — master (#987)
by
unknown
04:24
created

CryptTrait::decrypt()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
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
use Exception;
17
use LogicException;
18
19
trait CryptTrait
20
{
21
    /**
22
     * @var string|Key
23
     */
24
    protected $encryptionKey;
25
26
    /**
27
     * Encrypt data with encryptionKey.
28
     *
29
     * @param string $unencryptedData
30
     *
31
     * @throws LogicException
32
     *
33
     * @return string
34
     */
35 1
    protected function encrypt($unencryptedData)
36
    {
37
        try {
38 1
            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...
39
                return Crypto::encrypt($unencryptedData, $this->encryptionKey);
40
            }
41
42 1
            return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
43
        } catch (Exception $e) {
44
            throw new LogicException($e->getMessage(), null, $e);
45
        }
46
    }
47
48
    /**
49
     * Decrypt data with encryptionKey.
50
     *
51
     * @param string $encryptedData
52
     *
53
     * @throws LogicException
54
     *
55
     * @return string
56
     */
57
    protected function decrypt($encryptedData)
58
    {
59
        try {
60
            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...
61
                return Crypto::decrypt($encryptedData, $this->encryptionKey);
62
            }
63
64
            return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
65
        } catch (Exception $e) {
66
            throw new LogicException($e->getMessage(), null, $e);
67
        }
68
    }
69
70
    /**
71
     * Set the encryption key
72
     *
73
     * @param string|Key $key
74
     */
75 8
    public function setEncryptionKey($key = null)
76
    {
77 8
        $this->encryptionKey = $key;
78 8
    }
79
}
80