ModelEncryption::encryptAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Famdirksen\LaravelModelEncryption;
4
5
trait ModelEncryption
6
{
7
    /**
8
     * @param $key
9
     *
10
     * @return bool
11
     */
12
    public function isEncryptable($key)
13
    {
14
        if (! isset($this->encryptable)) {
15
            return false;
16
        }
17
18
        return in_array($key, $this->encryptable);
19
    }
20
21
    /**
22
     * Decrypt a value.
23
     *
24
     * @param $value
25
     *
26
     * @return string
27
     */
28
    protected function decryptAttribute($value)
29
    {
30
        if ($value) {
31
            $value = decrypt($value);
32
        }
33
34
        return $value;
35
    }
36
37
    /**
38
     * Encrypt a value.
39
     *
40
     * @param $value
41
     *
42
     * @return string
43
     */
44
    protected function encryptAttribute($value)
45
    {
46
        if ($value) {
47
            $value = encrypt($value);
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * Extend the Eloquent method so properties present in
55
     * $encrypt are decrypted when directly accessed.
56
     *
57
     * @param $key  The attribute key
0 ignored issues
show
Bug introduced by
The type Famdirksen\LaravelModelEncryption\The 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...
58
     *
59
     * @return string
60
     */
61
    public function getAttribute($key)
62
    {
63
        $value = parent::getAttribute($key);
64
65
        if ($this->isEncryptable($key)) {
66
            $value = $this->decryptAttribute($value);
67
        }
68
69
        return $value;
70
    }
71
72
    /**
73
     * Extend the Eloquent method so properties present in
74
     * $encrypt are encrypted whenever they are set.
75
     *
76
     * @param $key      The attribute key
77
     * @param $value    Attribute value to set
0 ignored issues
show
Bug introduced by
The type Famdirksen\LaravelModelEncryption\Attribute 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...
78
     *
79
     * @return mixed
80
     */
81
    public function setAttribute($key, $value)
82
    {
83
        if ($this->isEncryptable($key)) {
84
            $value = $this->encryptAttribute($value);
85
        }
86
87
        return parent::setAttribute($key, $value);
88
    }
89
90
    /**
91
     * Extend the Eloquent method so properties in
92
     * $encrypt are decrypted when toArray()
93
     * or toJson() is called.
94
     *
95
     * @return mixed
96
     */
97
    public function getArrayableAttributes()
98
    {
99
        $attributes = parent::getArrayableAttributes();
100
101
        foreach ($attributes as $key => $attribute) {
102
            if ($this->isEncryptable($key)) {
103
                $attributes[$key] = $this->decryptAttribute($attribute);
104
            }
105
        }
106
107
        return $attributes;
108
    }
109
}
110