Passed
Push — master ( 461cd7...06c7d6 )
by Thomas
02:35
created

HasEncryption::getDecryptedValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use Exception;
6
use ParagonIE\CipherSweet\EncryptedField;
7
use ParagonIE\CipherSweet\Exception\InvalidCiphertextException;
8
9
/**
10
 * This trait allow encryption for fields that don't
11
 * require a blind index
12
 */
13
trait HasEncryption
14
{
15
    /**
16
     * @var Exception
17
     */
18
    protected $encryptionException;
19
20
    /**
21
     * @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...
22
     * @return EncryptedField
23
     */
24
    public function getEncryptedField($engine = null)
25
    {
26
        if ($engine === null) {
27
            $engine = EncryptHelper::getCipherSweet();
28
        }
29
        $encryptedField = new EncryptedField($engine, $this->tableName, $this->name);
30
        return $encryptedField;
31
    }
32
33
    /**
34
     * @return Exception
35
     */
36
    public function getEncryptionException()
37
    {
38
        return $this->encryptionException;
39
    }
40
41
    /**
42
     * prepValueForDB gets passed $this->value
43
     *
44
     * @param string $value
45
     * @return string
46
     */
47
    public function prepValueForDB($value)
48
    {
49
        if (!$value) {
50
            if ($this->getNullifyEmpty() || $value === null) {
0 ignored issues
show
Bug introduced by
It seems like getNullifyEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            if ($this->/** @scrutinizer ignore-call */ getNullifyEmpty() || $value === null) {
Loading history...
51
                return null;
52
            }
53
            return '';
54
        }
55
        // Don't encrypt twice
56
        if (EncryptHelper::isEncrypted($value)) {
57
            return $value;
58
        }
59
        $encryptedValue = $this->getEncryptedField()->encryptValue($value);
60
        return $encryptedValue;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getDecryptedValue()
67
    {
68
        if (EncryptHelper::isEncrypted($this->value)) {
69
            return $this->getEncryptedField()->decryptValue($this->value);
70
        }
71
        return $this->value;
72
    }
73
74
    public function setValue($value, $record = null, $markChanged = true)
0 ignored issues
show
Unused Code introduced by
The parameter $markChanged is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function setValue($value, $record = null, /** @scrutinizer ignore-unused */ $markChanged = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $record is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function setValue($value, /** @scrutinizer ignore-unused */ $record = null, $markChanged = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        // Return early if we keep encrypted value in memory
77
        if (!EncryptHelper::getAutomaticDecryption()) {
78
            $this->value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
            return $this;
80
        }
81
82
        // $markChanged is not used
83
        // The value might come encrypted from the database
84
        if ($value && EncryptHelper::isEncrypted($value)) {
85
            try {
86
                $this->value = $this->getEncryptedField()->decryptValue($value);
87
            } catch (InvalidCiphertextException $ex) {
88
                $this->encryptionException = $ex;
89
                // rotate backend ?
90
                if (EncryptHelper::getAutomaticRotation()) {
91
                    $encryption = EncryptHelper::getEncryption($value);
92
                    $engine = EncryptHelper::getEngineForEncryption($encryption);
93
                    $oldEncryptedField = $this->getEncryptedField($engine);
94
                    $this->value = $oldEncryptedField->decryptValue($value);
95
                } else {
96
                    $this->value = $value;
97
                }
98
            } catch (Exception $ex) {
99
                $this->encryptionException = $ex;
100
                // We cannot decrypt
101
                $this->value = $this->nullValue();
0 ignored issues
show
Bug introduced by
It seems like nullValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
                /** @scrutinizer ignore-call */ 
102
                $this->value = $this->nullValue();
Loading history...
102
            }
103
        } else {
104
            $this->value = $value;
105
        }
106
        return $this;
107
    }
108
}
109