68 lines
1.6 KiB
PHP
68 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
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
|
|
Doubler::syncForEvent($entry->audition->event_id);
|
|
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "updated" event.
|
|
*/
|
|
public function updated(Entry $entry): void
|
|
{
|
|
// Update doubler table when an entry is updated
|
|
Doubler::syncForEvent($entry->audition->event_id);
|
|
if ($entry->wasChanged('audition_id')) {
|
|
$originalData = $entry->getOriginal();
|
|
Doubler::syncForEvent($originalData->audition->event_id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "deleted" event.
|
|
*/
|
|
public function deleted(Entry $entry): void
|
|
{
|
|
Doubler::where('student_id', $entry->student_id)->delete();
|
|
$audition = Audition::where('id', $entry->audition_id)->first();
|
|
Doubler::syncForEvent($audition->event_id);
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "restored" event.
|
|
*/
|
|
public function restored(Entry $entry): void
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* Handle the Entry "force deleted" event.
|
|
*/
|
|
public function forceDeleted(Entry $entry): void
|
|
{
|
|
|
|
}
|
|
}
|