GeneralConfigType::class ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'data' => 'array' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass static::created(function (GeneralConfig $config) { Cache::delete('GeneralConfigGlobal'); }); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass static::updated(function (GeneralConfig $config) { Cache::delete('GeneralConfigGlobal'); }); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass static::deleted(function (GeneralConfig $config) { Cache::delete('GeneralConfigGlobal'); }); } /** * Get all cached configs * * @return \Illuminate\Database\Eloquent\Collection */ public static function getCachedConfigs(): Collection { if (!Cache::has('GeneralConfigGlobal')) { self::setCachedConfigs(); } return Cache::get('GeneralConfigGlobal'); } /** * Put configs into cache * * @return void */ protected static function setCachedConfigs() { $configs = GeneralConfig::all(); Cache::put('GeneralConfigGlobal', $configs); } /** * Get specific config, * Every config is mandatory * * @param GeneralConfigType $type * @return GeneralConfig */ public static function getCachedConfig(GeneralConfigType $type): GeneralConfig { return self::getCachedConfigs()->first(function (GeneralConfig $config) use ($type) { return $config->type->equals($type); }); } }