<?php

namespace App\Classes\Modules\Accounts\Validation;


use App\Classes\Modules\Accounts\DataTransferObjects\UserInvitationObject;
use App\Classes\Modules\ControllerLogic\Exceptions\ResourceConflictException;
use App\Models\UserInvitation;

class InvitationAlreadyExists
{

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

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

    /**
     * @param UserInvitationObject $object
     * @return bool
     * @throws ResourceConflictException
     */
    public function execute(UserInvitationObject $object){

        // ensure that there is not already an invitation for the email
        $existingInvitations = $this->repository->where('email', $object->getInviteeEmail())->first();

        if ($existingInvitations) {
            throw new ResourceConflictException("An existing invitation already exists for the email {$object->getInviteeEmail()}");
        }


        return true;
    }

}