diff --git a/app/Actions/Tabulation/AllJudgesCount.php b/app/Actions/Tabulation/AllJudgesCount.php index d0b9978..619d817 100644 --- a/app/Actions/Tabulation/AllJudgesCount.php +++ b/app/Actions/Tabulation/AllJudgesCount.php @@ -6,15 +6,17 @@ namespace App\Actions\Tabulation; use App\Exceptions\TabulationException; use App\Models\Entry; +use App\Services\AuditionService; class AllJudgesCount implements CalculateEntryScore { protected CalculateScoreSheetTotal $calculator; + protected AuditionService $auditionService; public function __construct() { - #$this->calculator = new CalculateScoreSheetTotal(); $this->calculator = app(CalculateScoreSheetTotal::class); + $this->auditionService = app(AuditionService::class); } public function calculate(string $mode, Entry $entry): array @@ -29,7 +31,7 @@ class AllJudgesCount implements CalculateEntryScore protected function getJudgeTotals($mode, Entry $entry) { $scores = []; - foreach ($entry->audition->judges as $judge) { + foreach ($this->auditionService->getJudges($entry->audition) as $judge) { $scores[] = $this->calculator->__invoke($mode, $entry, $judge); } $sums = []; @@ -59,7 +61,7 @@ class AllJudgesCount implements CalculateEntryScore protected function areAllJudgesIn(Entry $entry): void { - $assignedJudgeCount = $entry->audition->judges->count(); + $assignedJudgeCount = $this->auditionService->getJudges($entry->audition)->count(); if ($entry->scoreSheets->count() !== $assignedJudgeCount) { throw new TabulationException('Not all score sheets are in'); } @@ -67,7 +69,7 @@ class AllJudgesCount implements CalculateEntryScore protected function areAllJudgesValid(Entry $entry): void { - $validJudgeIds = $entry->audition->judges->sort()->pluck('id')->toArray(); + $validJudgeIds = $this->auditionService->getJudges($entry->audition)->sort()->pluck('id')->toArray(); $existingJudgeIds = $entry->scoreSheets->sort()->pluck('user_id')->toArray(); if ($validJudgeIds !== $existingJudgeIds) { throw new TabulationException('Score exists from a judge not assigned to this audition'); diff --git a/app/Actions/Tabulation/CalculateScoreSheetTotal.php b/app/Actions/Tabulation/CalculateScoreSheetTotal.php index e0c08ac..ce626e2 100644 --- a/app/Actions/Tabulation/CalculateScoreSheetTotal.php +++ b/app/Actions/Tabulation/CalculateScoreSheetTotal.php @@ -5,7 +5,6 @@ namespace App\Actions\Tabulation; use App\Exceptions\TabulationException; -use App\Models\Audition; use App\Models\Entry; use App\Models\ScoreSheet; use App\Models\User; @@ -14,6 +13,7 @@ use App\Services\AuditionService; class CalculateScoreSheetTotal { protected AuditionService $auditionService; + public function __construct(AuditionService $auditionService) { $this->auditionService = $auditionService; @@ -26,7 +26,6 @@ class CalculateScoreSheetTotal if (! $scoreSheet) { throw new TabulationException('No score sheet by that judge for that entry'); } - #$subscores = $this->getSubscores($mode, $entry->audition); $subscores = $this->auditionService->getSubscores($entry->audition, $mode); $scoreTotal = 0; $weightsTotal = 0; @@ -57,29 +56,4 @@ class CalculateScoreSheetTotal throw new TabulationException('Invalid judge provided'); } } - - protected function getSubscores($mode, Audition $audition) - { - static $seatingSubscores = []; - static $advancementSubscores = []; - - if ($mode === 'seating') { - if (! isset($seatingSubscores[$audition->id])) { - $seatingSubscores[$audition->id] = $audition->scoringGuide->subscores->where('for_seating', - true)->sortBy('tiebreak_order'); - } - - return $seatingSubscores[$audition->id]; - } - if ($mode === 'advancement') { - if (! isset($advancementSubscores[$audition->id])) { - $advancementSubscores[$audition->id] = $audition->scoringGuide->subscores->where('for_advance', - true)->sortBy('tiebreak_order'); - } - - return $advancementSubscores[$audition->id]; - } - - throw new TabulationException('Invalid mode requested. Mode must be seating or advancement'); - } } diff --git a/app/Services/AuditionService.php b/app/Services/AuditionService.php index fbed6f1..5ba6a3d 100644 --- a/app/Services/AuditionService.php +++ b/app/Services/AuditionService.php @@ -4,7 +4,7 @@ namespace App\Services; use App\Exceptions\AuditionServiceException; use App\Models\Audition; -use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Cache; class AuditionService { @@ -21,26 +21,34 @@ class AuditionService */ public function getSubscores(Audition $audition, $mode = 'seating', $sort = 'tiebreak') { - static $auditionSubscores = []; - $this->validateAudition($audition); - $this->validateMode($mode); - $this->validateSort($sort); + $cacheKey = 'auditionSubscores-'.$audition->id.'-'.$mode.'-'.$sort; - $sortColumn = match ($sort) { - 'tiebreak' => 'tiebreak_order', - 'display' => 'display_order', - }; - $modeColumn = match ($mode) { - 'seating' => 'for_seating', - 'advancement' => 'for_advance', - }; - if (! isset($auditionSubscores[$mode][$sort])) { - $auditionSubscores[$mode][$sort] = $audition->scoringGuide->subscores->where($modeColumn, true)->sortBy($sortColumn); - } else { - Log::debug('Using cached subscores'); - } + return Cache::remember($cacheKey, 10, function () use ($audition, $mode, $sort) { + $this->validateAudition($audition); + $this->validateMode($mode); + $this->validateSort($sort); - return $auditionSubscores[$mode][$sort]; + $sortColumn = match ($sort) { + 'tiebreak' => 'tiebreak_order', + 'display' => 'display_order', + }; + $modeColumn = match ($mode) { + 'seating' => 'for_seating', + 'advancement' => 'for_advance', + }; + + return $audition->scoringGuide->subscores->where($modeColumn, true)->sortBy($sortColumn); + }); + } + + public function getJudges(Audition $audition) + { + $cacheKey = 'auditionJudges-'.$audition->id; + return Cache::remember($cacheKey, 10, function () use ($audition) { + $this->validateAudition($audition); + + return $audition->judges; + }); } protected function validateAudition($audition)