From 17292f67166cd4e5c291ded1232499edbc40d39d Mon Sep 17 00:00:00 2001 From: Lorand Gombos Date: Thu, 7 May 2020 13:14:21 +0300 Subject: [PATCH] Updates (v 3.6.5) (#56) --- CHANGELOG.md | 5 +++++ README.md | 3 +++ src/Traits/HasSettingsField.php | 21 ++++++++------------- src/Traits/HasSettingsTable.php | 4 ++-- tests/Models/UserWithField.php | 5 +++++ tests/Models/UserWithRedis.php | 5 +++++ tests/Models/UserWithTextField.php | 5 +++++ tests/Models/UsersWithTable.php | 5 +++-- tests/Models/WrongUser.php | 5 +++++ tests/Models/WrongUserWithField.php | 5 +++++ tests/TextFieldSettingsManagerTest.php | 1 + 11 files changed, 47 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f8418a..f2b9174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `glorand/laravel-model-settings` will be documented in this file +## 3.6.5 - 2020-05-07 +### Fix +- code refactor +- use Cache facade instead of cache() helper function + ## 3.6.4 - 2020-04-24 ### Fix - github actions diff --git a/README.md b/README.md index ed67b76..155aa4b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ Total Downloads + + + Build Status diff --git a/src/Traits/HasSettingsField.php b/src/Traits/HasSettingsField.php index 76e1fc1..f4376ff 100644 --- a/src/Traits/HasSettingsField.php +++ b/src/Traits/HasSettingsField.php @@ -2,11 +2,11 @@ namespace Glorand\Model\Settings\Traits; -use Exception; use Glorand\Model\Settings\Contracts\SettingsManagerContract; use Glorand\Model\Settings\Exceptions\ModelSettingsException; use Glorand\Model\Settings\Managers\FieldSettingsManager; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Schema; /** @@ -93,21 +93,16 @@ public function setPersistSettings(bool $val = true) /** * @return mixed - * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException */ private function hasSettingsField() { - try { - return cache()->remember( - config('model_settings.settings_table_cache_prefix') . '::has_field', - now()->addDays(1), - function () { - return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName()); - } - ); - } catch (Exception $e) { - throw new ModelSettingsException("Cache: " . $e->getMessage()); - } + return Cache::remember( + config('model_settings.settings_table_cache_prefix') . '::has_field', + now()->addDays(1), + function () { + return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName()); + } + ); } abstract public function getTable(); diff --git a/src/Traits/HasSettingsTable.php b/src/Traits/HasSettingsTable.php index f3226dd..8cbeab6 100644 --- a/src/Traits/HasSettingsTable.php +++ b/src/Traits/HasSettingsTable.php @@ -6,6 +6,7 @@ use Glorand\Model\Settings\Managers\TableSettingsManager; use Glorand\Model\Settings\Models\ModelSettings; use Illuminate\Database\Eloquent\Relations\MorphOne; +use Illuminate\Support\Facades\Cache; /** * Trait HasSettingsTable @@ -29,12 +30,11 @@ public function settings(): SettingsManagerContract /** * @return array - * @throws \Exception */ public function getSettingsValue(): array { if (config('model_settings.settings_table_use_cache')) { - return cache()->rememberForever($this->getSettingsCacheKey(), function () { + return Cache::rememberForever($this->getSettingsCacheKey(), function () { return $this->__getSettingsValue(); }); } diff --git a/tests/Models/UserWithField.php b/tests/Models/UserWithField.php index 9404d88..146ea13 100644 --- a/tests/Models/UserWithField.php +++ b/tests/Models/UserWithField.php @@ -5,6 +5,11 @@ use Glorand\Model\Settings\Traits\HasSettingsField; use Illuminate\Database\Eloquent\Model; +/** + * Class UserWithField + * @package Glorand\Model\Settings\Tests\Models + * @method static first() + */ class UserWithField extends Model { use HasSettingsField; diff --git a/tests/Models/UserWithRedis.php b/tests/Models/UserWithRedis.php index 8f4335f..bdc5de6 100644 --- a/tests/Models/UserWithRedis.php +++ b/tests/Models/UserWithRedis.php @@ -5,6 +5,11 @@ use Glorand\Model\Settings\Traits\HasSettingsRedis; use Illuminate\Database\Eloquent\Model; +/** + * Class UserWithRedis + * @package Glorand\Model\Settings\Tests\Models + * @method static first() + */ class UserWithRedis extends Model { use HasSettingsRedis; diff --git a/tests/Models/UserWithTextField.php b/tests/Models/UserWithTextField.php index 52e53e7..c507123 100644 --- a/tests/Models/UserWithTextField.php +++ b/tests/Models/UserWithTextField.php @@ -5,6 +5,11 @@ use Glorand\Model\Settings\Traits\HasSettingsField; use Illuminate\Database\Eloquent\Model; +/** + * Class UserWithTextField + * @package Glorand\Model\Settings\Tests\Models + * @method static first() + */ class UserWithTextField extends Model { use HasSettingsField; diff --git a/tests/Models/UsersWithTable.php b/tests/Models/UsersWithTable.php index 7d20538..4498800 100644 --- a/tests/Models/UsersWithTable.php +++ b/tests/Models/UsersWithTable.php @@ -2,12 +2,13 @@ namespace Glorand\Model\Settings\Tests\Models; -use Closure; use Glorand\Model\Settings\Traits\HasSettingsTable; use Illuminate\Database\Eloquent\Model; /** - * @method static whereHas(string $string, Closure $param) + * Class UsersWithTable + * @package Glorand\Model\Settings\Tests\Models + * @method static first() */ class UsersWithTable extends Model { diff --git a/tests/Models/WrongUser.php b/tests/Models/WrongUser.php index ac7ef31..e7b1610 100644 --- a/tests/Models/WrongUser.php +++ b/tests/Models/WrongUser.php @@ -4,6 +4,11 @@ use Illuminate\Database\Eloquent\Model; +/** + * Class WrongUser + * @package Glorand\Model\Settings\Tests\Models + * @method static first() + */ class WrongUser extends Model { protected $table = 'wrong_users'; diff --git a/tests/Models/WrongUserWithField.php b/tests/Models/WrongUserWithField.php index 44a32ce..a703f74 100644 --- a/tests/Models/WrongUserWithField.php +++ b/tests/Models/WrongUserWithField.php @@ -5,6 +5,11 @@ use Glorand\Model\Settings\Traits\HasSettingsField; use Illuminate\Database\Eloquent\Model; +/** + * Class WrongUserWithField + * @package Glorand\Model\Settings\Tests\Models + * @method static first() + */ class WrongUserWithField extends Model { use HasSettingsField; diff --git a/tests/TextFieldSettingsManagerTest.php b/tests/TextFieldSettingsManagerTest.php index d169f8a..41d102d 100644 --- a/tests/TextFieldSettingsManagerTest.php +++ b/tests/TextFieldSettingsManagerTest.php @@ -34,6 +34,7 @@ public function testInit() $traits = class_uses($this->model); $this->assertArrayHasKey(HasSettingsField::class, $traits); } + /** * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException */