canUpdateConstant = $canUpdateConstant; $this->updatesConstant = $updatesConstant; $this->fetchesSegment = $fetchesSegment; $this->fetchesConstant = $fetchesConstant; $this->addItemToConstantValueArray = $addItemToConstantValueArray; $this->removeItemFromConstantValueArray = $removeItemFromConstantValueArray; $this->createsConstant = $createsConstant; } /** * @param string $name * @param int $companyModuleId * @throws \App\Classes\Exceptions\AccessForbiddenException * @throws \App\Classes\Exceptions\MalformedRequestException * @throws \App\Classes\Exceptions\RequestValidationException */ public function execute($segment_id, $reference, $postcode){ $segment = $this->fetchesSegment->execute(['id' => $segment_id]); try { $constant = $this->fetchesConstant->execute(['segment_id' => $segment->id, 'reference' => $reference]); $constantValue = $this->addItemToConstantValueArray->execute($constant->value, $postcode); $object = new ConstantObject($reference, $constantValue); } catch (ResourceNotFoundException $exception){ $constantObject = [$postcode]; $object = new ConstantObject( $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' => $reference == SegmentConstants::CENTER_POSTCODE ? SegmentConstants::OUTSTATION_POSTCODE : SegmentConstants::CENTER_POSTCODE]); $oppositeConstantValue = $this->removeItemFromConstantValueArray->execute($oppositeConstant->value, $postcode); $oppositeObject = new ConstantObject($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 $constant; } }