40 lines
1010 B
PHP
40 lines
1010 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|int $school): void
|
|
{
|
|
$this->assign($user, $school);
|
|
}
|
|
|
|
public function assign(User $user, School|int $school, bool $addDomainToSchool = true): void
|
|
{
|
|
if (is_int($school)) {
|
|
$school = School::find($school);
|
|
}
|
|
|
|
if (! User::where('id', $user->id)->exists()) {
|
|
throw new AuditionAdminException('User does not exist');
|
|
}
|
|
|
|
if (is_null($school) || ! 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,
|
|
]);
|
|
}
|
|
}
|