<?php
namespace Boldr\Shop\AllergenBundle\Presenter;
use Boldr\Shop\ShopBundle\Entity\ProductVariant;
use Boldr\Shop\AllergenBundle\Entity\ProductAllergens;
use Boldr\Shop\ShopBundle\Presenter\Product\{ ProductVariantView, ProductVariantViewProcessorInterface };
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
class ViewProcessor implements ProductVariantViewProcessorInterface
{
private EntityManagerInterface $em;
private TagAwareCacheInterface $cache;
public function __construct(EntityManagerInterface $em, TagAwareCacheInterface $cache)
{
$this->em = $em;
$this->cache = $cache;
}
public function processProductVariantView(ProductVariantView $productVariantView, ProductVariant $productVariant, string $locale): ProductVariantView
{
$product = $productVariant->getProduct();
$productVariantView->allergens = $this->cache->get('boldr_allergens.product_allergens.'. $product->getId(), function($item) use ($product) {
$item->tag('boldr_shop.product.'. $product->getId());
$productAllergensRepository = $this->em->getRepository(ProductAllergens::class);
$productAllergens = $productAllergensRepository->find($product);
if ($productAllergens === null)
{
$productAllergens = new ProductAllergens($product);
}
return new ProductAllergensView(
$productAllergens->getGluten(),
$productAllergens->getEgg(),
$productAllergens->getFish(),
$productAllergens->getPeanut(),
$productAllergens->getNuts(),
$productAllergens->getSoy(),
$productAllergens->getMilk(),
$productAllergens->getShellfish(),
$productAllergens->getMollusks(),
$productAllergens->getCelery(),
$productAllergens->getMustard(),
$productAllergens->getSesame(),
$productAllergens->getSulfites(),
$productAllergens->getLupine()
);
});
return $productVariantView;
}
}