auditionadmin/app/Services/DoublerService.php

64 lines
2.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Entry;
use App\Models\Student;
use Illuminate\Support\Facades\Cache;
class DoublerService
{
protected $doublersCacheKey = 'doublers';
protected $auditionCacheService;
protected $tabulationService;
/**
* Create a new class instance.
*/
public function __construct(AuditionCacheService $auditionCacheService, TabulationService $tabulationService)
{
$this->auditionCacheService = $auditionCacheService;
$this->tabulationService = $tabulationService;
}
public function getDoublers(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember($this->doublersCacheKey, 10, function () {
$students = Student::withCount('entries')
->with('entries')
->havingRaw('entries_count > ?', [1])
->get();
return $students;
});
}
public function getDoublerInfo($id): Array
{
// When getting a doubler we need to know
// 1) What their entrires are
// 2) For each audition they're entered in, what is their rank
// 3) For each audition they'er entered in, how many entries are unscored
// 4) How many are accepted on that instrument
$doubler = $this->getDoublers()->firstWhere('id',$id);
$info = [];
foreach ($doubler->entries as $entry) {
$info[] = [
'auditionID' => $entry->audition_id,
'auditionName' => $this->auditionCacheService->getAudition($entry->audition_id)->name,
'rank' => $this->tabulationService->entryRank($entry),
'unscored' => $this->tabulationService->remainingEntriesForAudition($entry->audition_id)
];
$entry->audition = $this->auditionCacheService->getAudition($entry->audition_id);
}
return $info;
}
public function entryIsDoubler(Entry $entry): bool
{
// Return true if $entry->student_id is associated with a student in the collection from $this->getDoublers()
return $this->getDoublers()->contains('id', $entry->student_id);
}
}