EncryptedCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A set() 0 16 3
1
<?php
2
3
4
namespace RichardStyles\EloquentEncryption\Casts;
5
6
7
use Illuminate\Database\Eloquent\JsonEncodingException;
8
use Illuminate\Support\Collection;
9
10
class EncryptedCollection extends Encrypted
11
{
12
    /**
13
     * Cast the given value and decrypt
14
     *
15
     * @param  \Illuminate\Database\Eloquent\Model  $model
16
     * @param  string  $key
17
     * @param  mixed  $value
18
     * @param  array  $attributes
19
     * @return int
20
     */
21
    public function get($model, $key, $value, $attributes)
22
    {
23
        return new Collection(json_decode(parent::get($model, $key, $value, $attributes)));
24
    }
25
26
    /**
27
     * Prepare the given value for storage.
28
     *
29
     * @param  \Illuminate\Database\Eloquent\Model  $model
30
     * @param  string  $key
31
     * @param  array  $value
32
     * @param  array  $attributes
33
     * @return string
34
     */
35
    public function set($model, $key, $value, $attributes)
36
    {
37
        if($value instanceof Collection){
38
            $value = $value->toJson();
39
        }else {
40
            $value = json_encode($value);
41
42
            if ($value === false) {
43
                throw JsonEncodingException::forAttribute(
44
                    $this, $key, json_last_error_msg()
45
                );
46
            }
47
        }
48
49
        return parent::set($model, $key, $value, $attributes);
50
    }
51
}
52