EncryptsAttributes::setAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Soved\Laravel\Gdpr;
4
5
trait EncryptsAttributes
6
{
7
    /**
8
     * Get a plain attribute (not a relationship).
9
     *
10
     * @param string $key
11
     *
12
     * @return mixed
13
     */
14
    public function getAttributeValue($key)
15
    {
16
        $value = parent::getAttributeValue($key);
17
18
        if (in_array($key, $this->encrypted) &&
19
            ! is_null($value)) {
20
            return decrypt($value);
21
        }
22
23
        return $value;
24
    }
25
26
    /**
27
     * Set a given attribute on the model.
28
     *
29
     * @param string $key
30
     * @param mixed  $value
31
     *
32
     * @return $this
33
     */
34
    public function setAttribute(
35
        $key,
36
        $value
37
    ) {
38
        if (in_array($key, $this->encrypted) &&
39
            ! is_null($value)) {
40
            $value = encrypt($value);
41
        }
42
43
        parent::setAttribute($key, $value);
44
    }
45
}
46