110 lines
3.6 KiB
PHP
110 lines
3.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Actions\Products;
|
||
|
|
|
||
|
|
use App\Contracts\Action;
|
||
|
|
use App\Contracts\ActionInput;
|
||
|
|
use App\Models\Product;
|
||
|
|
use App\Models\Tag;
|
||
|
|
use Illuminate\Support\Facades\Date;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Webmozart\Assert\Assert;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @implements \App\Contracts\Action<\App\Actions\Products\CreateProductInput, \App\Models\Product>
|
||
|
|
*/
|
||
|
|
final class CreateProduct implements Action
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @param \App\Actions\Products\CreateProductInput $input
|
||
|
|
*/
|
||
|
|
#[\Override]
|
||
|
|
public function __invoke(ActionInput $input): Product
|
||
|
|
{
|
||
|
|
Assert::isInstanceOf($input, CreateProductInput::class);
|
||
|
|
|
||
|
|
$product = new Product();
|
||
|
|
$product->name = $input->name;
|
||
|
|
$product->short_description = $input->shortDescription;
|
||
|
|
$product->description = $input->description;
|
||
|
|
$product->price = $input->price;
|
||
|
|
$product->invoicing_unit = $input->invoicingUnit;
|
||
|
|
$product->stock = $input->stock;
|
||
|
|
$product->range_id = $input->rangeId;
|
||
|
|
$product->sell_unit = $input->sellUnit;
|
||
|
|
$product->order_unit = $input->orderUnit;
|
||
|
|
$product->tax_rate = $input->taxRate;
|
||
|
|
$product->brand = $input->brand;
|
||
|
|
$product->weight = $input->weight;
|
||
|
|
$product->weight_variable = $input->weightVariable;
|
||
|
|
$product->orderable_start = $input->orderableStart !== null ? Date::instance($input->orderableStart) : null;
|
||
|
|
$product->orderable_end = $input->orderableEnd !== null ? Date::instance($input->orderableEnd) : null;
|
||
|
|
$product->star = $input->star;
|
||
|
|
$product->status = $input->status;
|
||
|
|
|
||
|
|
if ($input->picture !== null) {
|
||
|
|
$product->setAttribute('picture', $input->picture);
|
||
|
|
}
|
||
|
|
$product->picture_alt = (string) $input->pictureAlt;
|
||
|
|
$product->picture_title = (string) $input->pictureTitle;
|
||
|
|
|
||
|
|
if ($input->pictureLarge !== null) {
|
||
|
|
$product->setAttribute('picture_large', $input->pictureLarge);
|
||
|
|
}
|
||
|
|
$product->picture_large_alt = (string) $input->pictureLargeAlt;
|
||
|
|
$product->picture_large_title = (string) $input->pictureLargeTitle;
|
||
|
|
|
||
|
|
$product->saveOrFail();
|
||
|
|
|
||
|
|
$this->syncTags($input->tags, $product);
|
||
|
|
$this->syncAssociatedProducts($input->associatedProductsIds, $product);
|
||
|
|
|
||
|
|
return $product;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<array{id?: int|string, label: string}> $tags
|
||
|
|
*/
|
||
|
|
private function syncTags(array $tags, Product $product): void
|
||
|
|
{
|
||
|
|
$tagIdList = [];
|
||
|
|
foreach ($tags as $tag) {
|
||
|
|
$tagIdList[] = empty($tag['id'])
|
||
|
|
? $this->storeTag($tag)
|
||
|
|
: (int) $tag['id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$product->tags()->sync($tagIdList);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array{id?: int|string, label: string} $tag
|
||
|
|
*/
|
||
|
|
private function storeTag(array $tag): int
|
||
|
|
{
|
||
|
|
$slug = Str::slug($tag['label']);
|
||
|
|
$tagModel = Tag::query()
|
||
|
|
->where('slug', $slug)
|
||
|
|
->firstOrNew();
|
||
|
|
$tagModel->setAttribute('label', $tag['label']);
|
||
|
|
$tagModel->saveOrFail();
|
||
|
|
|
||
|
|
return (int) $tagModel->id;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param list<string> $productIdList
|
||
|
|
*/
|
||
|
|
private function syncAssociatedProducts(array $productIdList, Product $product): void
|
||
|
|
{
|
||
|
|
$syncData = [];
|
||
|
|
foreach ($productIdList as $index => $id) {
|
||
|
|
$syncData[$id] = ['sort_order' => $index + 1];
|
||
|
|
}
|
||
|
|
|
||
|
|
$product->products()->sync($syncData);
|
||
|
|
}
|
||
|
|
}
|