122 lines
3.4 KiB
PHP
122 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Directories;
|
|
|
|
use App\Contracts\Directories\ProductDirectory;
|
|
use App\Dto\ProductWithPricingDto;
|
|
use App\Models\Product;
|
|
use Domain\Models\ValueObjects\Pagination;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
use Domain\Services\PricingService;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Collection;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
use SortDirection;
|
|
|
|
class EloquentProductDirectory implements ProductDirectory
|
|
{
|
|
public function __construct(
|
|
private readonly ProductRepository $domainProductRepository,
|
|
private readonly PricingService $pricingService,
|
|
) {
|
|
}
|
|
|
|
public function find(UuidInterface $id): ?Product
|
|
{
|
|
return Product::query()
|
|
->where('id', $id->toString())
|
|
->first();
|
|
}
|
|
|
|
public function getStarProductsWithPricing(): Collection
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Builder<Product> $query */
|
|
$query = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->where('star', true)
|
|
->published();
|
|
|
|
return $query->get()->map(fn ($product) => [
|
|
'product' => $product,
|
|
'pricing' => optional(
|
|
$this->domainProductRepository->find($product->getId()),
|
|
fn ($domainProduct) => $domainProduct ?
|
|
$this->pricingService->calculateForProduct($domainProduct) : null
|
|
),
|
|
])->filter(fn ($item) => $item['pricing'] !== null)
|
|
->map(fn ($item) => new ProductWithPricingDto(
|
|
product: $item['product'],
|
|
pricing: $item['pricing']
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $ranges
|
|
* @param array<string, mixed> $tags
|
|
* @param array<string, mixed> $brands
|
|
* @return LengthAwarePaginator<array-key,Product>
|
|
*/
|
|
public function getPaginatedProducts(
|
|
Pagination $pagination,
|
|
string $search = '',
|
|
array $ranges = [],
|
|
array $tags = [],
|
|
array $brands = [],
|
|
bool $isDiscounted = false,
|
|
): LengthAwarePaginator {
|
|
/** @var \Illuminate\Database\Eloquent\Builder<Product> $query */
|
|
$query = Product::query()
|
|
->with(['inco', 'validDiscount'])
|
|
->orderBy('order', SortDirection::Ascending)
|
|
->published();
|
|
|
|
if (! empty($search)) {
|
|
$query->whereLike('name', "%{$search}%");
|
|
}
|
|
|
|
if ($isDiscounted) {
|
|
$query->whereHas('validDiscount');
|
|
}
|
|
|
|
if (! empty($ranges)) {
|
|
$query->whereHas(
|
|
'range',
|
|
fn ($q) => $q->whereIn('id', $ranges)
|
|
);
|
|
}
|
|
|
|
if (! empty($tags)) {
|
|
$query->whereHas(
|
|
'tags',
|
|
fn ($q) => $q->whereIn('tags.id', $tags)
|
|
);
|
|
}
|
|
|
|
if (! empty($brands)) {
|
|
$query->whereIn(
|
|
'brand',
|
|
$brands
|
|
);
|
|
}
|
|
|
|
return $query->paginate(
|
|
perPage: $pagination->pageSize(),
|
|
page: $pagination->pageNumber()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int,\Domain\Enums\ProductBrand>
|
|
*/
|
|
public function getPublishedBrands(): Collection
|
|
{
|
|
// @phpstan-ignore return.type
|
|
return Product::query()
|
|
->published()
|
|
->select(['brand'])
|
|
->distinct()
|
|
->pluck('brand');
|
|
}
|
|
}
|