firefly-iii/app/Models/Preference.php
2016-11-18 20:06:08 +01:00

71 lines
1.6 KiB
PHP

<?php
/**
* Preference.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Models;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\Model;
use Log;
/**
* Class Preference
*
* @package FireflyIII\Models
*/
class Preference extends Model
{
protected $dates = ['created_at', 'updated_at'];
protected $fillable = ['user_id', 'data', 'name', 'data'];
/**
* @param $value
*
* @return mixed
* @throws FireflyException
*/
public function getDataAttribute($value)
{
try {
$data = Crypt::decrypt($value);
} catch (DecryptException $e) {
Log::error('Could not decrypt preference.', ['id' => $this->id, 'name' => $this->name, 'data' => $value]);
throw new FireflyException(
sprintf('Could not decrypt preference #%d. If this error persists, please run "php artisan cache:clear" on the command line.', $this->id)
);
}
return json_decode($data);
}
/**
* @param $value
*/
public function setDataAttribute($value)
{
$this->attributes['data'] = Crypt::encrypt(json_encode($value));
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('FireflyIII\User');
}
}