src/ViewProcessor/ProductViewProcessor.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\ViewProcessor;
  3. use Boldr\Shop\ShopBundle\Presenter\Product\{ ProductView, ProductViewProcessorInterface };
  4. use Boldr\Shop\ShopBundle\Entity\Product;
  5. use App\Entity\{ ProductFeatures, ProductOptions };
  6. use Doctrine\ORM\EntityManagerInterface;
  7. class ProductViewProcessor implements ProductViewProcessorInterface
  8. {
  9. private EntityManagerInterface $em;
  10. private array $productOptionsById;
  11. public function __construct(EntityManagerInterface $em)
  12. {
  13. $this->em = $em;
  14. }
  15. private function getProductOptions(Product $product): ?ProductOptions
  16. {
  17. if (!isset($this->productOptionsById))
  18. {
  19. $this->productOptionsById = [];
  20. $allProductOptions = $this->em->getRepository(ProductOptions::class)->findAll();
  21. foreach ($allProductOptions as $productOptions)
  22. {
  23. $this->productOptionsById[$productOptions->getProduct()->getId()] = $productOptions;
  24. }
  25. }
  26. return $this->productOptionsById[$product->getId()] ?? null;
  27. }
  28. public function processProductView(ProductView $productView, Product $product, string $locale): ProductView
  29. {
  30. /** @var ProductFeatures */
  31. $productFeatures = $this->em->getRepository(ProductFeatures::class)->find($product);
  32. $productOptions = $this->getProductOptions($product);
  33. $productView->homemade = $productFeatures === null ? false : $productFeatures->getIsHomemade();
  34. $productView->new = $productFeatures === null ? false : $productFeatures->getIsNew();
  35. $productView->vegan = $productFeatures === null ? false : $productFeatures->getIsVegan();
  36. $productView->vegetarian = $productFeatures === null ? false : $productFeatures->getIsVegetarian();
  37. $productView->fish = $productFeatures === null ? false : $productFeatures->getIsFish();
  38. $productView->beef = $productFeatures === null ? false : $productFeatures->getIsBeef();
  39. $productView->receiptStyle = $productOptions?->getReceiptStyle();
  40. return $productView;
  41. }
  42. }