134 lines
4.3 KiB
PHP
134 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* Prize.
|
|
*
|
|
* @property string $label Label
|
|
* @property string $picture Path of the picture
|
|
* @property string $picture_alt Alt text for the picture
|
|
* @property string $picture_title Title text for the picture
|
|
* @property string $arg_title Description title
|
|
* @property string|null $arg_subtitle Description subtitle
|
|
* @property string $arg_content Description content
|
|
* @property integer $quantity Quantity
|
|
* @property string|null $pdf_winning_proof Path of the winning proof file
|
|
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\InstantWin[] $instantWins
|
|
* Associated instantWins.
|
|
*/
|
|
class Prize extends Model
|
|
{
|
|
use IsCachedAsCollection;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'label',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'arg_title',
|
|
'arg_subtitle',
|
|
'arg_content',
|
|
'quantity',
|
|
'pdf_winning_proof'
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
ToolboxHelper::assertGameIsNotOppened();
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function () {
|
|
self::removeCached();
|
|
});
|
|
static::updating(function (self $model) {
|
|
ToolboxHelper::assertGameIsNotOppened();
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
self::removeCached();
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'pdf_winning_proof');
|
|
});
|
|
static::deleting(function (self $model) {
|
|
ToolboxHelper::assertGameIsNotOppened();
|
|
if ($model->instantWins()->count()) {
|
|
throw ValidationException::withMessages([
|
|
'error' =>
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
trans("Il n'est pas possible de supprimer ce lot car il est associé a des instants gagnants,\u{00A0} supprimer les instants gagnants d'abord.")
|
|
]);
|
|
}
|
|
});
|
|
static::deleted(function (self $model) {
|
|
self::removeCached();
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'pdf_winning_proof');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Common methods function.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model)
|
|
{
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
if ($model->pdf_winning_proof) {
|
|
$model->pdf_winning_proof = FileStorageHelper::storeFile($model, $model->pdf_winning_proof, true);
|
|
}
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* HasMany InstantWin relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function instantWins(): HasMany
|
|
{
|
|
return $this->hasMany(InstantWin::class, 'prize_id');
|
|
}
|
|
|
|
// * CACHE METHOD
|
|
|
|
/**
|
|
* Change query for cached elements
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
protected static function queryCached(Builder $query): Builder
|
|
{
|
|
return $query->with('instantWins');
|
|
}
|
|
}
|