auditionadmin/app/Actions/Tabulation/PublishSeats.php

58 lines
2.0 KiB
PHP

<?php
namespace App\Actions\Tabulation;
use App\Exceptions\AuditionAdminException;
use App\Models\Audition;
use App\Models\Seat;
use Illuminate\Support\Facades\Cache;
class PublishSeats
{
public function __construct()
{
//
}
/**
* Publishes the given audition with the provided seats.
*
* This method first validates that the seats array is not empty. If the array is empty,
* an AuditionAdminException is thrown.
*
* Next, it deletes existing records in the `seats` table associated with the provided audition
* using the `audition_id`.
*
* Then, it iterates through the provided seats array to create new records in the `seats` table
* with the specified `ensemble_id`, `audition_id`, `seat`, and `entry_id`.
*
* Finally, it marks the audition as having its seats published by adding a relevant flag
* to the audition, and clears cached data associated with the results seat list and
* public results page entries in the cache store.
*
* @param Audition $audition The audition instance to be published.
* @param array $seats An array of seat data to be associated with the audition.
*
* @throws AuditionAdminException If the provided seats array is empty.
*/
public function __invoke(Audition $audition, array $seats): void
{
if (count($seats) === 0) {
throw new AuditionAdminException('Cannot publish an audition with no seats.');
}
// Delete from the seats table where audition_id = $audition->id
Seat::where('audition_id', $audition->id)->delete();
foreach ($seats as $seat) {
Seat::create([
'ensemble_id' => $seat['ensemble_id'],
'audition_id' => $seat['audition_id'],
'seat' => $seat['seat'],
'entry_id' => $seat['entry_id'],
]);
}
$audition->addFlag('seats_published');
Cache::forget('resultsSeatList');
Cache::forget('publicResultsPage');
}
}