ldc-legaulois/app/Models/Offer.php
2025-06-10 13:30:22 +02:00

152 lines
5.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Settings\SettingsType;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
/**
* @property string|null $title Offer title.
* @property string|null $location Offer location.
* @property string|null $location_details Offer location details.
* @property string|null $location_latitude Offer location latitude.
* @property string|null $location_longitude Offer location longitude.
* @property string|null $contract Offer contract.
* @property string|null $contract_period Offer contract period.
* @property string|null $function Offer function.
* @property string|null $experience_level Offer experience level.
* @property string|null $qualifications Offer qualifications.
* @property string|null $skills Offer skills.
* @property string|null $picture Offer picture.
* @property string|null $picture_alt Picture alt attribute.
* @property string|null $picture_title Picture title attribute.
* @property string|null $link Offer link.
* @property string|null $description Offer description.
* @property string|null $recruiter_firstname Offer recruiter firstname.
* @property string|null $recruiter_lastname Offer recruiter lastname.
* @property string|null $recruiter_job Offer recruiter job.
* @property string|null $recruiter_picture Offer recruiter picture.
* @property boolean $published Offer publish status.
* @property \Carbon\Carbon $published_at Offer published date.
* @property \Carbon\Carbon $created_at Created date.
* @property \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Product $products Product polymorphic relationship.
*/
class Offer extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'location',
'location_details',
'location_latitude',
'location_longitude',
'contract',
'contract_period',
'function',
'experience_level',
'qualifications',
'skills',
'picture',
'picture_alt',
'picture_title',
'link',
'description',
'recruiter_firstname',
'recruiter_lastname',
'recruiter_job',
'recruiter_picture',
'published',
'published_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'skills' => 'array',
'published' => 'boolean',
'published_at' => 'datetime',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $offer) {
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
$offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true);
});
static::created(function () {
self::resetCache();
});
static::updating(function (self $offer) {
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
$offer->recruiter_picture = FileStorageHelper::storeFile($offer, $offer->recruiter_picture, true);
});
static::updated(function (self $offer) {
FileStorageHelper::removeOldFile($offer, 'picture');
FileStorageHelper::removeOldFile($offer, 'recruiter_picture');
self::resetCache();
});
static::deleted(function (self $offer) {
FileStorageHelper::removeFile($offer, 'picture');
FileStorageHelper::removeFile($offer, 'recruiter_picture');
self::resetCache();
});
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('offersCached');
Cache::forget('offersCountCached');
self::getOffersCached();
self::getOffersCountCached();
}
/**
* Get all offers.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getOffersCached(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('offersCached', 86400, function () {
return self::query()->where('published', true)->get();
});
}
/**
* Get offers count.
*
* @return integer
*/
public static function getOffersCountCached(): int
{
return Cache::remember('offersCountCached', 86400, function () {
return GeneralSetting::query()->where('type', SettingsType::offersCount)->firstOrFail()->value['count'];
});
}
}