firefly-iii/app/Support/Preferences.php

153 lines
3.4 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-06 13:43:19 -06:00
namespace FireflyIII\Support;
use Auth;
2015-06-03 15:11:50 -05:00
use Cache;
2015-02-11 00:35:10 -06:00
use FireflyIII\Models\Preference;
use FireflyIII\User;
2015-06-03 14:25:11 -05:00
2015-02-06 13:43:19 -06:00
/**
* Class Preferences
*
* @package FireflyIII\Support
*/
class Preferences
{
/**
* @param $name
*
* @return bool
* @throws \Exception
*/
public function delete($name): bool
{
$fullName = 'preference' . Auth::user()->id . $name;
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
/** @var Preference $preference */
$preference = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first();
$preference->delete();
return true;
}
2016-04-03 00:07:17 -05:00
/**
* @param $name
* @param null $default
*
2016-04-05 15:00:03 -05:00
* @return \FireflyIII\Models\Preference|null
2016-04-03 00:07:17 -05:00
*/
public function get($name, $default = null)
{
$user = Auth::user();
if (is_null($user)) {
return $default;
}
return $this->getForUser(Auth::user(), $name, $default);
}
2015-02-06 13:43:19 -06:00
/**
* @param string $name
2015-06-06 16:09:12 -05:00
* @param string $default
2015-02-06 13:43:19 -06:00
*
2015-05-05 03:23:01 -05:00
* @return null|\FireflyIII\Models\Preference
2015-02-06 13:43:19 -06:00
*/
2016-04-03 00:07:17 -05:00
public function getForUser(User $user, $name, $default = null)
2015-02-06 13:43:19 -06:00
{
2016-04-03 00:07:17 -05:00
$fullName = 'preference' . $user->id . $name;
2015-06-03 15:11:50 -05:00
if (Cache::has($fullName)) {
return Cache::get($fullName);
}
2016-04-03 00:07:17 -05:00
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
2015-05-25 15:56:00 -05:00
2015-06-03 14:58:06 -05:00
if ($preference) {
2015-06-03 15:11:50 -05:00
Cache::forever($fullName, $preference);
2015-06-03 14:58:06 -05:00
return $preference;
2015-05-25 15:56:00 -05:00
}
// no preference found and default is null:
if (is_null($default)) {
2015-02-06 13:43:19 -06:00
// return NULL
return null;
}
2016-04-03 00:07:17 -05:00
return $this->setForUser($user, $name, $default);
2015-02-06 13:43:19 -06:00
}
2016-01-20 08:23:36 -06:00
/**
* @return string
*/
2016-04-05 15:00:03 -05:00
public function lastActivity(): string
2016-01-20 08:23:36 -06:00
{
$preference = $this->get('lastActivity', microtime())->data;
return md5($preference);
}
/**
* @return bool
*/
2016-04-05 15:00:03 -05:00
public function mark(): bool
2016-01-20 08:23:36 -06:00
{
$this->set('lastActivity', microtime());
return true;
}
2015-02-06 13:43:19 -06:00
/**
2015-06-06 16:09:12 -05:00
* @param $name
* @param string $value
2015-02-06 13:43:19 -06:00
*
2015-03-29 11:28:49 -05:00
* @return Preference
2015-02-06 13:43:19 -06:00
*/
2016-04-05 15:00:03 -05:00
public function set($name, $value): Preference
2015-02-06 13:43:19 -06:00
{
$user = Auth::user();
if (is_null($user)) {
2016-04-06 02:27:45 -05:00
// make new preference, return it:
$pref = new Preference;
$pref->name = $name;
$pref->data = $value;
return $pref;
}
return $this->setForUser(Auth::user(), $name, $value);
}
/**
* @param \FireflyIII\User $user
* @param $name
* @param string $value
*
* @return Preference
*/
2016-04-05 15:00:03 -05:00
public function setForUser(User $user, $name, $value): Preference
{
$fullName = 'preference' . $user->id . $name;
2015-06-03 15:11:50 -05:00
Cache::forget($fullName);
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
if (!is_null($pref)) {
2015-06-03 14:58:06 -05:00
$pref->data = $value;
} else {
$pref = new Preference;
$pref->name = $name;
$pref->data = $value;
$pref->user()->associate($user);
2015-06-03 14:58:06 -05:00
2015-03-13 02:44:07 -05:00
}
2015-06-03 14:58:06 -05:00
$pref->save();
2015-02-06 13:43:19 -06:00
2015-06-03 15:11:50 -05:00
Cache::forever($fullName, $pref);
2015-02-06 13:43:19 -06:00
return $pref;
}
2015-03-29 01:14:32 -05:00
}