Passed
Push — master ( 5383e7...548176 )
by Thomas
02:56
created

HasBaseEncryption::setEncryptionAad()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use Exception;
6
use ParagonIE\CipherSweet\EncryptedField;
7
use ParagonIE\CipherSweet\Exception\InvalidCiphertextException;
8
9
trait HasBaseEncryption
10
{
11
    /**
12
     * @var Exception
13
     */
14
    protected $encryptionException;
15
16
    /**
17
     * @var string
18
     */
19
    protected $encryptionAad = '';
20
21
    /**
22
     * @return Exception
23
     */
24
    public function getEncryptionException()
25
    {
26
        return $this->encryptionException;
27
    }
28
29
    /**
30
     * @param CipherSweet $engine
0 ignored issues
show
Bug introduced by
The type LeKoala\Encrypt\CipherSweet was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     * @return EncryptedField
32
     */
33
    public function getEncryptedField($engine = null)
34
    {
35
        if ($engine === null) {
36
            $engine = EncryptHelper::getCipherSweet();
37
        }
38
        $encryptedField = new EncryptedField($engine, $this->tableName, $this->name);
39
        return $encryptedField;
40
    }
41
42
    /**
43
     * Decrypt current value using underlying EncryptedField instance
44
     *
45
     * @return string
46
     */
47
    public function getDecryptedValue()
48
    {
49
        if (EncryptHelper::isEncrypted($this->value)) {
50
            return $this->decryptValue($this->value);
51
        }
52
        return $this->value;
53
    }
54
55
    /**
56
     * @param DataObject $record
0 ignored issues
show
Bug introduced by
The type LeKoala\Encrypt\DataObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     * @return void
58
     */
59
    protected function setEncryptionAad($record)
60
    {
61
        $field = EncryptHelper::getAadSource();
62
        if (!$field) {
63
            return;
64
        }
65
        if ($record && isset($record->$field)) {
66
            $this->encryptionAad = (string)$record->$field;
67
        }
68
    }
69
70
    /**
71
     * Decrypt a value using underlying EncryptedField instance
72
     *
73
     * @param string $value
74
     * @return string
75
     */
76
    protected function decryptValue($value)
77
    {
78
        $decrypted = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $decrypted is dead and can be removed.
Loading history...
79
        $aad = $this->encryptionAad;
80
        try {
81
            $decrypted = $this->getEncryptedField()->decryptValue($value, $aad);
82
        } catch (InvalidCiphertextException $ex) {
83
            $this->encryptionException = $ex;
84
            // rotate backend ?
85
            if (EncryptHelper::getAutomaticRotation()) {
86
                $encryption = EncryptHelper::getEncryption($value);
87
                $engine = EncryptHelper::getEngineForEncryption($encryption);
88
                $oldEncryptedField = $this->getEncryptedField($engine);
89
                $decrypted = $oldEncryptedField->decryptValue($value, $aad);
90
            } else {
91
                $decrypted = $value;
92
            }
93
        } catch (Exception $ex) {
94
            $this->encryptionException = $ex;
95
        }
96
        return $decrypted;
97
    }
98
}
99