Passed
Push — master ( 45a3bb...2cf7c0 )
by Thomas
03:37
created

HasEncryption::setValue()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 3
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
     * @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...
17
     * @return EncryptedField
18
     */
19
    public function getEncryptedField($engine = null)
20
    {
21
        if ($engine === null) {
22
            $engine = EncryptHelper::getCipherSweet();
23
        }
24
        $encryptedField = new EncryptedField($engine, $this->tableName, $this->name);
25
        return $encryptedField;
26
    }
27
28
    public function prepValueForDB($value)
29
    {
30
        if (!$value) {
31
            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

31
            if ($this->/** @scrutinizer ignore-call */ getNullifyEmpty() || $value === null) {
Loading history...
32
                return null;
33
            }
34
            return '';
35
        }
36
37
        $encryptedValue = $this->getEncryptedField()->encryptValue($value);
38
        return $encryptedValue;
39
    }
40
41
    public function setValue($value, $record = null, $markChanged = true)
0 ignored issues
show
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

41
    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...
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

41
    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...
42
    {
43
        if ($value && EncryptHelper::isEncrypted($value)) {
44
            try {
45
                $this->value = $this->getEncryptedField()->decryptValue($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...
46
            } catch (InvalidCiphertextException $ex) {
47
                // rotate backend ?
48
                // $this->value = $newEncryptedField->decryptValue($encryptedValue);
49
            } catch (Exception $ex) {
50
                // We cannot decrypt
51
                $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

51
                /** @scrutinizer ignore-call */ 
52
                $this->value = $this->nullValue();
Loading history...
52
            }
53
        } else {
54
            $this->value = $value;
55
        }
56
        return $this;
57
    }
58
}
59