Skip to content

Commit

Permalink
Merge pull request #102 from fleetbase/dev-v1.4.30
Browse files Browse the repository at this point in the history
v1.4.30
  • Loading branch information
roncodes authored Jul 19, 2024
2 parents 4163cce + d72a8bc commit 83577d0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
88 changes: 88 additions & 0 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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();
}
}
15 changes: 2 additions & 13 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,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,
Expand Down Expand Up @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 83577d0

Please sign in to comment.