ldc-legaulois/app/Models/Video.php
2024-10-08 12:22:52 +02:00

142 lines
4.1 KiB
PHP

<?php
namespace App\Models;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
/**
* @property string $title Video title.
* @property string $video_id Youtube/Vimeo video ID.
* @property \App\Models\Product|null $product_id Associated product.
* @property integer $order Change order request.
* @property boolean $published Video publish status.
* @property boolean $is_on_homepage Is the video on the home page?
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class Video extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'video_id',
'product_id',
'is_on_homepage',
'order',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'published' => 'boolean',
'is_on_homepage' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $video) {
self::setOrder($video);
});
static::created(function () {
self::resetCache();
});
static::updated(function () {
self::resetCache();
});
static::deleted(function () {
self::resetCache();
});
}
// * METHODS
/**
* Set order after the last element of the list.
*
* @param self $model
* @return void
*/
private static function setOrder(self $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('9publishedHomeVideosOrderedByOrderWithProducts');
self::get9PublishedHomeVideosOrderedWithProducts();
Cache::forget('6publishedVideosOrderedByOrderWithProducts');
self::get6PublishedVideosOrderedWithProducts();
}
/**
* Get 9 published Videos to display in homepage ordered by order with products relations.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function get9PublishedHomeVideosOrderedWithProducts(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('9publishedHomeVideosOrderedByOrderWithProducts', function () {
return self::query()
->where('published', true)
->where('is_on_homepage', true)
->orderBy('order')
->with('product')
->limit(9)
->get();
});
}
/**
* Get 9 published Videos to display in homepage ordered by order with products relations.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function get6PublishedVideosOrderedWithProducts(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('6publishedVideosOrderedByOrderWithProducts', function () {
return self::query()
->where('published', true)
->orderBy('order')
->with('product')
->take(6)
->get();
});
}
// * RELATIONSHIPS
/**
* Get the product for the video.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Product::class);
}
}