Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/UserSettingsTrait.php
Show All 33 Lines | trait UserSettingsTrait | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | * $user = User::firstOrCreate(['email' => 'some@other.erg']); | ||||
* $user->setSetting('locale', 'en'); | * $user->setSetting('locale', 'en'); | ||||
* ``` | * ``` | ||||
* | * | ||||
* @param string $key Setting name | * @param string $key Setting name | ||||
* @param string $value The new value for the setting. | * @param string|null $value The new value for the setting. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function setSetting(string $key, $value) | public function setSetting(string $key, $value) | ||||
{ | { | ||||
$this->storeSetting($key, $value); | $this->storeSetting($key, $value); | ||||
$this->setCache(); | $this->setCache(); | ||||
} | } | ||||
Show All 16 Lines | trait UserSettingsTrait | ||||
{ | { | ||||
foreach ($data as $key => $value) { | foreach ($data as $key => $value) { | ||||
$this->storeSetting($key, $value); | $this->storeSetting($key, $value); | ||||
} | } | ||||
$this->setCache(); | $this->setCache(); | ||||
} | } | ||||
private function storeSetting(string $key, $value) | private function storeSetting(string $key, $value): void | ||||
{ | { | ||||
$record = UserSetting::where(['user_id' => $this->id, 'key' => $key])->first(); | if ($value === null || $value === '') { | ||||
UserSetting::where(['user_id' => $this->id, 'key' => $key])->delete(); | |||||
if ($record) { | |||||
$record->value = $value; | |||||
$record->save(); | |||||
} else { | } else { | ||||
$data = new UserSetting(['key' => $key, 'value' => $value]); | UserSetting::updateOrCreate( | ||||
$this->settings()->save($data); | ['user_id' => $this->id, 'key' => $key], | ||||
['value' => $value] | |||||
); | |||||
} | } | ||||
} | } | ||||
private function getCache() | private function getCache() | ||||
{ | { | ||||
if (Cache::has('user_settings_' . $this->id)) { | if (Cache::has('user_settings_' . $this->id)) { | ||||
return Cache::get('user_settings_' . $this->id); | return Cache::get('user_settings_' . $this->id); | ||||
} | } | ||||
return $this->setCache(); | return $this->setCache(); | ||||
} | } | ||||
private function setCache() | private function setCache() | ||||
{ | { | ||||
if (Cache::has('user_settings_' . $this->id)) { | if (Cache::has('user_settings_' . $this->id)) { | ||||
Cache::forget('user_settings_' . $this->id); | Cache::forget('user_settings_' . $this->id); | ||||
} | } | ||||
$cached = []; | $cached = []; | ||||
foreach ($this->settings()->get() as $entry) { | foreach ($this->settings()->get() as $entry) { | ||||
if ($entry->value !== null && $entry->value !== '') { | |||||
$cached[$entry->key] = $entry->value; | $cached[$entry->key] = $entry->value; | ||||
} | } | ||||
} | |||||
Cache::forever('user_settings_' . $this->id, $cached); | Cache::forever('user_settings_' . $this->id, $cached); | ||||
return $this->getCache(); | return $this->getCache(); | ||||
} | } | ||||
} | } |