Files
kupshop/bundles/KupShop/ComponentsBundle/Dto/CollectionMultiFetchProxy.php
2025-08-02 16:30:27 +02:00

71 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\ComponentsBundle\Dto;
use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\CatalogBundle\Util\Product\ProductDiscountCalculator;
use KupShop\CatalogBundle\Util\ProductAvailabilityUtil;
/**
* mixin ProductCollection.
*/
class CollectionMultiFetchProxy
{
private array $wasCalled = [];
public function __construct(
private readonly ProductCollection $productCollection,
private readonly ProductAvailabilityUtil $productAvailability,
private readonly ProductDiscountCalculator $productDiscountCalculator,
) {
}
public function __call(string $name, array $arguments): void
{
if (!str_starts_with($name, 'fetch')) {
throw new \BadMethodCallException('Nope :)');
}
if (array_key_exists($name, $this->wasCalled)) {
return;
}
$this->productCollection->{$name}(...$arguments);
$this->wasCalled[$name] = true;
}
public function prefetchPriceTypes(): void
{
if (array_key_exists(__FUNCTION__, $this->wasCalled)) {
return;
}
$this->productDiscountCalculator->prefetchPriceTypes($this->productCollection);
$this->wasCalled[__FUNCTION__] = true;
}
public function fetchShowMaxInStore(): void
{
if (array_key_exists(__FUNCTION__, $this->wasCalled)) {
return;
}
$this->productAvailability->fetchShowMaxInStore($this->productCollection);
$this->wasCalled[__FUNCTION__] = true;
}
/**
* @internal
*
* @private
*/
public function clear(): void
{
$this->wasCalled = [];
}
}