vendor/boldr/ingredients-bundle/src/Presenter/ViewProcessor.php line 53

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\IngredientsBundle\Presenter;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Boldr\Shop\IngredientsBundle\Entity\{ ProductIngredient, IngredientsOption };
  5. use Boldr\Shop\ShopBundle\Entity\{ Product, OrderItem };
  6. use Boldr\Shop\ShopBundle\Presenter\Product\{ ProductView, ProductViewProcessorInterface };
  7. use Boldr\Shop\ShopBundle\Presenter\Order\{ OrderItemView, OrderItemViewProcessorInterface };
  8. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  9. class ViewProcessor implements ProductViewProcessorInterface, OrderItemViewProcessorInterface
  10. {
  11. private IngredientPresenter $ingredientPresenter;
  12. private EntityManagerInterface $em;
  13. private TagAwareCacheInterface $cache;
  14. public function __construct(IngredientPresenter $ingredientPresenter, EntityManagerInterface $em, TagAwareCacheInterface $cache)
  15. {
  16. $this->ingredientPresenter = $ingredientPresenter;
  17. $this->em = $em;
  18. $this->cache = $cache;
  19. }
  20. public function processProductView(ProductView $productView, Product $product, string $locale): ProductView
  21. {
  22. $cacheKey = 'boldr_ingredients.product_ingredient_views.'. $product->getId().'.'.$locale;
  23. $ingredientViews = $this->cache->get($cacheKey, function($item) use ($product, $locale) {
  24. $item->tag('boldr_ingredients.product_ingredient_views.product.'. $product->getId());
  25. $productIngredientRepository = $this->em->getRepository(ProductIngredient::class);
  26. $productIngredients = $productIngredientRepository->createQueryBuilder('pi')
  27. ->where('pi.product = ?1 AND (pi.optional = false OR pi.defaultValue = true)')
  28. ->setParameter(1, $product)
  29. ->addOrderBy('pi.sortOrder', 'ASC')
  30. ->getQuery()
  31. ->getResult();
  32. foreach ($productIngredients as $productIngredient)
  33. {
  34. $item->tag('boldr_ingredients.product_ingredient_views.ingredient.'. $productIngredient->getIngredient()->getId());
  35. }
  36. $ingredientViews = array_map(
  37. fn($productIngredients) => $this->ingredientPresenter->createIngredientView($productIngredients->getIngredient(), $locale),
  38. $productIngredients
  39. );
  40. return $ingredientViews;
  41. });
  42. $productView->ingredients = $ingredientViews;
  43. return $productView;
  44. }
  45. public function processOrderItemView(OrderItemView $orderItemView, OrderItem $orderItem, string $locale): OrderItemView
  46. {
  47. $productVariant = $orderItem->getProductVariant();
  48. $value = null;
  49. $result = [];
  50. $optionValues = $orderItem->getOptionValues();
  51. foreach ($this->em->getRepository(IngredientsOption::class)->findAll() as $option)
  52. {
  53. if (isset($optionValues[$option->getId()]))
  54. {
  55. $value = $optionValues[$option->getId()];
  56. break;
  57. }
  58. }
  59. if ($value !== null)
  60. {
  61. $productIngredients = $this->em->getRepository(ProductIngredient::class)
  62. ->findBy(['product' => $productVariant->getProduct()], ['sortOrder' => 'ASC']);
  63. foreach ($productIngredients as $productIngredient)
  64. {
  65. if ($productIngredient->getOptional() && (!$productIngredient->getDefaultValue() && in_array($productIngredient->getIngredient()->getId(), $value)))
  66. {
  67. $ingredientView = $this->ingredientPresenter->createIngredientView($productIngredient->getIngredient(), $locale);
  68. $amount = $productIngredient->getIngredient()->getAmountByCurrency()[$orderItem->getOrder()->getCurrency()] ?? 0;
  69. $result[] = [
  70. 'with' => true,
  71. 'ingredient' => $ingredientView,
  72. 'amount' => $amount
  73. ];
  74. }
  75. }
  76. foreach ($productIngredients as $productIngredient)
  77. {
  78. if ($productIngredient->getOptional() && in_array($productIngredient->getIngredient()->getId(), $value))
  79. {
  80. $ingredientView = $this->ingredientPresenter->createIngredientView($productIngredient->getIngredient(), $locale);
  81. if ($productIngredient->getDefaultValue())
  82. {
  83. $result[] = [
  84. 'with' => false,
  85. 'ingredient' => $ingredientView,
  86. 'amount' => 0
  87. ];
  88. }
  89. }
  90. }
  91. }
  92. $orderItemView->ingredients = $result;
  93. return $orderItemView;
  94. }
  95. }