akaunting/app/Jobs/Auth/DeleteUser.php
2022-06-01 10:15:55 +03:00

42 lines
843 B
PHP

<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\UserDeleted;
use App\Events\Auth\UserDeleting;
use App\Interfaces\Job\ShouldDelete;
class DeleteUser extends Job implements ShouldDelete
{
public function handle(): bool
{
$this->authorize();
event(new UserDeleting($this->model));
\DB::transaction(function () {
$this->model->delete();
$this->model->flushCache();
});
event(new UserDeleted($this->model));
return true;
}
/**
* Determine if this action is applicable.
*/
public function authorize(): void
{
// Can't delete yourself
if ($this->model->id == user()->id) {
$message = trans('auth.error.self_delete');
throw new \Exception($message);
}
}
}