Files
portal/app/Classes/Modules/Contacts/ControllerLogic/CreateContactControllerLogic.php
T
2020-08-12 04:16:41 +08:00

70 lines
2.0 KiB
PHP

<?php
namespace App\Classes\Modules\Contacts\ControllerLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Contacts\Services\CreatesContact;
use App\Classes\Modules\Contacts\Standards\Rules\CanCreateContact;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Http\Resources\ContactResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class CreateContactControllerLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Contact',
'message' => 'You have successfully created a new Contact'
];
}
/** @var CanCreateContact */
private $canCreateContact;
/** @var CreatesContact */
private $createsContact;
/**
* CreateContactControllerLogic constructor.
* @param CanCreateContact $canCreateContact
* @param CreatesContact $createsContact
*/
public function __construct(CanCreateContact $canCreateContact, CreatesContact $createsContact)
{
$this->canCreateContact = $canCreateContact;
$this->createsContact = $createsContact;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
dd($request->all());
try {
$object = new ContactObject($request->input('company_id'), $request->input('name'), $request->input('designation'), $request->input('email'), $request->input('phone'), $request->input('wechat_id'));
$this->canCreateContact->passes($object);
$query = $this->createsContact->execute($object);
return $this->resourceResponse(new ContactResource($query));
} catch (\Exception $exception){
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}