Passed
Push — master ( 06c7d6...5383e7 )
by Thomas
02:04
created

HasBaseEncryption   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 25
c 1
b 0
f 0
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncryptedField() 0 7 2
A decryptValue() 0 20 4
A getDecryptedValue() 0 6 2
A getEncryptionException() 0 3 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
     * @return Exception
18
     */
19
    public function getEncryptionException()
20
    {
21
        return $this->encryptionException;
22
    }
23
24
    /**
25
     * @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...
26
     * @return EncryptedField
27
     */
28
    public function getEncryptedField($engine = null)
29
    {
30
        if ($engine === null) {
31
            $engine = EncryptHelper::getCipherSweet();
32
        }
33
        $encryptedField = new EncryptedField($engine, $this->tableName, $this->name);
34
        return $encryptedField;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getDecryptedValue()
41
    {
42
        if (EncryptHelper::isEncrypted($this->value)) {
43
            return $this->decryptValue($this->value);
44
        }
45
        return $this->value;
46
    }
47
48
    /**
49
     * @param string $value
50
     * @return string
51
     */
52
    protected function decryptValue($value)
53
    {
54
        $decrypted = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $decrypted is dead and can be removed.
Loading history...
55
        try {
56
            $decrypted = $this->getEncryptedField()->decryptValue($value);
57
        } catch (InvalidCiphertextException $ex) {
58
            $this->encryptionException = $ex;
59
            // rotate backend ?
60
            if (EncryptHelper::getAutomaticRotation()) {
61
                $encryption = EncryptHelper::getEncryption($value);
62
                $engine = EncryptHelper::getEngineForEncryption($encryption);
63
                $oldEncryptedField = $this->getEncryptedField($engine);
64
                $decrypted = $oldEncryptedField->decryptValue($value);
65
            } else {
66
                $decrypted = $value;
67
            }
68
        } catch (Exception $ex) {
69
            $this->encryptionException = $ex;
70
        }
71
        return $decrypted;
72
    }
73
}
74