auditionadmin/app/Observers/EntryFlagObserver.php

55 lines
1.7 KiB
PHP

<?php
namespace App\Observers;
use App\Actions\Tabulation\DoublerSync;
use App\Exceptions\AuditionAdminException;
use App\Models\Doubler;
use App\Models\EntryFlag;
use Illuminate\Support\Facades\Cache;
use function in_array;
class EntryFlagObserver
{
/**
* Handle the EntryFlag "created" event.
*/
public function creating(EntryFlag $entryFlag): void
{
if (in_array($entryFlag->flag_name->value, ['declined', 'no_show', 'failed_prelim'])) {
if ($entryFlag->entry->audition->hasFlag('seats_published') || $entryFlag->entry->audition->hasFlag('advancement_published')) {
throw new AuditionAdminException('Cannot change flag for published auditions.');
}
}
}
public function created(EntryFlag $entryFlag): void
{
$syncer = app(DoublerSync::class);
$syncer();
Cache::forget('rank_advancement_'.$entryFlag->entry->audition_id);
Cache::forget('rank_seating_'.$entryFlag->entry->audition_id);
}
/**
* Handle the EntryFlag "deleted" event.
*/
public function deleting(EntryFlag $entryFlag): void
{
if (in_array($entryFlag->flag_name->value, ['declined', 'no_show', 'failed_prelim'])) {
if ($entryFlag->entry->audition->hasFlag('seats_published') || $entryFlag->entry->audition->hasFlag('advancement_published')) {
throw new AuditionAdminException('Cannot change flag for published auditions.');
}
}
}
public function deleted(EntryFlag $entryFlag): void
{
Doubler::syncDoublers();
Cache::forget('rank_advancement_'.$entryFlag->entry->audition_id);
Cache::forget('rank_seating_'.$entryFlag->entry->audition_id);
}
}