ldc-groupe/app/Models/CompanySector.php
2025-12-01 14:51:11 +01:00

153 lines
No EOL
4 KiB
PHP

<?php
namespace App\Models;
use App\Contracts\TranslatableModel;
use App\Helpers\TranslationHelper;
use App\Traits\Models\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
/**
* @property integer $id Id.
* @property string $name Name of the company sector.
* @property string $published Publish status of the company sector.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
* @property-read \Illuminate\Support\Carbon $deleted_at Deleted date.
*/
class CompanySector extends Model implements TranslatableModel
{
use ActivityLog;
use HasFactory;
use HasTranslations;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string, array[]>
*/
protected $fillable = [
'name',
'published',
];
/**
* The translatable columns.
*
* @var list<string>
*/
public $translatable = [
'name',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'published' => 'boolean',
];
/**
* 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();
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function () {
self::resetCache();
});
static::deleted(function () {
self::resetCache();
});
static::restored(function () {
self::resetCache();
});
static::forceDeleted(function () {
self::resetCache();
});
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name');
self::validateFallbackTranslations($model);
}
/**
* Assert fallback locale translation is present.
*
* @param self $model
* @return void
* @throws \Illuminate\Validation\ValidationException Translations missing.
*/
public static function validateFallbackTranslations(self $model): void
{
if (!$model->published) {
return;
}
TranslationHelper::validateFallbackTranslations($model);
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
public static function resetCache(): void
{
Cache::forget('allCompanySectors');
self::getAllCompanySectors();
Cache::forget('publishedCompanySectors');
self::getPublishedCompanySectors();
}
/**
* Get all company sectors.
*
* @return \Illuminate\Database\Eloquent\Collection<int,self>
*/
public static function getAllCompanySectors(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('allCompanySectors', 86400, function () {
return self::query()->get();
});
}
/**
* Get all published company sectors.
*
* @return \Illuminate\Database\Eloquent\Collection<int,self>
*/
public static function getPublishedCompanySectors(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('publishedCompanySectors', 86400, function () {
return self::getAllCompanySectors()->where('published', true);
});
}
}