ldc-cocotte/app/Actions/ProductIncos/CreateProductInco.php

47 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\ProductIncos;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Models\ProductInco;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\ProductIncos\CreateProductIncoInput, \App\Models\ProductInco>
*/
final class CreateProductInco implements Action
{
/**
* @param \App\Actions\ProductIncos\CreateProductIncoInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): ProductInco
{
Assert::isInstanceOf($input, CreateProductIncoInput::class);
$maxNoArt = ProductInco::query()->max('no_art');
$noArt = $input->no_art ?? (is_numeric($maxNoArt) ? ((int) $maxNoArt) + 1 : 1);
if ($noArt <= 0) {
$noArt = 1;
}
$inco = new ProductInco();
$inco->no_art = $noArt;
$inco->ingredients = $input->ingredients;
$inco->preparation = $input->preparation;
$inco->servings = $input->servings;
$inco->allergens = $input->allergens;
$inco->selling_argument = $input->selling_argument;
$inco->nutriscore = $input->nutriscore;
$inco->nutriscore_args = $input->nutriscore_args;
$inco->nutrition = $input->nutrition;
$inco->product()->associate($input->product);
$inco->saveOrFail();
return $inco;
}
}