From d2ea390f7eb636fc2f3b5e30c9c473fd6a4f7724 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 19 Jul 2024 13:18:15 +0800 Subject: [PATCH 1/2] more utility functions and docblocks for setting model --- composer.json | 2 +- src/Models/Setting.php | 88 +++++++++++++++++++++++++++ src/Providers/CoreServiceProvider.php | 13 +--- src/Support/Utils.php | 21 +++++++ 4 files changed, 111 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index c054f2c..1f74e16 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.4.29", + "version": "1.4.30", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 09c3a2e..41c12bd 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -8,6 +8,8 @@ use Fleetbase\Traits\HasApiModelBehavior; use Fleetbase\Traits\Searchable; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; class Setting extends EloquentModel { @@ -203,6 +205,16 @@ public static function getByKey(string $key) return static::where('key', $key)->first(); } + /** + * Retrieve the branding settings. + * + * This function fetches the branding settings, including icon URL, logo URL, and default theme. + * It first retrieves the values from the configuration and then checks for any overrides + * in the settings database. If the icon or logo UUID is valid, it fetches the corresponding + * file record to get the URL. + * + * @return array an associative array containing branding settings such as 'icon_url', 'logo_url', and 'default_theme' + */ public static function getBranding() { $brandingSettings = [ @@ -241,6 +253,15 @@ public static function getBranding() return $brandingSettings; } + /** + * Retrieve the branding logo URL. + * + * This function fetches the logo URL for branding purposes. It first checks the settings database + * for a logo UUID. If a valid UUID is found, it retrieves the corresponding file record and returns the URL. + * If no valid UUID is found, it returns the default logo URL from the configuration. + * + * @return string the URL of the branding logo + */ public static function getBrandingLogoUrl() { $logoUuid = static::where('key', 'branding.logo_uuid')->value('value'); @@ -256,6 +277,15 @@ public static function getBrandingLogoUrl() return config('fleetbase.branding.logo_url'); } + /** + * Retrieve the branding icon URL. + * + * This function fetches the icon URL for branding purposes. It first checks the settings database + * for an icon UUID. If a valid UUID is found, it retrieves the corresponding file record and returns the URL. + * If no valid UUID is found, it returns the default icon URL from the configuration. + * + * @return string the URL of the branding icon + */ public static function getBrandingIconUrl() { $iconUuid = static::where('key', 'branding.icon_uuid')->value('value'); @@ -271,13 +301,71 @@ public static function getBrandingIconUrl() return config('fleetbase.branding.icon_url'); } + /** + * Retrieve a value from the setting. + * + * This function fetches a value from the setting's stored JSON data. If the key is not found, + * it returns the provided default value. + * + * @param string $key the key to retrieve the value for + * @param mixed $defaultValue the default value to return if the key is not found + * + * @return mixed the value corresponding to the key, or the default value if the key is not found + */ public function getValue(string $key, $defaultValue = null) { return data_get($this->value, $key, $defaultValue); } + /** + * Retrieve a boolean value from the setting. + * + * This function fetches a value from the setting's stored JSON data and casts it to a boolean. + * + * @param string $key the key to retrieve the boolean value for + * + * @return bool the boolean value corresponding to the key + */ public function getBoolean(string $key) { return Utils::castBoolean($this->getValue($key, false)); } + + /** + * Check if there is a database connection. + * + * This function attempts to establish a connection to the database and checks if the 'settings' table exists. + * If the connection or table check fails, it returns false. + * + * @return bool true if there is a valid database connection and the 'settings' table exists, otherwise false + */ + public static function hasConnection(): bool + { + try { + // Try to make a simple DB call + DB::connection()->getPdo(); + + // Check if the settings table exists + if (!Schema::hasTable('settings')) { + return false; + } + } catch (\Throwable $e) { + // Connection failed, or other error occurred + return false; + } + + return true; + } + + /** + * Check if there is no database connection. + * + * This function checks if there is no valid database connection by negating the result of `hasConnection`. + * + * @return bool true if there is no valid database connection, otherwise false + */ + public static function doesntHaveConnection(): bool + { + return !static::hasConnection(); + } } diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index df19ffc..558da7c 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -9,8 +9,6 @@ use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Blade; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; use Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem; @@ -160,16 +158,7 @@ public function registerCustomBladeComponents() */ public function mergeConfigFromSettings() { - try { - // Try to make a simple DB call - DB::connection()->getPdo(); - - // Check if the settings table exists - if (!Schema::hasTable('settings')) { - return; - } - } catch (\Exception $e) { - // Connection failed, or other error occurred + if (Setting::doesntHaveConnection()) { return; } diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 4247838..0a2a624 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -2407,4 +2407,25 @@ public static function getObjectKeyValue($obj, $key, $defaultValue = null) { return $obj->{$key} ?? $defaultValue; } + + /** + * Check if there is a database connection. + * + * This function attempts to establish a connection to the database by making a simple PDO call. + * If the connection attempt fails, it catches the exception and returns false. + * + * @return bool true if a valid database connection is established, otherwise false + */ + public static function hasDatabaseConnection(): bool + { + try { + // Try to make a simple DB call + DB::connection()->getPdo(); + } catch (\Throwable $e) { + // Connection failed, or other error occurred + return false; + } + + return true; + } } From d72a8bc48eb64f587b5dc5b22355cc094c4969d1 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 19 Jul 2024 15:59:16 +0800 Subject: [PATCH 2/2] up rate limit to 80 req per min --- src/Providers/CoreServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index 558da7c..7677393 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -46,7 +46,7 @@ class CoreServiceProvider extends ServiceProvider \Fleetbase\Http\Middleware\ClearCacheAfterDelete::class, ], 'fleetbase.api' => [ - 'throttle:60,1', + 'throttle:80,1', \Illuminate\Session\Middleware\StartSession::class, \Fleetbase\Http\Middleware\AuthenticateOnceWithBasicAuth::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,