Issues (84)

src/HasEncryption.php (2 issues)

1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use SilverStripe\Model\ModelData;
6
7
/**
8
 * This trait allow encryption for fields that don't
9
 * require a blind index
10
 */
11
trait HasEncryption
12
{
13
    use HasBaseEncryption;
14
15
    /**
16
     * prepValueForDB gets passed $this->value
17
     *
18
     * @param string $value
19
     * @return string
20
     */
21
    public function prepValueForDB(mixed $value): array|string|null
22
    {
23
        if (!$value) {
24
            if ($this->getNullifyEmpty() || $value === null) {
0 ignored issues
show
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

24
            if ($this->/** @scrutinizer ignore-call */ getNullifyEmpty() || $value === null) {
Loading history...
25
                return null;
26
            }
27
            return '';
28
        }
29
        // Don't encrypt twice
30
        if (EncryptHelper::isEncrypted($value)) {
31
            return $value;
32
        }
33
        $aad = $this->encryptionAad;
34
        $encryptedValue = $this->getEncryptedField()->encryptValue($value, $aad);
35
        return $encryptedValue;
36
    }
37
38
    public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
39
    {
40
        $this->setEncryptionAad($record);
41
42
        // Return early if we keep encrypted value in memory
43
        if (!EncryptHelper::getAutomaticDecryption()) {
44
            $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...
45
            return $this;
46
        }
47
48
        // $markChanged is not used
49
        // The value might come encrypted from the database
50
        if ($value && EncryptHelper::isEncrypted($value)) {
51
            $this->value = $this->decryptValue($value);
52
        } else {
53
            $this->value = $value;
54
        }
55
        return $this;
56
    }
57
}
58