auditionadmin/app/Actions/Schools/AssignUserToSchool.php

36 lines
895 B
PHP

<?php
namespace App\Actions\Schools;
use App\Exceptions\AuditionAdminException;
use App\Models\School;
use App\Models\User;
class AssignUserToSchool
{
public function __invoke(User $user, School $school): void
{
$this->assign($user, $school);
}
public function assign(User $user, School $school, bool $addDomainToSchool = true): void
{
if (! User::where('id', $user->id)->exists()) {
throw new AuditionAdminException('User does not exist');
}
if (! School::where('id', $school->id)->exists()) {
throw new AuditionAdminException('School does not exist');
}
$domainRecorder = app(AddSchoolEmailDomain::class);
if ($addDomainToSchool) {
$domainRecorder($school, $user->emailDomain());
}
$user->update([
'school_id' => $school->id,
]);
}
}