akaunting/app/Jobs/Auth/CreateInvitation.php

45 lines
1.0 KiB
PHP
Raw Normal View History

2022-06-01 10:15:55 +03:00
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Models\Auth\UserInvitation;
2022-06-28 21:45:14 +03:00
use App\Notifications\Auth\Invitation as Notification;
use Exception;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Str;
2022-06-28 21:45:14 +03:00
use Symfony\Component\Mailer\Exception\TransportException;
2022-06-01 10:15:55 +03:00
class CreateInvitation extends Job
{
protected $invitation;
protected $user;
2022-06-28 21:45:14 +03:00
public function __construct($user)
2022-06-01 10:15:55 +03:00
{
$this->user = $user;
}
public function handle(): UserInvitation
{
\DB::transaction(function () {
$this->invitation = UserInvitation::create([
'user_id' => $this->user->id,
'token' => (string) Str::uuid(),
]);
2022-06-28 21:45:14 +03:00
$notification = new Notification($this->invitation);
try {
$this->dispatch(new NotifyUser($this->user, $notification));
} catch (TransportException $e) {
$message = trans('errors.title.500');
throw new Exception($message);
}
});
2022-06-01 10:15:55 +03:00
return $this->invitation;
}
}