auditionadmin/app/Observers/EntryObserver.php

52 lines
1.2 KiB
PHP

<?php
namespace App\Observers;
use App\Actions\Tabulation\DoublerSync;
use App\Models\Audition;
use App\Models\Doubler;
use App\Models\Entry;
class EntryObserver
{
/**
* Handle the Entry "created" event.
*/
public function created(Entry $entry): void
{
// Count how many entries the student has for the event
$count = $entry->student->entriesForEvent($entry->audition->event_id)->count();
// If less than two entries, they're not a doubler
if ($count < 2) {
return;
}
// Update doublers for the event
$syncer = app(DoublerSync::class);
$syncer($entry->audition->event_id);
}
/**
* Handle the Entry "updated" event.
*/
public function updated(Entry $entry): void
{
$syncer = app(DoublerSync::class);
// Update doubler table when an entry is updated
$syncer();
}
/**
* Handle the Entry "deleted" event.
*/
public function deleted(Entry $entry): void
{
$syncer = app(DoublerSync::class);
Doubler::where('student_id', $entry->student_id)->delete();
$audition = Audition::where('id', $entry->audition_id)->first();
$syncer($audition->event_id);
}
}