Files
exchange-2.0/app/Classes/Modules/Segments/ControllerLogic/CreateSegmentLogic.php
T
CheeSiong 8b01421be7 -Update Non Standard Segment Logic Class
-Update Non Segment Constant Service Class
-Create Non Standard Segment Constant Logic Class
-Update Non Standard Segment Constant Logic Class
-Delete Non Standard Segment Logic Class
2020-11-17 12:08:10 +08:00

75 lines
2.0 KiB
PHP

<?php
namespace App\Classes\Modules\Segments\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Segments\Standards\Rules\CanCreateSegment;
use App\Classes\Modules\Segments\Services\CreatesSegment;
use App\Classes\Modules\Segments\DataTransferObjects\SegmentObject;
use App\Http\Resources\SegmentResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CreateSegmentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Segment',
'message' => 'You have successfully created a new Segment'
];
}
/** @var CanCreateSegment */
private $canCreateSegment;
/** @var CreatesSegment */
private $createsSegment;
/**
* CreateSegmentLogic constructor.
* @param CanCreateSegment $canCreateSegment
* @param CreatesSegment $createsSegment
*/
public function __construct(
CanCreateSegment $canCreateSegment,
CreatesSegment $createsSegment
)
{
$this->canCreateSegment = $canCreateSegment;
$this->createsSegment = $createsSegment;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
DB::beginTransaction();
$segment_object = new SegmentObject(
$request->input('name')
);
$this->canCreateSegment->passes($segment_object);
$segment_query = $this->createsSegment->execute($segment_object);
DB::commit();
return $this->resourceResponse(new SegmentResource($segment_query));
} catch (\Exception $exception) {
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}