<?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\UserInvitation;

final class CreatesUserInvitation
{

    /** @var UserInvitation */
    private $repository;

    /**
     * CreatesUserInvitation constructor.
     * @param UserInvitation $repository
     */
    public function __construct(UserInvitation $repository)
    {
        $this->repository = $repository;
    }


    /**
     * @param UserInvitationObject $object
     * @return UserInvitation|null
     * @throws ResourceConflictException
     */
    public function execute(UserInvitationObject $object): ?UserInvitation {



        $this->repository->email = $object->getInviteeEmail();
        $this->repository->hash = $object->getHash();
        $this->repository->role_id = $object->getRole();
        $this->repository->sender_id = auth()->user()->getAuthIdentifier();
        $this->repository->is_complete = false;

        $this->repository->save();

        return $this->repository;

    }

}