src/Controller/ProductController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Dto\CartItem;
  4. use App\Dto\KimAddress;
  5. use App\Dto\Product as ProductDto;
  6. use App\Dto\ProductAddon;
  7. use App\Entity\Product;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class ProductController extends AbstractController {
  14. use ServiceTrait;
  15. /**
  16. * @Route(
  17. * "/produkt/{productId}",
  18. * name="product_details",
  19. * requirements={"productId"="\d+"}
  20. * )
  21. */
  22. public function productsAction(int $productId): Response {
  23. $product = $this->entityManager->getRepository(Product::class)->find($productId);
  24. return $this->render('page/product/details.html.twig', [
  25. 'product' => $product,
  26. ]);
  27. }
  28. /**
  29. * @Route(
  30. * "/cart/add",
  31. * name="add_to_cart",
  32. * methods="POST"
  33. * )
  34. */
  35. public function addProductToCartAction(Request $request): JsonResponse {
  36. /** @var string */
  37. $content = $request->getContent();
  38. $productDto = $this->transformProduct($content);
  39. /** @var CartItem */
  40. $cartItem = $this->productService->createCartItem($productDto);
  41. $result = $this->shoppingCartService->addItem($cartItem);
  42. if ($result) {
  43. return new JsonResponse([
  44. 'success' => true,
  45. 'message' => 'Produkt wurde erfolgreich zum Warenkorb hinzugefügt.',
  46. ]);
  47. }
  48. return new JsonResponse([
  49. 'success' => $result,
  50. 'message' => 'Produkt konnte nicht zum Warenkorb hinzugefügt werden.',
  51. ]);
  52. }
  53. /**
  54. * @Route(
  55. * "/cart/remove/{productId}",
  56. * name="remove_from_cart",
  57. * requirements={
  58. * "productId"="\d+"
  59. * }
  60. * )
  61. */
  62. public function removeProductFromCartAction(int $productId): JsonResponse {
  63. /** @var Product */
  64. $product = $this->productService->findProduct($productId);
  65. /** @var CartItem */
  66. $cartItem = $this->productService->convertToCartItem($product);
  67. $result = $this->shoppingCartService->removeItem($cartItem);
  68. if ($result) {
  69. return new JsonResponse([
  70. 'success' => true,
  71. 'message' => 'Produkt wurde erfolgreich aus dem Warenkorb entfernt.',
  72. ]);
  73. }
  74. return new JsonResponse([
  75. 'success' => $result,
  76. 'message' => 'Produkt konnte nicht aus dem Warenkorb entfernt werden.',
  77. ]);
  78. }
  79. /**
  80. * @Route(
  81. * "/cart/increase/{productId}",
  82. * name="increase_quantity",
  83. * requirements={
  84. * "productId"="\d+"
  85. * }
  86. * )
  87. */
  88. public function increaseQuantityAction(int $productId): JsonResponse {
  89. /** @var Product */
  90. $product = $this->productService->findProduct($productId);
  91. /** @var CartItem */
  92. $cartItem = $this->productService->convertToCartItem($product);
  93. $result = $this->shoppingCartService->increaseQuantity($cartItem);
  94. if ($result) {
  95. return new JsonResponse([
  96. 'success' => true,
  97. 'message' => 'Die Produktmenge wurde erfolgreich erhöht.',
  98. ]);
  99. }
  100. return new JsonResponse([
  101. 'success' => $result,
  102. 'message' => 'Die Produktmenge konnte nicht erhöht werden.',
  103. ]);
  104. }
  105. /**
  106. * @Route(
  107. * "/cart/decrease/{productId}",
  108. * name="decrease_quantity",
  109. * requirements={
  110. * "productId"="\d+"
  111. * }
  112. * )
  113. */
  114. public function decreaseQuantityAction(int $productId): JsonResponse {
  115. /** @var Product */
  116. $product = $this->productService->findProduct($productId);
  117. /** @var CartItem */
  118. $cartItem = $this->productService->convertToCartItem($product);
  119. $result = $this->shoppingCartService->decreaseQuantity($cartItem);
  120. if ($result) {
  121. return new JsonResponse([
  122. 'success' => true,
  123. 'message' => 'Die Produktmenge wurde erfolgreich verringert.',
  124. ]);
  125. }
  126. return new JsonResponse([
  127. 'success' => $result,
  128. 'message' => 'Die Produktmenge konnte nicht verringert werden.',
  129. ]);
  130. }
  131. /**
  132. * Transforms a JSON string into a ProductDto object.
  133. *
  134. * @param string $product
  135. *
  136. * @return ProductDto
  137. */
  138. private function transformProduct(string $product): ProductDto {
  139. $product = json_decode($product);
  140. $productDto = new ProductDto(
  141. id: $product->id,
  142. quantity: $product->quantity
  143. );
  144. foreach ($product->kim_addresses as $kimAddress) {
  145. $kimAddress = new KimAddress(
  146. address: $kimAddress->address,
  147. domain: $kimAddress->domain,
  148. isIncluded: $kimAddress->included,
  149. price: $kimAddress->price,
  150. );
  151. $productDto->addKimAddress($kimAddress);
  152. }
  153. foreach ($product->product_addons as $productAddon) {
  154. $productAddon = new ProductAddon(
  155. itemNumber: $productAddon->item_number,
  156. quantity: $productAddon->quantity,
  157. price: $productAddon->price,
  158. );
  159. $productDto->addProductAddon($productAddon);
  160. }
  161. return $productDto;
  162. }
  163. }