Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

Preference::getDataAttribute()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 9
nop 1
1
<?php
2
/**
3
 * Preference.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Models;
24
25
use Crypt;
26
use Exception;
27
use FireflyIII\Exceptions\FireflyException;
28
use Illuminate\Contracts\Encryption\DecryptException;
29
use Illuminate\Database\Eloquent\Model;
30
use Log;
31
32
/**
33
 * Class Preference.
34
 *
35
 * @property mixed $data
36
 */
37
class Preference extends Model
38
{
39
    /**
40
     * The attributes that should be casted to native types.
41
     *
42
     * @var array
43
     */
44
    protected $casts
45
        = [
46
            'created_at' => 'datetime',
47
            'updated_at' => 'datetime',
48
        ];
49
50
    /** @var array */
51
    protected $fillable = ['user_id', 'data', 'name', 'data'];
52
53
    /**
54
     * @param $value
55
     *
56
     * @return mixed
57
     *
58
     * @throws FireflyException
59
     */
60
    public function getDataAttribute($value)
61
    {
62
        $result = null;
63
        try {
64
            $data = Crypt::decrypt($value);
65
        } catch (DecryptException $e) {
66
            Log::error('Could not decrypt preference.', ['id' => $this->id, 'name' => $this->name, 'data' => $value]);
0 ignored issues
show
Bug introduced by James Cole
The property name does not seem to exist on FireflyIII\Models\Preference. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
67
            throw new FireflyException(
68
                sprintf('Could not decrypt preference #%d. If this error persists, please run "php artisan cache:clear" on the command line.', $this->id)
69
            );
70
        }
71
        $serialized = true;
72
        try {
73
            unserialize($data, ['allowed_classes' => false]);
74
        } catch (Exception $e) {
75
            $serialized = false;
76
            Log::debug(sprintf('Could not unserialise preference #%d. This is good. %s', $this->id, $e->getMessage()));
77
        }
78
        if (!$serialized) {
79
            $result = json_decode($data, true);
80
        }
81
        if ($serialized) {
82
            Log::error(sprintf('Preference #%d ("%s") was stored as serialised object. It will be deleted and recreated.', $this->id, $this->name));
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * @codeCoverageIgnore
90
     *
91
     * @param $value
92
     *
93
     * @throws \Illuminate\Contracts\Encryption\EncryptException
94
     */
95
    public function setDataAttribute($value)
96
    {
97
        $this->attributes['data'] = Crypt::encrypt(json_encode($value));
98
    }
99
100
    /**
101
     * @codeCoverageIgnore
102
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
103
     */
104
    public function user()
105
    {
106
        return $this->belongsTo('FireflyIII\User');
107
    }
108
}
109