- Added AssociateProductIncoOutput and SyncProductIncoOutput classes to handle action results. - Updated AssociateProductInco and SyncProductInco actions to return structured output instead of raw models. - Enhanced error handling for API interactions in both actions. - Refactored ProductIncosController to utilize new output structures for better response management. - Introduced new enums for status handling in actions and queries. - Added tests for new output handling and error scenarios in Associate and Sync actions.
57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\ProductIncos;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Models\ProductInco;
|
|
use App\Services\ArcadiaIncoService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<
|
|
* \App\Cqs\Actions\ProductIncos\SyncProductIncoInput,
|
|
* \App\Cqs\Actions\ProductIncos\SyncProductIncoOutput
|
|
* >
|
|
*/
|
|
final class SyncProductInco implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly ArcadiaIncoService $arcadiaIncoService,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\ProductIncos\SyncProductIncoInput $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): SyncProductIncoOutput
|
|
{
|
|
Assert::isInstanceOf($input, SyncProductIncoInput::class);
|
|
|
|
$inco = ProductInco::query()
|
|
->where('product_id', $input->productId->value->toString())
|
|
->first();
|
|
|
|
if ($inco === null) {
|
|
return SyncProductIncoOutput::notAssociated();
|
|
}
|
|
|
|
try {
|
|
$incoData = $this->arcadiaIncoService->fetchInco($inco->no_art);
|
|
} catch (\RuntimeException $e) {
|
|
return SyncProductIncoOutput::apiError($e->getMessage());
|
|
}
|
|
|
|
if ($incoData === null) {
|
|
return SyncProductIncoOutput::notFoundOnArcadia($inco->no_art);
|
|
}
|
|
|
|
$inco->hydrateFromArcadiaDto($incoData);
|
|
$inco->saveOrFail();
|
|
|
|
return SyncProductIncoOutput::success($inco);
|
|
}
|
|
}
|