102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Entry;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class EntryService
|
|
{
|
|
protected $auditionCache;
|
|
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct(AuditionService $auditionCache)
|
|
{
|
|
$this->auditionCache = $auditionCache;
|
|
}
|
|
|
|
/**
|
|
* Return a collection of all entries for the provided auditionId along with the
|
|
* student.school for each entry.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public function getEntriesForAudition($auditionId, $mode = 'seating')
|
|
{
|
|
// TODO this invokes a lot of lazy loading. Perhaps cache the data for all entries then draw from that for each audition
|
|
$cacheKey = 'audition'.$auditionId.'entries';
|
|
|
|
$entries = Cache::remember($cacheKey, 3600, function () use ($auditionId) {
|
|
return Entry::where('audition_id', $auditionId)
|
|
->with('student.school')
|
|
->get()
|
|
->keyBy('id');
|
|
});
|
|
|
|
switch ($mode) {
|
|
case 'seating':
|
|
return $entries->filter(function ($entry) {
|
|
return $entry->for_seating;
|
|
});
|
|
case 'advancement':
|
|
return $entries->filter(function ($entry) {
|
|
return $entry->for_advancement;
|
|
});
|
|
default:
|
|
return $entries;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a collection of collections of entries, one collection for each audition.
|
|
* The outer collection is keyed by the audition ID. The included entries are
|
|
* with their student.school.
|
|
*/
|
|
public function getAllEntriesByAudition(): Collection
|
|
{
|
|
$auditions = $this->auditionCache->getAuditions();
|
|
$allEntries = [];
|
|
foreach ($auditions as $audition) {
|
|
$allEntries[$audition->id] = $this->getEntriesForAudition($audition->id);
|
|
}
|
|
|
|
return collect($allEntries);
|
|
}
|
|
|
|
public function getAllEntries()
|
|
{
|
|
$cacheKey = 'allEntries';
|
|
|
|
return Cache::remember($cacheKey, 5, function () {
|
|
return Entry::all();
|
|
});
|
|
}
|
|
|
|
public function clearEntryCacheForAudition($auditionId): void
|
|
{
|
|
$cacheKey = 'audition'.$auditionId.'entries';
|
|
Cache::forget($cacheKey);
|
|
Cache::forget('allEntries');
|
|
}
|
|
|
|
public function clearEntryCaches(): void
|
|
{
|
|
$auditions = $this->auditionCache->getAuditions();
|
|
foreach ($auditions as $audition) {
|
|
$this->clearEntryCacheForAudition($audition->id);
|
|
}
|
|
}
|
|
|
|
public function entryIsLate(Entry $entry): bool
|
|
{
|
|
if ($entry->hasFlag('wave_late_fee')) {
|
|
return false;
|
|
}
|
|
|
|
return $entry->created_at > $entry->audition->entry_deadline;
|
|
}
|
|
}
|