ldc-gen-game-web/app/Models/Participation.php

239 lines
6.7 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Models;
use App\Casts\Ip;
use App\Lib\Helpers\FileStorageHelper;
use App\Notifications\Customer\PrizeHasBeenSent;
2026-10-02 12:09:46 +02:00
use App\Traits\Models\HasActivityLog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
2023-12-12 12:55:05 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-10-02 12:09:46 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2023-12-12 12:55:05 +01:00
use Illuminate\Database\Eloquent\Relations\HasOne;
2026-10-02 12:09:46 +02:00
use Illuminate\Support\Carbon;
use IPLib\Address\AddressInterface;
/**
* Participation.
*
2026-10-02 12:09:46 +02:00
* @property AddressInterface $ip IP
* @property string|null $sticker Path of the sticker file
* @property string|null $ticket Path of the ticket file
* @property string|null $barcode Path of the barcode file
* @property string|null $discount_code Treatment code of the BR
* @property string $comment Administrators comments
* @property bool $prize_sent Has the won prize been sent ?
* @property-read Carbon $created_at Creation Date.
* @property-read Carbon $updated_at Last update Date.
2023-12-12 12:25:45 +01:00
* @property-read string|null $ean_code Accessor for ean_code, from product table.
2026-10-02 12:09:46 +02:00
* @property-read Customer $customer Associated customer.
* @property-read Code $code Associated code.
* @property-read Product|null $product Associated product.
* @property-read InstantWin $instantWin Associated instantWin.
2024-02-15 15:53:36 +01:00
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Participation extends Model
{
2026-10-02 12:09:46 +02:00
use HasActivityLog;
2024-02-15 15:53:36 +01:00
/**
* The attributes that are mass assignable.
*
2026-10-02 12:09:46 +02:00
* @var list<string>
*/
2026-10-02 12:09:46 +02:00
#[\Override]
protected $fillable = [
'barcode',
'ticket',
'sticker',
2026-10-02 12:09:46 +02:00
'discount_code',
];
/**
* The attributes that should be cast.
*
2024-01-31 09:03:18 +00:00
* @var array<string, string>
*/
2026-10-02 12:09:46 +02:00
#[\Override]
protected $casts = [
'ip' => Ip::class,
2026-10-02 12:09:46 +02:00
'price_sent' => 'bool',
];
2023-12-13 18:10:37 +01:00
/**
* The model's attributes.
*
* @var array<string,mixed>
2023-12-13 18:10:37 +01:00
*/
2026-10-02 12:09:46 +02:00
#[\Override]
2023-12-13 18:10:37 +01:00
protected $attributes = [
2025-10-27 16:23:02 +01:00
'sticker' => null,
'ticket' => null,
'barcode' => null,
2026-10-02 12:09:46 +02:00
'discount_code' => null,
2023-12-13 18:10:37 +01:00
];
/**
* Perform any actions required after the model boots.
*/
protected static function booted(): void
{
2026-10-02 12:09:46 +02:00
static::creating(function (self $model): void {
self::manageFields($model);
});
2026-10-02 12:09:46 +02:00
static::updating(function (self $model): void {
self::manageFields($model);
2026-10-02 12:09:46 +02:00
/** @var Settings */
$settings = resolve(Settings::class);
2023-12-14 09:41:19 +01:00
if (
// ? Les notification doivent être envoyées ?
2026-10-02 12:09:46 +02:00
$settings->notify_prize_sent && $model->prize_sent && ! $model->getOriginal('prize_sent')
2023-12-14 09:41:19 +01:00
) {
2026-10-02 12:09:46 +02:00
/** @var Customer */
$customer = $model->customer;
$customer->notify(new PrizeHasBeenSent());
}
});
2026-10-02 12:09:46 +02:00
static::updated(function (self $model): void {
if ($model->getOriginal('barcode')) {
FileStorageHelper::removeOldFile($model, 'barcode');
}
if ($model->getOriginal('ticket')) {
FileStorageHelper::removeOldFile($model, 'ticket');
}
if ($model->getOriginal('sticker')) {
FileStorageHelper::removeOldFile($model, 'sticker');
}
});
2026-10-02 12:09:46 +02:00
static::deleted(function (self $model): void {
FileStorageHelper::removeFile($model, 'barcode');
FileStorageHelper::removeFile($model, 'ticket');
FileStorageHelper::removeFile($model, 'sticker');
});
}
2023-12-05 12:33:25 +01:00
// * RELATIONSHIPS
/**
* BelongsTo User relation
*
2026-10-02 12:09:46 +02:00
* @return BelongsTo<Customer, $this>
*/
2023-12-12 12:55:05 +01:00
public function customer(): BelongsTo
2021-08-18 17:08:02 +02:00
{
return $this->belongsTo(Customer::class);
}
/**
* BelongsTo Codes relation
*
2026-10-02 12:09:46 +02:00
* @return BelongsTo<Code, $this>
*/
2023-12-12 12:55:05 +01:00
public function code(): BelongsTo
2021-08-18 17:08:02 +02:00
{
return $this->belongsTo(Code::class, 'code_unique_id');
}
/**
* BelongsTo Product relation
*
2026-10-02 12:09:46 +02:00
* @return BelongsTo<Product, $this>
*/
2023-12-12 12:55:05 +01:00
public function product(): BelongsTo
2021-08-18 17:08:02 +02:00
{
return $this->belongsTo(Product::class);
}
/**
2023-12-12 14:56:12 +01:00
* One to one InstantWin relation
*
2026-10-02 12:09:46 +02:00
* @return HasOne<InstantWin, $this>
*/
2023-12-12 14:56:12 +01:00
public function instantWin(): HasOne
2021-08-18 17:08:02 +02:00
{
2023-12-12 14:56:12 +01:00
return $this->hasOne(InstantWin::class);
}
2024-02-15 15:53:36 +01:00
/**
* Activities logs One-to-many relationship.
*
2026-10-02 12:09:46 +02:00
* @return HasMany<ActivityLog, $this>
2024-02-15 15:53:36 +01:00
*/
2026-10-02 12:09:46 +02:00
public function activityLogs(): HasMany
2024-02-15 15:53:36 +01:00
{
return $this->hasMany(ActivityLog::class);
}
2023-12-05 12:33:25 +01:00
// * GETTERS
/**
2023-12-12 12:25:45 +01:00
* Accessor for ean_code, from product table.
*/
2026-10-02 12:09:46 +02:00
protected function eanCode(): Attribute
2021-08-18 17:08:02 +02:00
{
return Attribute::make(
2026-10-02 12:09:46 +02:00
get: fn () => $this->product?->ean_code,
);
}
// * METHODS
/**
* Make new participation but do not persist on DB
*
2026-10-02 12:09:46 +02:00
* @param AddressInterface $ip Ip for the participation.
* @param Customer $customer The Customer playing the game.
* @param Code|null $code The unique code used to play.
* @param Product|null $product The product that holds the sticker if codes are actived.
*/
public static function makeNew(
AddressInterface $ip,
Customer $customer,
?Code $code = null,
?Product $product = null
): self {
$self = new self();
$self->ip = $ip;
$self->customer()->associate($customer);
2026-10-02 12:09:46 +02:00
if (! is_null($code)) {
$self->code()->associate($code);
}
2026-10-02 12:09:46 +02:00
if (! is_null($product)) {
$self->product()->associate($product);
}
2026-10-02 12:09:46 +02:00
return $self;
}
/**
* Check if a customer has already won
*/
public static function customerAlreadyHasAnInstantWin(Customer $customer): bool
{
return self::query()->whereHas('instantWin')
2026-10-02 12:09:46 +02:00
->whereHas('customer', fn (Builder $q) => $q->where('id', $customer->id))
->exists();
}
/**
* Common methods function.
*/
2026-10-02 12:09:46 +02:00
private static function manageFields(self $model): void
{
2023-12-13 18:10:37 +01:00
if ($model->barcode) {
$model->barcode = FileStorageHelper::storeFile($model, $model->barcode, true);
}
if ($model->ticket) {
$model->ticket = FileStorageHelper::storeFile($model, $model->ticket, true);
}
if ($model->sticker) {
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
}
2026-10-02 12:09:46 +02:00
if (! $model->comment) {
$model->comment = '';
}
}
}