66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\ProductIncos;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\ActionInputEmpty;
|
|
use App\Models\ProductInco;
|
|
use App\Services\ArcadiaIncoService;
|
|
use Psr\Log\LoggerInterface;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Contracts\ActionInputEmpty, int>
|
|
*/
|
|
final class SynchronizeProductIncosWithArcadiaApi implements Action
|
|
{
|
|
public function __construct(
|
|
private readonly ArcadiaIncoService $arcadiaIncoService,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Contracts\ActionInputEmpty $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): int
|
|
{
|
|
Assert::isInstanceOf($input, ActionInputEmpty::class);
|
|
|
|
$this->logger->info('Starting synchronization of all ProductIncos with Arcadia API.');
|
|
$updatedCount = 0;
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection<int, ProductInco> $incos */
|
|
$incos = ProductInco::query()->get();
|
|
|
|
foreach ($incos as $inco) {
|
|
$incoData = $this->arcadiaIncoService->fetchInco($inco->no_art);
|
|
|
|
if ($incoData === null) {
|
|
$this->logger->warning("Could not sync ProductInco #{$inco->id} (no_art: {$inco->no_art}): not found on Arcadia.");
|
|
|
|
continue;
|
|
}
|
|
|
|
$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();
|
|
$updatedCount++;
|
|
}
|
|
|
|
$this->logger->info("Completed synchronization of ProductIncos: {$updatedCount} updated.");
|
|
|
|
return $updatedCount;
|
|
}
|
|
}
|