41 lines
1015 B
PHP
41 lines
1015 B
PHP
<?php
|
|
|
|
namespace App\Actions\Schools;
|
|
|
|
use App\Exceptions\AuditionAdminException;
|
|
use App\Models\School;
|
|
use App\Models\User;
|
|
|
|
use function auditionLog;
|
|
use function is_null;
|
|
|
|
class SetHeadDirector
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function __invoke(User $user, School $school): void
|
|
{
|
|
$this->setHeadDirector($user, $school);
|
|
}
|
|
|
|
/**
|
|
* @throws AuditionAdminException
|
|
*/
|
|
public function setHeadDirector(User $user): void
|
|
{
|
|
if (is_null($user->school_id)) {
|
|
throw new AuditionAdminException('User is not associated with a school');
|
|
}
|
|
foreach ($user->school->directors as $director) {
|
|
$director->removeFlag('head_director');
|
|
}
|
|
$user->addFlag('head_director');
|
|
|
|
$logMessage = 'Set '.$user->full_name().' as head director at '.$user->school->name;
|
|
$logAffected = ['users' => [$user->id], 'schools' => [$user->school_id]];
|
|
auditionLog($logMessage, $logAffected);
|
|
}
|
|
}
|