2026-10-03 10:54:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
namespace App\Cqs\Actions\ProductIncos;
|
2026-10-03 10:54:52 +02:00
|
|
|
|
|
|
|
|
use App\Contracts\Action;
|
|
|
|
|
use App\Contracts\ActionInput;
|
|
|
|
|
use App\Models\ProductInco;
|
|
|
|
|
use App\Services\ArcadiaIncoService;
|
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-15 20:11:28 +01:00
|
|
|
* @implements \App\Contracts\Action<
|
|
|
|
|
* \App\Cqs\Actions\ProductIncos\AssociateProductIncoInput,
|
|
|
|
|
* \App\Cqs\Actions\ProductIncos\AssociateProductIncoOutput
|
|
|
|
|
* >
|
2026-10-03 10:54:52 +02:00
|
|
|
*/
|
|
|
|
|
final class AssociateProductInco implements Action
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly ArcadiaIncoService $arcadiaIncoService,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @param \App\Cqs\Actions\ProductIncos\AssociateProductIncoInput $input
|
2026-10-03 10:54:52 +02:00
|
|
|
*/
|
|
|
|
|
#[\Override]
|
2026-11-15 20:11:28 +01:00
|
|
|
public function __invoke(ActionInput $input): AssociateProductIncoOutput
|
2026-10-03 10:54:52 +02:00
|
|
|
{
|
|
|
|
|
Assert::isInstanceOf($input, AssociateProductIncoInput::class);
|
|
|
|
|
|
2026-11-15 20:11:28 +01:00
|
|
|
try {
|
|
|
|
|
$incoData = $this->arcadiaIncoService->fetchInco($input->no_art);
|
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
|
return AssociateProductIncoOutput::apiError($e->getMessage());
|
|
|
|
|
}
|
2026-10-03 10:54:52 +02:00
|
|
|
|
|
|
|
|
if ($incoData === null) {
|
2026-11-15 20:11:28 +01:00
|
|
|
return AssociateProductIncoOutput::notFoundOnArcadia($input->no_art);
|
2026-10-03 10:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-14 16:06:21 +01:00
|
|
|
$productId = $input->productId->value->toString();
|
|
|
|
|
|
2026-10-03 10:54:52 +02:00
|
|
|
// * Remove existing product_inco for this product if present
|
2026-11-14 16:06:21 +01:00
|
|
|
$existing = ProductInco::query()->where('product_id', $productId)->first();
|
2026-10-03 10:54:52 +02:00
|
|
|
if ($existing !== null) {
|
|
|
|
|
$existing->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 20:11:28 +01:00
|
|
|
$inco = new ProductInco();
|
|
|
|
|
$inco->product_id = $productId;
|
|
|
|
|
$inco->no_art = $incoData->noArt;
|
|
|
|
|
$inco->hydrateFromArcadiaDto($incoData);
|
2026-10-03 10:54:52 +02:00
|
|
|
$inco->saveOrFail();
|
|
|
|
|
|
2026-11-15 20:11:28 +01:00
|
|
|
return AssociateProductIncoOutput::success($inco);
|
2026-10-03 10:54:52 +02:00
|
|
|
}
|
|
|
|
|
}
|