$locales Selected locales used in the application. * @property array $sub_title Sous-titre de l'application. * @property array $footer_phrase Texte en bas de page. * @property string $color HTML Color string. * @property string $logo Logo path for the app. * @property integer $home_timeout Timeout time in seconds before returning to homepage. * @property string $tenant_id Id of the related tenant. * @property-read \Illuminate\Support\Carbon $created_at Created date. * @property-read \Illuminate\Support\Carbon $updated_at Updated date. * */ class Settings extends Model { use HasFactory; use IsCachedAsCollection; use BelongsToTenant; use ActivityLog; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'locales', 'sub_title', 'footer_phrase', 'color', 'logo', 'home_timeout', 'tenant_id', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'locales' => 'array', 'sub_title' => 'array', 'footer_phrase' => 'array', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::manageFields($model); }); static::created(function (self $model) { self::resetCache($model->tenant_id); }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileHelper::removeOldFile($model, 'logo'); self::resetCache($model->tenant_id); }); static::deleted(function (self $model) { FileHelper::removeFile($model, 'logo'); self::removeFromCache($model->tenant_id); }); } // * METHODS /** * Common methods function. * * @param self $model * @return void */ private static function manageFields(self $model) { // * TRANSFORMATIONS /** @phpstan-ignore-next-line */ if ($model->logo instanceof UploadedFile) { $model->logo = ShowroomFileHelper::storeFileWithUUIDName($model->logo, $model); } if (!$model->locales) { $model->locales = []; } } /** * Get from cache a Setting model with a key like this : {tenant}_settings and for remember it 24 hours. * * @param string|null $tenant_id * * @return self|null */ public static function getFromCache(?string $tenant_id): self|null { if (!$tenant_id) { return null; }; return Cache::remember( self::getCacheKey($tenant_id), 86400, function () use ($tenant_id) { return self::query()->where('tenant_id', $tenant_id)->first(); }, ); } /** * Reset all cache elements related to a given tenant id. * * @param string $tenant_id * * @return void */ private static function resetCache(string $tenant_id): void { Cache::forget(self::getCacheKey($tenant_id)); self::getFromCache($tenant_id); } /** * Remove from cache a Setting model. * * @param string $tenant_id * * @return void */ private static function removeFromCache(string $tenant_id): void { Cache::forget(self::getCacheKey($tenant_id)); } /** * Get the cache key for a setting related to its tenant. * * @param string $tenant_id * * @return string */ public static function getCacheKey(string $tenant_id): string { return \sprintf('%s_settings', $tenant_id); } /** * Create a new Setting with fake datas. * * @param string $tenantId The ID of the related tenant. * * @return void */ public static function createNewData(string $tenantId) { $settings = new Settings(); $settings->logo = FileHelper::storeFile( new Settings(), new \SplFileInfo( \resource_path('../database/factories/assets/settings/default-picture-200x200.jpg') ), prefix: $tenantId ); $settings->locales = ['fr']; $settings->sub_title = [ 'fr' => 'FR', ]; $settings->footer_phrase = [ 'fr' => 'FR', ]; $settings->color = '#000000'; $settings->home_timeout = 60; $settings->tenant_id = $tenantId; $settings->saveOrFail(); } }