'Updated Segment Constant', 'message' => 'You have successfully updated the Segment Constant' ]; } /** @var CanUpdateConstant */ private $canUpdateConstant; /** @var UpdatesConstant */ private $updatesConstant; /** @var FetchesSegment */ private $fetchesSegment; /** @var FetchesConstant */ private $fetchesConstant; /** @var AddItemToConstantValueArray */ private $addItemToConstantValueArray; /** @var RemoveItemFromConstantValueArray */ private $removeItemFromConstantValueArray; /** * UpdateConstantLogic constructor. * @param CanUpdateConstant $canUpdateConstant * @param UpdatesConstant $updatesConstant * @param FetchesSegment $fetchesSegment * @param FetchesConstant $fetchesConstant * @param AddItemToConstantValueArray $addItemToConstantValueArray * @param RemoveItemFromConstantValueArray $removeItemFromConstantValueArray */ public function __construct(CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant,AddItemToConstantValueArray $addItemToConstantValueArray, RemoveItemFromConstantValueArray $removeItemFromConstantValueArray) { $this->canUpdateConstant = $canUpdateConstant; $this->updatesConstant = $updatesConstant; $this->fetchesSegment = $fetchesSegment; $this->fetchesConstant = $fetchesConstant; $this->addItemToConstantValueArray = $addItemToConstantValueArray; $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; } /** * @param Request $request * @return JsonResponse * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\RequestValidationException */ public function logic(Request $request) : JsonResponse { $segment = $this->fetchesSegment->execute(['id' => $request->route('id')]); $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference')]); $object = new ConstantObject($constant->reference, $this->addItemToConstantValueArray->execute($constant->value, $request->input('postcode'))); $this->canUpdateConstant->passes($object); $constant = $this->updatesConstant->execute($constant, $object); $oppositeConstant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); $oppositeObject = new ConstantObject($request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE, $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $request->input('postcode'))); $oppositeConstant = $this->updatesConstant->execute($oppositeConstant, $oppositeObject); return $this->response([ 'CENTER_POSTCODE' => $constant->reference == SegmentConstants::CENTER_POSTCODE ? new ConstantResource($constant) : New ConstantResource($oppositeConstant), 'OUTSTATION_POSTCODE' => $constant->reference == SegmentConstants::OUTSTATION_POSTCODE ? new ConstantResource($constant) : New ConstantResource($oppositeConstant), ]); } }