155 lines
5 KiB
PHP
155 lines
5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Infrastructure\Adapters\Repositories;
|
|
|
|
use App\Models\Product;
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
|
|
use Domain\Models\Entities\Product as DomainProduct;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\Slug;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
use Illuminate\Support\Carbon;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
class EloquentProductRepository implements ProductRepository
|
|
{
|
|
#[\Override]
|
|
public function find(ProductId $id): ?DomainProduct
|
|
{
|
|
$product = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->where('id', $id->value)
|
|
->first();
|
|
|
|
return $product ? $this->toEntity($product) : null;
|
|
}
|
|
|
|
#[\Override]
|
|
public function findByslug(Slug $slug): ?DomainProduct
|
|
{
|
|
$product = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->where('slug', $slug->value)
|
|
->first();
|
|
|
|
return $product ? $this->toEntity($product) : null;
|
|
}
|
|
|
|
#[\Override]
|
|
public function findMany(array $ids): array
|
|
{
|
|
if ($ids === []) {
|
|
return [];
|
|
}
|
|
|
|
$entities = [];
|
|
$models = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->whereIn('id', array_map(static fn (ProductId $id): string => $id->value->toString(), $ids))
|
|
->get();
|
|
|
|
foreach ($models as $model) {
|
|
$entities[(string) $model->id] = $this->toEntity($model);
|
|
}
|
|
|
|
return $entities;
|
|
}
|
|
|
|
#[\Override]
|
|
public function persist(DomainProduct $entity): void
|
|
{
|
|
$model = Product::query()
|
|
->where('id', $entity->id()->value->toString())
|
|
->firstOrNew();
|
|
|
|
$model->id = $entity->id()->value->toString();
|
|
$model->price = $entity->price;
|
|
$model->tax_rate = $entity->tax_rate;
|
|
$model->product_weight = $entity->product_weight;
|
|
$model->invoicing_unit = $entity->invoicing_unit;
|
|
$model->sell_unit = $entity->sell_unit;
|
|
$model->status = $entity->status;
|
|
$model->orderable_start = optional(
|
|
$entity->orderable_start,
|
|
fn (?CarbonImmutable $date) => $date ?
|
|
Carbon::instance($date) : null
|
|
);
|
|
$model->orderable_end = optional(
|
|
$entity->orderable_end,
|
|
fn (?CarbonImmutable $date) => $date ?
|
|
Carbon::instance($date) : null
|
|
);
|
|
|
|
$model->save();
|
|
}
|
|
|
|
#[\Override]
|
|
public function delete(DomainProduct $product): void
|
|
{
|
|
Product::query()
|
|
->where('id', $product->id()->value->toString())
|
|
->delete();
|
|
}
|
|
|
|
/**
|
|
* @throws \Domain\Exceptions\OrderTunnel\UnorderableProductException
|
|
*/
|
|
#[\Override]
|
|
public function getBasketProduct(ProductId $id, int $qty): BasketProduct
|
|
{
|
|
$product = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->where('id', $id->value)
|
|
->firstOrFail();
|
|
|
|
$domainProduct = $this->toEntity($product);
|
|
|
|
if (! $domainProduct->isOrderable()) {
|
|
throw new UnorderableProductException();
|
|
}
|
|
|
|
$pricing = $domainProduct->pricing();
|
|
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: $product->name,
|
|
qty: $qty,
|
|
unitPriceHt: $pricing->basePriceHt(),
|
|
unitDiscount: $pricing->discountAmountHt(),
|
|
taxRate: $product->tax_rate,
|
|
weightGrams: (int) $product->weight,
|
|
brand: $product->brand,
|
|
incoId: $product->inco ? $product->inco->getId() : null,
|
|
orderUnit: $product->order_unit ?? OrderUnit::PCE,
|
|
slug: $product->slug,
|
|
picture: $product->picture,
|
|
description: $product->description,
|
|
weightVariable: $product->weight_variable,
|
|
discountRate: $pricing->discountRate,
|
|
priceKgTtc: $pricing->priceKgTtc(),
|
|
options: []
|
|
);
|
|
}
|
|
|
|
public function toEntity(Product $model): DomainProduct
|
|
{
|
|
return new DomainProduct(
|
|
id: new ProductId(UuidHelper::fromString($model->id)),
|
|
price: $model->price,
|
|
tax_rate: $model->tax_rate,
|
|
product_weight: $model->product_weight ? clone $model->product_weight : null,
|
|
invoicing_unit: $model->invoicing_unit,
|
|
sell_unit: $model->sell_unit,
|
|
validDiscount: $model->validDiscount ? clone $model->validDiscount->value : null,
|
|
status: (bool) $model->status,
|
|
orderable_start: $model->orderable_start ? CarbonImmutable::instance($model->orderable_start) : null,
|
|
orderable_end: $model->orderable_end ? CarbonImmutable::instance($model->orderable_end) : null,
|
|
hasInco: $model->inco !== null
|
|
);
|
|
}
|
|
}
|