Files
exchange-2.0/app/Classes/Modules/SegmentConstants/ControllerLogic/CreateSegmentConstantLogic.php
T
2020-12-08 16:21:53 +08:00

105 lines
3.4 KiB
PHP

<?php
namespace App\Classes\Modules\SegmentConstants\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\SegmentConstants\Services\FetchesSegmentConstant;
use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Classes\Modules\SegmentConstants\Standards\Rules\CanCreateSegmentConstant;
use App\Classes\Modules\SegmentConstants\Services\CreatesSegmentConstant;
use App\Classes\Modules\SegmentConstants\DataTransferObjects\SegmentConstantObject;
use App\Http\Resources\SegmentConstantResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CreateSegmentConstantLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Created Segment Constant',
'message' => 'You have successfully created a new Segment Constant'
];
}
/** @var CanCreateSegmentConstant */
private $canCreateSegmentConstant;
/** @var CreatesSegmentConstant */
private $createsSegmentConstant;
/** @var FetchesSegmentConstant */
private $fetchesSegmentConstant;
/** @var FetchesSegment */
private $fetchesSegment;
/**
* CreateSegmentConstantLogic constructor.
* @param CanCreateSegmentConstant $canCreateSegmentConstant
* @param CreatesSegmentConstant $createsSegmentConstant
* @param FetchesSegmentConstant $fetchesSegmentConstant
* @param FetchesSegment $fetchesSegment
*/
public function __construct(
CanCreateSegmentConstant $canCreateSegmentConstant,
CreatesSegmentConstant $createsSegmentConstant,
FetchesSegmentConstant $fetchesSegmentConstant,
FetchesSegment $fetchesSegment
)
{
$this->canCreateSegmentConstant = $canCreateSegmentConstant;
$this->createsSegmentConstant = $createsSegmentConstant;
$this->fetchesSegmentConstant = $fetchesSegmentConstant;
$this->fetchesSegment = $fetchesSegment;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
try {
DB::beginTransaction();
$segment_query = $this->fetchesSegment->execute([
'id' => $request->input('segment_id'),
]);
$standard_segment_constant_query = $this->fetchesSegmentConstant->execute([
'id' => $request->input('standard_segment_constant_id'),
]);
$segment_constant_object = new SegmentConstantObject(
$request->input('segment_id'),
$standard_segment_constant_query->name,
$standard_segment_constant_query->reference,
$standard_segment_constant_query->detail->type,
$request->input('detail')
);
$this->canCreateSegmentConstant->passes($segment_constant_object);
$standard_segment_constant_query = $this->createsSegmentConstant->execute($segment_query, $segment_constant_object);
DB::commit();
return $this->resourceResponse(new SegmentConstantResource($standard_segment_constant_query));
} catch (\Exception $exception) {
throw new ErrorException($exception->getMessage(), $exception->getCode());
}
}
}