<?php
namespace App\ViewProcessor;
use Boldr\Shop\ShopBundle\Presenter\Product\{ ProductView, ProductViewProcessorInterface };
use Boldr\Shop\ShopBundle\Entity\Product;
use App\Entity\{ ProductFeatures, ProductOptions };
use Doctrine\ORM\EntityManagerInterface;
class ProductViewProcessor implements ProductViewProcessorInterface
{
private EntityManagerInterface $em;
private array $productOptionsById;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
private function getProductOptions(Product $product): ?ProductOptions
{
if (!isset($this->productOptionsById))
{
$this->productOptionsById = [];
$allProductOptions = $this->em->getRepository(ProductOptions::class)->findAll();
foreach ($allProductOptions as $productOptions)
{
$this->productOptionsById[$productOptions->getProduct()->getId()] = $productOptions;
}
}
return $this->productOptionsById[$product->getId()] ?? null;
}
public function processProductView(ProductView $productView, Product $product, string $locale): ProductView
{
/** @var ProductFeatures */
$productFeatures = $this->em->getRepository(ProductFeatures::class)->find($product);
$productOptions = $this->getProductOptions($product);
$productView->homemade = $productFeatures === null ? false : $productFeatures->getIsHomemade();
$productView->new = $productFeatures === null ? false : $productFeatures->getIsNew();
$productView->vegan = $productFeatures === null ? false : $productFeatures->getIsVegan();
$productView->vegetarian = $productFeatures === null ? false : $productFeatures->getIsVegetarian();
$productView->fish = $productFeatures === null ? false : $productFeatures->getIsFish();
$productView->beef = $productFeatures === null ? false : $productFeatures->getIsBeef();
$productView->receiptStyle = $productOptions?->getReceiptStyle();
return $productView;
}
}