mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
123 lines
4.6 KiB
PHP
123 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Segments\Processors;
|
|
|
|
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 UpdatePostcodeSegmentProcessor
|
|
{
|
|
/** @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;
|
|
/**
|
|
* UpdatePostcodeSegmentProcessor 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 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;
|
|
|
|
}
|
|
|
|
}
|