auditionadmin/app/Services/DrawService.php

64 lines
1.7 KiB
PHP

<?php
namespace App\Services;
use App\Models\Audition;
use Illuminate\Support\Facades\DB;
class DrawService
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
public function runOneDraw(Audition $audition): void
{
// set draw number null on each entry before beginning
DB::table('entries')->where('audition_id', $audition->id)->update(['draw_number' => null]);
$randomizedEntries = $audition->entries->shuffle();
// Move entries flagged as no show to the end
[$noShowEntries, $otherEntries] = $randomizedEntries->partition(function ($entry) {
return $entry->hasFlag('no_show');
});
// Merge the entries, placing the 'no_show' entries at the end
$randomizedEntries = $otherEntries->merge($noShowEntries);
foreach ($randomizedEntries as $index => $entry) {
$entry->draw_number = $index + 1;
$entry->save();
}
$audition->addFlag('drawn');
}
public function runDrawsOnCollection($auditions): void
{
$auditions->each(fn ($audition) => $this->runOneDraw($audition));
}
public function checkCollectionForDrawnAuditions($auditions): bool
{
$auditions->loadMissing('flags');
return $auditions->contains(fn ($audition) => $audition->hasFlag('drawn'));
}
public function clearDrawForAudition(Audition $audition): void
{
$audition->removeFlag('drawn');
DB::table('entries')->where('audition_id', $audition->id)->update(['draw_number' => null]);
}
public function clearDrawsOnCollection($auditions): void
{
$auditions->each(fn ($audition) => $this->clearDrawForAudition($audition));
}
}