perf(products): avoid reloading already hydrated products in ProductDtoAssemblerService
This commit is contained in:
parent
bb23dae81e
commit
48e9c9d38e
2 changed files with 34 additions and 14 deletions
|
|
@ -9,23 +9,21 @@ use App\Dto\ProductPricingDto;
|
|||
use App\Dto\ProductWithPricingDto;
|
||||
use App\Models\Product;
|
||||
use Domain\Models\Entities\Product as DomainProduct;
|
||||
use Domain\Models\ValueObjects\Entities\ProductId;
|
||||
use Domain\Ports\Repositories\ProductRepository;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection as BaseCollection;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* Point unique d'assemblage des DTOs produit : charge les entités domaine en une
|
||||
* seule requête (pas de N+1) et en dérive la commandabilité et le pricing, afin
|
||||
* que les DTOs restent purs (aucun accès au conteneur ni au repository).
|
||||
* Point unique d'assemblage des DTOs produit : précharge en lot les relations
|
||||
* manquantes sans recharger les produits déjà hydratés et en dérive la commandabilité
|
||||
* et le pricing, afin que les DTOs restent purs (aucun accès au conteneur ni au repository).
|
||||
*/
|
||||
final readonly class ProductDtoAssemblerService
|
||||
{
|
||||
public function __construct(
|
||||
private ProductRepository $productRepository,
|
||||
private EloquentProductRepository $productRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -122,14 +120,14 @@ final readonly class ProductDtoAssemblerService
|
|||
$models[] = $product;
|
||||
}
|
||||
|
||||
(new Collection($models))->loadMissing('range');
|
||||
(new Collection($models))->loadMissing(['range', 'inco', 'validDiscount']);
|
||||
|
||||
$ids = array_map(
|
||||
static fn (Product $m): ProductId => new ProductId(Uuid::fromString((string) $m->id)),
|
||||
$models
|
||||
);
|
||||
$entities = [];
|
||||
foreach ($models as $model) {
|
||||
$entities[(string) $model->id] = $this->productRepository->toEntity($model);
|
||||
}
|
||||
|
||||
return [$models, $this->productRepository->findMany($ids)];
|
||||
return [$models, $entities];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -69,7 +69,29 @@ describe('ProductDtoAssemblerService', function (): void {
|
|||
|
||||
// THEN
|
||||
expect($dtos)->toHaveCount(5)
|
||||
->and($queryCount)->toBeLessThanOrEqual(3); // range (lot) + findMany + relations du lot
|
||||
->and($queryCount)->toBeLessThanOrEqual(3); // range + inco + validDiscount (0 requêtes sur products)
|
||||
});
|
||||
|
||||
it('executes zero queries when relations are already eager-loaded on products', function (): void {
|
||||
// GIVEN
|
||||
$products = collect(range(1, 5))
|
||||
->map(fn (int $i): Product => assemblerTestProduct("Produit Préchargé {$i}"));
|
||||
$eagerLoaded = Product::query()
|
||||
->with(['range', 'inco', 'validDiscount'])
|
||||
->whereIn('id', $products->pluck('id'))
|
||||
->get();
|
||||
|
||||
$assembler = new ProductDtoAssemblerService(new EloquentProductRepository());
|
||||
|
||||
// WHEN
|
||||
DB::enableQueryLog();
|
||||
$dtos = $assembler->toDtosWithPricing($eagerLoaded);
|
||||
$queryCount = count(DB::getQueryLog());
|
||||
DB::disableQueryLog();
|
||||
|
||||
// THEN
|
||||
expect($dtos)->toHaveCount(5)
|
||||
->and($queryCount)->toBe(0);
|
||||
});
|
||||
|
||||
it('preserves input order and flags a product without INCO as not orderable', function (): void {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue