Skip to content

Commit

Permalink
Fix: Error is thrown if the key is not found in the database.
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Oct 11, 2023
1 parent 6bb623c commit efa0869
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "anisaronno/laravel-settings",
"description": "Laravel Settings Table",
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"keywords": [
"laravel-settings",
Expand Down
27 changes: 12 additions & 15 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@
use AnisAronno\LaravelSettings\Models\SettingsProperty;
use Illuminate\Support\Collection;

if (!function_exists('getAllSettings')) {

if (!function_exists('getSettings')) {
/**
* Get all Settings
* Get Settings
*
* @return Collection
* @param string $key
* @return string|null
* @throws Exception
*/
function getAllSettings(): Collection
function getSettings(string $key): string
{
return AnisAronno\LaravelSettings\Helpers\SettingsHelper::getAllSettings();
return AnisAronno\LaravelSettings\Helpers\SettingsHelper::getSettings($key);
}
}

if (!function_exists('getSettings')) {
if (!function_exists('getAllSettings')) {

/**
* Get Settings
* Get all Settings
*
* @param string $key
* @return string|null
* @return Collection
* @throws Exception
*/
function getSettings(string $key): ?string
function getAllSettings(): Collection
{
try {
return AnisAronno\LaravelSettings\Helpers\SettingsHelper::getSettings($key);
} catch (\Throwable $th) {
return null;
}
return AnisAronno\LaravelSettings\Helpers\SettingsHelper::getAllSettings();
}
}

Expand Down
34 changes: 20 additions & 14 deletions src/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ public static function getSettings(string $settingsKey): string
{
$key = CacheHelper::getLaravelSettingsCacheKey();


try {
$settings = CacheHelper::init($key)->remember($settingsKey, now()->addDay(), function () use ($settingsKey) {
return SettingsProperty::where('settings_key', $settingsKey)->first();

return SettingsProperty::select('settings_value')->find($settingsKey);
});
return $settings['settings_value'];

if(isset($settings['settings_value'])) {
return $settings['settings_value'];
} else {
throw new Exception('The '.$settingsKey.' key was not found in the settings table.', 404);
}

} catch (\Throwable $th) {
throw new Exception($th->getMessage(), 400);
Expand All @@ -43,7 +48,7 @@ public static function getSettings(string $settingsKey): string
*/
public static function setSettings(string $key, string $value): SettingsProperty
{
if(! self::issKeyAlphaDash($key, 'ascii')) {
if(! self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -80,7 +85,7 @@ public static function setSettings(string $key, string $value): SettingsProperty
*/
public static function upsertSettings(string $key, string $value): SettingsProperty
{
if(! self::issKeyAlphaDash($key, 'ascii')) {
if(! self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -132,7 +137,7 @@ public static function getAllSettings(): Collection
*/
public static function updateSettings(string $key, string $value = ''): bool
{
if(! self::issKeyAlphaDash($key, 'ascii')) {
if(! self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -177,14 +182,15 @@ public static function deleteSettings(string $key): bool
}

/**
* Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters,
* dashes, and underscores.
*
* @param mixed $value
* @return bool
*/
private static function issKeyAlphaDash($value, $parameters)
* Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
* If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters,
* dashes, and underscores.
*
* @param mixed $value
* @param string $parameters
* @return bool
*/
private static function isKeyAlphaDash($value, string $parameters = 'ascii'): bool
{
if (! is_string($value) && ! is_numeric($value)) {
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/Models/SettingsProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @method static where(string $string, string $settingsKey)
* @method static select(string $string, string $string1)
* @method static select(string $string, string $string1='')
* @method static updateOrCreate(string[] $array, array $array1)
* @method static findOrFail($key)
* @property mixed|string $settings_key
Expand All @@ -20,17 +20,17 @@ class SettingsProperty extends Model
{
use HasFactory;

/**
* @var mixed|string
*/
protected $table = 'settings';

protected $fillable = [
'settings_key',
'settings_value',
'user_id',
];

/**
* @var mixed|string
*/
protected $table = 'settings';

protected $primaryKey = 'settings_key';

public $incrementing = false;
Expand Down

0 comments on commit efa0869

Please sign in to comment.