mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim.git
synced 2026-08-27 00:13:57 +00:00
55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Accounts\Services;
|
|
|
|
|
|
use App\Classes\common;
|
|
use App\Classes\Modules\Accounts\DataTransferObjects\UserInvitationObject;
|
|
use App\Classes\Modules\Accounts\Exceptions\InvitationAlreadyExistsException;
|
|
use App\Classes\Modules\ControllerLogic\Exceptions\ResourceConflictException;
|
|
use App\Models\User;
|
|
use App\Models\UserInvitation;
|
|
|
|
final class CreatesUserInvitation
|
|
{
|
|
|
|
/** @var UserInvitation */
|
|
private $repository;
|
|
|
|
/** @var User */
|
|
private $user;
|
|
|
|
/**
|
|
* @param UserInvitation $repository
|
|
* @param User $user
|
|
*/
|
|
public function __construct(UserInvitation $repository, User $user) {
|
|
$this->repository = $repository;
|
|
$this->user = $user;
|
|
}
|
|
|
|
/**
|
|
* @param UserInvitationObject $object
|
|
* @return UserInvitation|null
|
|
* @throws ResourceConflictException
|
|
*/
|
|
public function execute(UserInvitationObject $object): ?UserInvitation {
|
|
// ensure that there is not already an invitation for the email
|
|
$existingInvitations = $this->repository->where('email', $object->getInviteeEmail())->get();
|
|
if ($existingInvitations->count() !== 0) {
|
|
throw new ResourceConflictException("An existing invitation already exists for the email {$object->getInviteeEmail()}");
|
|
}
|
|
|
|
$model = $this->repository->create([
|
|
'email' => $object->getInviteeEmail(),
|
|
'hash' => Common::generateRandomHash(50),
|
|
'role_id' => $object->getRole(),
|
|
'sender_id' => 1,//$this->user->getAuthIdentifier(),
|
|
'is_complete' => false,
|
|
]);
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
} |