create new contact recode and clean up

This commit is contained in:
hazim
2019-03-05 15:23:11 +08:00
parent 4848e1bd5b
commit c46b8570f1
6 changed files with 225 additions and 1 deletions
@@ -0,0 +1,98 @@
<?php
namespace App\Modules\Contacts\DataTransferObjects;
class ContactObject
{
/** @var int */
private $type;
/** @var int */
private $referenceId;
/** @var string */
private $name;
/** @var string */
private $designation;
/** @var string */
private $contact;
/** @var string */
private $email;
/**
* ContactObject constructor.
* @param int $type
* @param int $referenceId
* @param string $name
* @param string $designation
* @param string $contact
* @param string $email
*/
public function __construct(int $type, int $referenceId, string $name, string $designation, string $contact, string $email)
{
$this->type = $type;
$this->referenceId = $referenceId;
$this->name = $name;
$this->designation = $designation;
$this->contact = $contact;
$this->email = $email;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @return int
*/
public function getReferenceId(): int
{
return $this->referenceId;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string
*/
public function getDesignation(): string
{
return $this->designation;
}
/**
* @return string
*/
public function getContact(): string
{
return $this->contact;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Classes\Modules\Contacts\Exceptions;
use App\Classes\Exceptions\Common\ErrorException;
use App\Classes\ValueObjects\Constants\HttpStatus;
final class CannotCreateContactException extends ErrorException
{
/**
* @param string $message
*/
public function __construct(string $message) {
parent::__construct($message, HttpStatus::BAD_REQUEST);
}
}
@@ -0,0 +1,48 @@
<?php
namespace App\Modules\Contacts\Services;
use App\Classes\Modules\Contacts\Exceptions\CannotCreateContactException;
use App\Models\Contact;
use App\Modules\Contacts\DataTransferObjects\ContactObject;
class CreatesContact
{
/** @var Contact */
private $repository;
/**
* CreatesContact constructor.
* @param Contact $repository
*/
public function __construct(Contact $repository)
{
$this->repository = $repository;
}
public function execute(ContactObject $object){
try {
$this->repository->type = $object->getType();
$this->repository->reference_id = $object->getReferenceId();
$this->repository->name = $object->getName();
$this->repository->designation = $object->getDesignation();
$this->repository->contact = $object->getContact();
$this->repository->email = $object->getEmail();
$this->repository->save();
return $this->repository;
} catch (\Exception $exception){
throw new CannotCreateContactException($exception->getMessage());
}
}
}
@@ -0,0 +1,42 @@
<?php
namespace App\Modules\ControllerLogic\Contacts;
use App\Classes\Constants;
use App\Classes\Modules\Contacts\Exceptions\CannotCreateContactException;
use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Modules\Contacts\Services\CreatesContact;
use Illuminate\Http\Request;
class CreateContactLogic
{
/** @var CreatesContact */
private $createsContact;
/**
* CreateContactLogic constructor.
* @param CreatesContact $createsContact
*/
public function __construct(CreatesContact $createsContact)
{
$this->createsContact = $createsContact;
}
public function execute(int $type, int $referenceId, Request $request){
try {
$object = new ContactObject($type, $referenceId, $request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email'));
return $this->createsContact->execute($object);
} catch (CannotCreateContactException $exception){
throw new InternalServerErrorException("Failed to create contact because {$exception->getMessage()}");
}
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Http\Controllers\Contacts;
use App\Classes\Constants;
use App\Modules\ControllerLogic\Contacts\CreateContactLogic;
use Illuminate\Http\Request;
class CreateContactController
{
public function create(string $type, int $referenceId, Request $request, CreateContactLogic $logic){
return $logic->execute(Constants::MODULE_TYPES[$type], $referenceId, $request);
}
}
+3 -1
View File
@@ -70,7 +70,9 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function
Route::get('list/{type}/{reference}', 'ContactController@index')->name('list');
// Create new contact
Route::post('{type}/{reference}', 'ContactController@store')->name('create');
Route::post('{type}/{reference}', 'CreateContactController@create')->name('create');
Route::post('update/{type}/reference/{id}', 'CreateContactController@update')->name('update');
// Update contact
Route::put('{type}/{reference}/{id}', 'ContactController@store')->name('update');