Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Removing setters from Store #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions src/CacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,6 @@ public function __construct(
$this->cacheName = $cacheName;
}

/**
* Flush all setting changes to the backing store.
*
* @param int $userId The ID of the user to save the user settings for.
*
* @return bool Whether the settings were flushed correctly.
*/
protected function flush($userId = -1)
{
parent::flush($userId);

$this->cache->forget($this->cacheName);
}

/**
* Load all settings into the setting store.
*
Expand Down
130 changes: 6 additions & 124 deletions src/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,21 @@ public function __construct(
$this->settingValueModel = $settingValueModel;
}

/**
* Flush all setting changes to the backing store.
*
* @param int $userId The ID of the user to save the user settings for.
*
* @return bool Whether the settings were flushed correctly.
*/
protected function flush($userId = -1)
{
$this->handleCreatedSettings();
$this->handleUpdatedSettings();
$this->handleDeletedSettings();
}

/**
* Load all settings into the setting store.
*
* @return array An array of all of the loaded settings.
*/
protected function loadSettings()
{
$settings = $this->settingsModel->leftJoin('setting_values', 'setting_values.setting_id', '=', 'settings.id')
$settings = $this->settingsModel->newQuery()
->leftJoin('setting_values', 'setting_values.setting_id', '=', 'settings.id')
->select([
'settings.id',
'settings.name',
'settings.package',
'setting_values.value',
'setting_values.user_id'
'setting_values.user_id',
]);

if (($user = $this->guard->user()) !== null && $user->getAuthIdentifier() > 0) {
Expand All @@ -89,118 +76,13 @@ protected function loadSettings()
$settingType = ($setting->user_id === null) ? Store::DEFAULT_SETTING_KEY : Store::USER_SETTING_KEY;

$this->settings[$setting->package][$setting->name][$settingType] = [
'id' => $setting->id,
'value' => $setting->value,
'id' => $setting->id,
'value' => $setting->value,
'package' => $setting->package,
'name' => $setting->name,
'name' => $setting->name,
];
}

return $this->settings;
}

/**
* Update existing settings with new setting values.
*/
private function handleUpdatedSettings()
{
foreach ($this->modifiedSettings as $id => $setting) {
if (is_numeric($id)) {
$this->settingValueModel->where('setting_id', '=', $id)
->where('user_id', '=', $setting['user_id'])
->update(['value' => $setting['value']]);
} else {
if ($setting['id'] != -1) {
$this->settingValueModel->create([
'value' => $setting['value'],
'user_id' => $setting['user_id'],
'setting_id' => $setting['id']
]);
} else {
$foundSetting = $this->settingsModel->where('name', '=', $setting['name'])
->where('package', '=', $setting['package'])->first();

if ($foundSetting != null) {
$foundSetting->values()->create([
'value' => $setting['value'],
'user_id' => $setting['user_id']
]);
}
}
}

unset($this->modifiedSettings[$id]);
}
}

/**
* Create any new settings that have been created.
*/
private function handleCreatedSettings()
{
foreach ($this->createdSettings[static::DEFAULT_SETTING_KEY] as $key => $createdDefaultSetting) {
$setting = $this->settingsModel->create([
'package' => $createdDefaultSetting['package'],
'name' => $createdDefaultSetting['name'],
]);

$setting->values()->create(['value' => $createdDefaultSetting['value']]);

unset($this->createdSettings[static::DEFAULT_SETTING_KEY][$key]);
}

if (($user = $this->guard->user()) !== null && $user->getAuthIdentifier() > 0) {
foreach ($this->createdSettings[static::USER_SETTING_KEY] as $key => $createdDefaultSetting) {
$setting = $this->settingsModel->create([
'package' => $createdDefaultSetting['package'],
'name' => $createdDefaultSetting['name'],
]);

$setting->values()->create([
'value' => $createdDefaultSetting['value'],
'user_id' => $user->getAuthIdentifier()
]);

unset($this->createdSettings[static::USER_SETTING_KEY][$key]);
}
}
}

/**
* Remove any settings that have been deleted.
*/
private function handleDeletedSettings()
{
foreach ($this->deletedSettings as $key => $setting) {
if ($setting['just_user']) {
if (($user = $this->guard->user()) !== null) {
$this->settingValueModel->join(
'settings',
'setting_values.setting_id',
'=',
'settings.id'
)->where('settings.name', '=', $setting['name'])->where(
'settings.package',
'=',
$setting['package']
)->where(
'setting_values.user_id',
'=',
$user->getAuthIdentifier()
)->delete();
}
} else {
$settingEntry = $this->settingsModel->where('name', '=', $setting['name'])->where(
'package',
'=',
$setting['package']
)->first();

$settingEntry->values()->delete();
$settingEntry->delete();
}

unset($this->deletedSettings[$key]);
}
}
}
2 changes: 1 addition & 1 deletion src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class Facade extends \Illuminate\Support\Facades\Facade
*/
protected static function getFacadeAccessor()
{
return 'MyBB\Settings\Manager';
return Manager::class;
}
}
19 changes: 12 additions & 7 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

namespace MyBB\Settings;

use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Cache\Repository;
use MyBB\Settings\Models\Setting;
use MyBB\Settings\Models\SettingValue;

class Manager extends \Illuminate\Support\Manager
{
/**
Expand All @@ -32,9 +37,9 @@ public function getDefaultDriver()
public function createDatabaseDriver()
{
return new DatabaseStore(
$this->app->make('Illuminate\Contracts\Auth\Guard'),
$this->app->make('MyBB\Settings\Models\Setting'),
$this->app->make('MyBB\Settings\Models\SettingValue')
$this->app->make(Guard::class),
$this->app->make(Setting::class),
$this->app->make(SettingValue::class)
);
}

Expand All @@ -45,13 +50,13 @@ public function createDatabaseDriver()
*/
public function createCacheDriver()
{
$cache = $this->app->make('Illuminate\Contracts\Cache\Repository');
$cache = $this->app->make(Repository::class);
$cacheName = $this->app['config']->get('settings.settings_cache_name');

return new CacheStore(
$this->app->make('Illuminate\Contracts\Auth\Guard'),
$this->app->make('MyBB\Settings\Models\Setting'),
$this->app->make('MyBB\Settings\Models\SettingValue'),
$this->app->make(Guard::class),
$this->app->make(Setting::class),
$this->app->make(SettingValue::class),
$cache,
$cacheName
);
Expand Down
59 changes: 0 additions & 59 deletions src/Middleware/SaveSettingsOnTerminate.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ class Setting extends Model

public function values()
{
return $this->hasMany('MyBB\Settings\Models\SettingValue');
return $this->hasMany(SettingValue::class);
}
}
2 changes: 1 addition & 1 deletion src/Models/SettingValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ class SettingValue extends Model

public function setting()
{
return $this->belongsTo('MyBB\Settings\Models\Setting');
return $this->belongsTo(Setting::class);
}
}
Loading