src/UI/WebBundle/Controller/Product/PublicProductController.php line 52

Open in your IDE?
  1. <?php
  2. namespace Whater\UI\WebBundle\Controller\Product;
  3. use Whater\UI\WebBundle\Controller\AbstractBusController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Whater\Application\UseCase\Product\CommandRequest\GetProductForShowCommand;
  8. use Whater\Domain\Product\Model\Product;
  9. use Whater\Domain\Whater\Model\WhaterOrganization;
  10. use JMS\Serializer\SerializationContext;
  11. use Whater\Application\UseCase\Product\CommandRequest\ListPublicProductAttributeCommand;
  12. use Whater\Application\UseCase\Product\CommandRequest\ListPublicProductCategoryCommand;
  13. /**
  14. * @Route("public/product")
  15. */
  16. class PublicProductController extends AbstractBusController
  17. {
  18. /**
  19. * @Route("/list", name="web_public_product_list", defaults={"_format" = "html"})
  20. */
  21. public function productListAction(Request $request)
  22. {
  23. $productCategories = [];
  24. try {
  25. $grantedUser = $this->getGrantedUser();
  26. if ($grantedUser != null) {
  27. return new RedirectResponse($this->router()->generate('web_app_product_list'));
  28. }
  29. $listPublicProductCategoryCommand = new ListPublicProductCategoryCommand(1, 120, "product_category_name", "asc", "isMainLevel", "T");
  30. $productCategoriesPage = $this->handle($listPublicProductCategoryCommand);
  31. $productCategories = $productCategoriesPage->getCurrentPageResults();
  32. } catch (\Exception $e) {
  33. $errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
  34. $this->setFlash('error', $this->translator()->trans('product.public.list.exception') . $errorMessage);
  35. }
  36. return $this->render(
  37. 'Product/public_product_list.html.twig',
  38. [
  39. 'productCategories' => $productCategories
  40. ]
  41. );
  42. }
  43. /**
  44. * @Route("/product/show/{productSlug}", name="web_public_show_product", defaults={"_format" = "html"})
  45. */
  46. public function postPublicProductBySlugAction(Request $request, string $productSlug)
  47. {
  48. $product = null;
  49. $attributes = null;
  50. try {
  51. $product = $this->handle(new GetProductForShowCommand($productSlug));
  52. if ($product == null) {
  53. $this->setFlash('warning', 'Producto no encontrado!');
  54. return new RedirectResponse($this->router()->generate('web_public_product_list'));
  55. }
  56. $attributes = $this->handle(new ListPublicProductAttributeCommand());
  57. if ($product->productType() == Product::PRODUCT_TYPE_WATER_SUPPLY) {
  58. $whaterpoint = $product->whaterPoint();
  59. return new RedirectResponse($this->router()->generate('web_public_whater_point_show_by_slug', ['whaterPointSlug' => $whaterpoint->slug()]));
  60. } else if ($product->productType() == Product::PRODUCT_TYPE_WATERCOINS) {
  61. return new RedirectResponse($this->router()->generate('web_app_cart_order_buy_whatercoins'));
  62. } else if ($product->productType() == Product::PRODUCT_TYPE_LICENSE) {
  63. throw new \Exception('TODO: Redirect to license product page!');
  64. }
  65. if ($product->whaterOrganization()->stripeExpressAccountStatus() != WhaterOrganization::STRIPE_CONNECT_STATUS_CONNECTED) {
  66. return new RedirectResponse($this->router()->generate('web_public_product_list'));
  67. }
  68. $productVariationsSerialized = $this->jmsSerializer()->serialize(
  69. $product,
  70. 'json',
  71. SerializationContext::create()->setGroups(['PublicProductVariationsShow'])
  72. );
  73. } catch (\Exception $e) {
  74. $errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
  75. $this->setFlash('error', $this->translator()->trans('product.public.show.exception') . $errorMessage);
  76. return new RedirectResponse($this->router()->generate('web_public_product_list'));
  77. }
  78. return $this->render('Product/public_product_show.html.twig', array(
  79. 'product' => $product,
  80. 'productVariationsSerialized' => $productVariationsSerialized,
  81. 'attributes' => $attributes
  82. ));
  83. }
  84. }