ldc-boutique-cnc-sable/app/Actions/ProductIncos/SyncProductInco.php

54 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\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\Actions\ProductIncos\SyncProductIncoInput, \App\Models\ProductInco>
*/
final class SyncProductInco implements Action
{
public function __construct(
private readonly ArcadiaIncoService $arcadiaIncoService,
) {
}
/**
* @param \App\Actions\ProductIncos\SyncProductIncoInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): ProductInco
{
Assert::isInstanceOf($input, SyncProductIncoInput::class);
$inco = $input->product->inco;
if ($inco === null) {
throw new \RuntimeException('Product does not have an associated INCO sheet to sync.');
}
$incoData = $this->arcadiaIncoService->fetchInco($inco->no_art);
if ($incoData === null) {
throw new \RuntimeException(sprintf('INCO sheet #%d was not found on Arcadia API.', $inco->no_art));
}
$inco->ingredients = $incoData->ingredients;
$inco->preparation = $incoData->preparation;
$inco->servings = $incoData->servings;
$inco->allergens = $incoData->allergens;
$inco->selling_argument = $incoData->sellingArgument;
$inco->nutriscore = $incoData->nutriscore;
$inco->nutriscore_args = $incoData->nutriscoreArgs;
$inco->nutrition = $incoData->nutrition;
$inco->saveOrFail();
return $inco;
}
}