133 lines
4.0 KiB
PHP
133 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Exceptions\AuditionServiceException;
|
|
use App\Models\Audition;
|
|
use App\Models\Ensemble;
|
|
use App\Models\SeatingLimit;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class AuditionService
|
|
{
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public static Collection $allAuditionIds;
|
|
|
|
public function __construct()
|
|
{
|
|
$cacheKey = 'allAuditionIds';
|
|
self::$allAuditionIds = Cache::remember($cacheKey, 60, function () {
|
|
return Audition::pluck('id');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @throws AuditionServiceException
|
|
*/
|
|
public function getSubscores(Audition $audition, $mode = 'seating', $sort = 'tiebreak')
|
|
{
|
|
$cacheKey = 'auditionSubscores-'.$audition->id.'-'.$mode.'-'.$sort;
|
|
|
|
return Cache::remember($cacheKey, 10, function () use ($audition, $mode, $sort) {
|
|
$this->validateAudition($audition);
|
|
$this->validateMode($mode);
|
|
$this->validateSort($sort);
|
|
|
|
$sortColumn = match ($sort) {
|
|
'tiebreak' => 'tiebreak_order',
|
|
'display' => 'display_order',
|
|
};
|
|
$modeColumn = match ($mode) {
|
|
'seating' => 'for_seating',
|
|
'advancement' => 'for_advance',
|
|
};
|
|
$audition->load('scoringGuide.subscores');
|
|
|
|
return $audition->scoringGuide->subscores->where($modeColumn, true)->sortBy($sortColumn);
|
|
});
|
|
}
|
|
|
|
public function getJudgesOLD(Audition $audition)
|
|
{
|
|
$cacheKey = 'auditionJudges-'.$audition->id;
|
|
|
|
return Cache::remember($cacheKey, 10, function () use ($audition) {
|
|
$this->validateAudition($audition);
|
|
|
|
return $audition->judges;
|
|
});
|
|
}
|
|
|
|
public function getJudges(Audition $audition)
|
|
{
|
|
$cacheKey = 'auditionJudgeAssignments';
|
|
$assignments = Cache::remember($cacheKey, 60, function () {
|
|
$allAuditions = Audition::with('judges')->get();
|
|
$return = [];
|
|
foreach ($allAuditions as $audition) {
|
|
$return[$audition->id] = $audition->judges;
|
|
}
|
|
|
|
return $return;
|
|
});
|
|
|
|
return $assignments[$audition->id];
|
|
}
|
|
|
|
public function getSeatingLimits(Audition $audition)
|
|
{
|
|
$cacheKey = 'auditionSeatingLimits';
|
|
$allLimits = Cache::remember($cacheKey, 60, function () {
|
|
$lims = [];
|
|
$auditions = Audition::all();
|
|
$ensembles = Ensemble::orderBy('rank')->get();
|
|
foreach ($auditions as $audition) {
|
|
foreach ($ensembles as $ensemble) {
|
|
if ($ensemble->event_id !== $audition->event_id) {
|
|
continue;
|
|
}
|
|
$lims[$audition->id][$ensemble->id] = [
|
|
'ensemble' => $ensemble,
|
|
'limit' => 0,
|
|
];
|
|
}
|
|
}
|
|
$limits = SeatingLimit::all();
|
|
|
|
foreach ($limits as $limit) {
|
|
$lims[$limit->audition_id][$limit->ensemble_id] = collect([
|
|
'ensemble' => $ensembles->find($limit->ensemble_id),
|
|
'limit' =>$limit->maximum_accepted,
|
|
]);
|
|
}
|
|
return collect($lims);
|
|
});
|
|
|
|
return collect($allLimits[$audition->id]) ?? [];
|
|
}
|
|
|
|
protected function validateAudition($audition)
|
|
{
|
|
if (! $audition->exists()) {
|
|
throw new AuditionServiceException('Invalid audition provided');
|
|
}
|
|
}
|
|
|
|
protected function validateMode($mode)
|
|
{
|
|
if ($mode !== 'seating' && $mode !== 'advancement') {
|
|
throw new AuditionServiceException('Invalid mode requested. Mode must be seating or advancement');
|
|
}
|
|
}
|
|
|
|
protected function validateSort($sort)
|
|
{
|
|
if ($sort !== 'tiebreak' && $sort !== 'display') {
|
|
throw new AuditionServiceException('Invalid sort requested. Sort must be tiebreak or weight');
|
|
}
|
|
}
|
|
}
|