Completed
Push — master ( 75d725...071852 )
by Robin
03:11
created

ModelEncryption::encryptAttribute()   A

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)) return false;
15
16
        return in_array($key, $this->encryptable);
17
    }
18
19
    /**
20
     * Decrypt a value.
21
     *
22
     * @param $value
23
     *
24
     * @return string
25
     */
26
    protected function decryptAttribute($value)
27
    {
28
        if ($value) {
29
            $value = decrypt($value);
30
        }
31
32
        return $value;
33
    }
34
35
    /**
36
     * Encrypt a value.
37
     *
38
     * @param $value
39
     *
40
     * @return string
41
     */
42
    protected function encryptAttribute($value)
43
    {
44
        if ($value) {
45
            $value = encrypt($value);
46
        }
47
48
        return $value;
49
    }
50
51
    /**
52
     * Extend the Eloquent method so properties present in
53
     * $encrypt are decrypted when directly accessed.
54
     *
55
     * @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...
56
     *
57
     * @return string
58
     */
59
    public function getAttribute($key)
60
    {
61
        $value = parent::getAttribute($key);
62
63
        if ($this->isEncryptable($key)) {
64
            $value = $this->decryptAttribute($value);
65
        }
66
67
        return $value;
68
    }
69
70
    /**
71
     * Extend the Eloquent method so properties present in
72
     * $encrypt are encrypted whenever they are set.
73
     *
74
     * @param $key      The attribute key
75
     * @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...
76
     *
77
     * @return mixed
78
     */
79
    public function setAttribute($key, $value)
80
    {
81
        if ($this->isEncryptable($key)) {
82
            $value = $this->encryptAttribute($value);
83
        }
84
85
        return parent::setAttribute($key, $value);
86
    }
87
88
    /**
89
     * Extend the Eloquent method so properties in
90
     * $encrypt are decrypted when toArray()
91
     * or toJson() is called.
92
     *
93
     * @return mixed
94
     */
95
    public function getArrayableAttributes()
96
    {
97
        $attributes = parent::getArrayableAttributes();
98
99
        foreach ($attributes as $key => $attribute) {
100
            if ($this->isEncryptable($key)) {
101
                $attributes[$key] = $this->decryptAttribute($attribute);
102
            }
103
        }
104
105
        return $attributes;
106
    }
107
}
108