<?php

namespace App\Classes\Modules\ControllerLogic\Contacts;

use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Exceptions\CannotCreateContactException;
use App\Classes\Modules\Contacts\Services\CreatesContact;
use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use Illuminate\Http\Request;

class CreateContactLogic
{
    /** @var CreatesContact */
    private $createContact;

    /**
     * CreateContactLogic constructor.
     * @param CreatesContact $createContact
     */
    public function __construct(CreatesContact $createContact)
    {
        $this->createContact = $createContact;
    }


    public function execute(string $type, int $referenceId, Request $request)
    {
        try{

            $object = new ContactObject($type, $referenceId, $request->input('name'), $request->input('contact'), $request->input('email'), $request->input('designation'));
            return $this->createContact->execute($object, $type, $referenceId);

        }
        catch (CannotCreateContactException $exception){
            throw new InternalServerErrorException("Failed to create contact because {$exception->getMessage()}");
        }
    }

}