Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/SettingsTrait.php
- This file was moved from src/app/Traits/UserSettingsTrait.php.
<?php | <?php | ||||
namespace App\Traits; | namespace App\Traits; | ||||
use App\UserSetting; | |||||
use Illuminate\Support\Facades\Cache; | use Illuminate\Support\Facades\Cache; | ||||
trait UserSettingsTrait | trait SettingsTrait | ||||
{ | { | ||||
/** | /** | ||||
* Obtain the value for a setting. | * Obtain the value for a setting. | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | * $user = User::firstOrCreate(['email' => 'some@other.erg']); | ||||
* $locale = $user->getSetting('locale'); | * $locale = $user->getSetting('locale'); | ||||
* ``` | * ``` | ||||
* | * | ||||
* @param string $key Lookup key | * @param string $key Setting name | ||||
* | * | ||||
* @return string|null | * @return string|null Setting value | ||||
*/ | */ | ||||
public function getSetting(string $key) | public function getSetting(string $key) | ||||
{ | { | ||||
$settings = $this->getCache(); | $settings = $this->getCache(); | ||||
if (!array_key_exists($key, $settings)) { | if (!array_key_exists($key, $settings)) { | ||||
return null; | return null; | ||||
} | } | ||||
$value = $settings[$key]; | $value = $settings[$key]; | ||||
return empty($value) ? null : $value; | return empty($value) ? null : $value; | ||||
} | } | ||||
/** | /** | ||||
* Remove a setting. | |||||
* | |||||
* Example Usage: | |||||
* | |||||
* ```php | |||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | |||||
* $user->removeSetting('locale'); | |||||
* ``` | |||||
* | |||||
* @param string $key Setting name | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function removeSetting(string $key): void | |||||
{ | |||||
$this->setSetting($key, null); | |||||
} | |||||
/** | |||||
* Create or update a setting. | * Create or update a setting. | ||||
* | * | ||||
* 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|null $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): void | ||||
{ | { | ||||
$this->storeSetting($key, $value); | $this->storeSetting($key, $value); | ||||
$this->setCache(); | $this->setCache(); | ||||
} | } | ||||
/** | /** | ||||
* Create or update multiple settings in one fell swoop. | * Create or update multiple settings in one fell swoop. | ||||
* | * | ||||
* Example Usage: | * Example Usage: | ||||
* | * | ||||
* ```php | * ```php | ||||
* $user = User::firstOrCreate(['email' => 'some@other.erg']); | * $user = User::firstOrCreate(['email' => 'some@other.erg']); | ||||
* $user->setSettings(['locale', 'en', 'country' => 'GB']); | * $user->setSettings(['locale', 'en', 'country' => 'GB']); | ||||
* ``` | * ``` | ||||
* | * | ||||
* @param array $data An associative array of key value pairs. | * @param array $data An associative array of key value pairs. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function setSettings(array $data = []) | public function setSettings(array $data = []): void | ||||
{ | { | ||||
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): void | private function storeSetting(string $key, $value): void | ||||
{ | { | ||||
if ($value === null || $value === '') { | if ($value === null || $value === '') { | ||||
if ($setting = UserSetting::where(['user_id' => $this->id, 'key' => $key])->first()) { | // Note: We're selecting the record first, so observers can act | ||||
if ($setting = $this->settings()->where('key', $key)->first()) { | |||||
$setting->delete(); | $setting->delete(); | ||||
} | } | ||||
} else { | } else { | ||||
UserSetting::updateOrCreate( | $this->settings()->updateOrCreate( | ||||
['user_id' => $this->id, 'key' => $key], | ['key' => $key], | ||||
['value' => $value] | ['value' => $value] | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
private function getCache() | private function getCache() | ||||
{ | { | ||||
if (Cache::has('user_settings_' . $this->id)) { | $model = \strtolower(get_class($this)); | ||||
return Cache::get('user_settings_' . $this->id); | |||||
if (Cache::has("{$model}_settings_{$this->id}")) { | |||||
return Cache::get("{$model}_settings_{$this->id}"); | |||||
} | } | ||||
return $this->setCache(); | return $this->setCache(); | ||||
} | } | ||||
private function setCache() | private function setCache() | ||||
{ | { | ||||
if (Cache::has('user_settings_' . $this->id)) { | $model = \strtolower(get_class($this)); | ||||
Cache::forget('user_settings_' . $this->id); | |||||
if (Cache::has("{$model}_settings_{$this->id}")) { | |||||
Cache::forget("{$model}_settings_{$this->id}"); | |||||
} | } | ||||
$cached = []; | $cached = []; | ||||
foreach ($this->settings()->get() as $entry) { | foreach ($this->settings()->get() as $entry) { | ||||
if ($entry->value !== null && $entry->value !== '') { | 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("{$model}_settings_{$this->id}", $cached); | ||||
return $this->getCache(); | return $this->getCache(); | ||||
} | } | ||||
} | } |