'Updated Postcode Constant', 'message' => 'You have successfully updated the Postcode 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; /** @var CreatesConstant */ private $createsConstant; /** * UpdateConstantLogic constructor. * @param CanUpdateConstant $canUpdateConstant * @param UpdatesConstant $updatesConstant * @param FetchesSegment $fetchesSegment * @param FetchesConstant $fetchesConstant * @param AddItemToConstantValueArray $addItemToConstantValueArray * @param CreatesConstant $createsConstant * @param RemoveItemFromConstantValueArray $removeItemFromConstantValueArray */ public function __construct( CanUpdateConstant $canUpdateConstant, UpdatesConstant $updatesConstant, FetchesSegment $fetchesSegment, FetchesConstant $fetchesConstant, AddItemToConstantValueArray $addItemToConstantValueArray, RemoveItemFromConstantValueArray $removeItemFromConstantValueArray, CreatesConstant $createsConstant ) { $this->canUpdateConstant = $canUpdateConstant; $this->updatesConstant = $updatesConstant; $this->fetchesSegment = $fetchesSegment; $this->fetchesConstant = $fetchesConstant; $this->addItemToConstantValueArray = $addItemToConstantValueArray; $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; $this->createsConstant = $createsConstant; } /** * @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')]); try { $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference')]); $constantValue = $this->addItemToConstantValueArray->execute($constant->value, $request->input('postcode')); $object = new ConstantObject($request->input('reference'), $constantValue); } catch (ResourceNotFoundException $exception){ $constantObject = [$request->input('postcode')]; $object = new ConstantObject( $request->input('reference'), $constantObject ); $constant = $this->createsConstant->execute($segment, $object); } try { // check if postcode exist in the opposite constant, and delete it $oppositeConstant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); $oppositeConstantValue = $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $request->input('postcode')); $oppositeObject = new ConstantObject($request->input('reference') == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE, $oppositeConstantValue); $this->updatesConstant->execute($oppositeConstant, $oppositeObject); } catch (ResourceNotFoundException $exception){ // if opposite constant does not exist, do nothing } $constant = $this->updatesConstant->execute($constant, $object); return $this->resourceResponse(new ConstantResource($constant)); } }