mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 12:34:18 +00:00
131 lines
5.0 KiB
PHP
131 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Segments\ControllersLogic;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Http\Resources\ConstantResource;
|
|
use App\Classes\ValueObjects\Constants\SegmentConstants;
|
|
use App\Classes\Modules\Segments\Services\FetchesSegment;
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\Segments\Services\FetchesConstant;
|
|
use App\Classes\Modules\Segments\Services\UpdatesConstant;
|
|
use App\Classes\Modules\Segments\Standards\Rules\CanUpdateConstant;
|
|
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
|
|
use App\Classes\Modules\Segments\Services\AddItemToConstantValueArray;
|
|
use App\Classes\Modules\Segments\Services\RemoveItemFromConstantValueArray;
|
|
use App\Classes\Exceptions\ResourceNotFoundException;
|
|
use App\Classes\Modules\Segments\Services\CreatesConstant;
|
|
|
|
class UpdateConstantPostcodeLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => '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));
|
|
}
|
|
|
|
} |