163 lines
4.8 KiB
PHP
163 lines
4.8 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;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Uwil\LaravelCommon\Rules\IsValidVimeoId;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
|
|
/**
|
|
* @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 implements SchemaOrgInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SchemaOrgTrait;
|
|
|
|
/**
|
|
* 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::remember('9publishedHomeVideosOrderedByOrderWithProducts', 86400, 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::remember('6publishedVideosOrderedByOrderWithProducts', 86400, function () {
|
|
return self::query()
|
|
->where('published', true)
|
|
->orderBy('order')
|
|
->with('product')
|
|
->take(6)
|
|
->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg.
|
|
*
|
|
* @return \Spatie\SchemaOrg\MediaObject
|
|
*/
|
|
public function toSchemaOrg(): \Spatie\SchemaOrg\MediaObject
|
|
{
|
|
$url = (IsValidVimeoId::validates($this->video_id))
|
|
? "https://player.vimeo.com/video/$this->video_id"
|
|
: "https://www.youtube.com/embed/$this->video_id";
|
|
return Schema::mediaObject()
|
|
->name($this->title)
|
|
->productionCompany($this->getOrganizationSchema())
|
|
->embedUrl($url);
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
}
|