src/Controller/Api/CartController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Address;
  4. use App\Entity\Cart;
  5. use App\Entity\CartElement;
  6. use App\Entity\Country;
  7. use App\Entity\Payment;
  8. use App\Entity\PaymentMethod;
  9. use App\Entity\User;
  10. use App\Service\CartService;
  11. use App\Service\Rsa;
  12. use App\Service\SerializeService;
  13. use App\Service\Tools;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Exception;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. /**
  23.  * Class CartController
  24.  * @package App\Controller\Api
  25.  * @Route(path="/api/cart", name="api_cart")
  26.  */
  27. class CartController extends AbstractController
  28. {
  29.     private EntityManagerInterface $em;
  30.     private SerializeService $serializeService;
  31.     private Rsa $rsa;
  32.     private TranslatorInterface $translator;
  33.     private CartService $cartService;
  34.     public function __construct(
  35.         EntityManagerInterface $em,
  36.         SerializeService $serializeService,
  37.         Rsa $rsa,
  38.         TranslatorInterface $translator,
  39.         CartService $cartService
  40.     ) {
  41.         $this->em = $em;
  42.         $this->serializeService = $serializeService;
  43.         $this->rsa = $rsa;
  44.         $this->translator = $translator;
  45.         $this->cartService = $cartService;
  46.     }
  47.     /**
  48.      * @param Request $request
  49.      * @return JsonResponse
  50.      * @throws Exception
  51.      * @Route("", name="")
  52.      */
  53.     public function find(Request $request): JsonResponse
  54.     {
  55.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  56.             return new JsonResponse([
  57.                 'success' => false,
  58.                 'message' => $this->translator->trans('global.invalidToken'),
  59.             ]);
  60.         }
  61.         /** @var Cart $cart */
  62.         $cart = $this->cartService->getCurrentCart();
  63.         $prices = [];
  64.         if ($cart) {
  65.             $prices = $this->cartService->getPrices($cart);
  66.         }
  67.         return new JsonResponse([
  68.             'success' => true,
  69.             'cart' => $this->serializeService->serializeCart($cart),
  70.             'prices' => $prices,
  71.         ]);
  72.     }
  73.     /**
  74.      * @param Request $request
  75.      * @return JsonResponse
  76.      * @Route("/update/address", name="update_address")
  77.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  78.      */
  79.     public function updateAddress(Request $request): JsonResponse
  80.     {
  81.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  82.             return new JsonResponse([
  83.                 'success' => false,
  84.                 'message' => $this->translator->trans('global.invalidToken'),
  85.             ]);
  86.         }
  87.         /** @var Cart $cart */
  88.         $cart = $this->cartService->getCurrentCart();
  89.         /** @var User $user */
  90.         $user = $cart->getUser();
  91.         $data = Tools::getRequestData($request);
  92.         $sameBillingAsDelivery = $data['sameBillingAsDelivery'];
  93.         // Delivery Address
  94.         $deliveryAddress = new Address();
  95.         $deliveryAddress->setCivility($data['form']['deliveryAddress']['civility']);
  96.         $deliveryAddress->setLastname($data['form']['deliveryAddress']['lastname']);
  97.         $deliveryAddress->setFirstname($data['form']['deliveryAddress']['firstname']);
  98.         $deliveryAddress->setCompany($data['form']['deliveryAddress']['company']);
  99.         $deliveryAddress->setStreet($data['form']['deliveryAddress']['street']);
  100.         $deliveryAddress->setAdditional($data['form']['deliveryAddress']['additional']);
  101.         $deliveryAddress->setPostalCode($data['form']['deliveryAddress']['postalCode']);
  102.         $deliveryAddress->setCity($data['form']['deliveryAddress']['city']);
  103.         $deliveryAddress->setPhone($data['form']['deliveryAddress']['phone']);
  104.         /** @var Country $country */
  105.         $country = $this->em->getRepository(Country::class)->find($data['form']['deliveryAddress']['country']);
  106.         $deliveryAddress->setCountry($country);
  107.         $deliveryAddressSerialized = $this->serializeService->serializeAddress($deliveryAddress);
  108.         unset($deliveryAddressSerialized['id']);
  109.         unset($deliveryAddressSerialized['name']);
  110.         unset($deliveryAddressSerialized['isDefault']);
  111.         $addressExist = null;
  112.         foreach ($cart->getUser()->getAddresses() as $address) {
  113.             $addressSerialized = $this->serializeService->serializeAddress($address);
  114.             unset($addressSerialized['id']);
  115.             unset($addressSerialized['name']);
  116.             unset($addressSerialized['isDefault']);
  117.             if ($deliveryAddressSerialized === $addressSerialized) {
  118.                 $addressExist = $address;
  119.             }
  120.         }
  121.         if (is_null($addressExist)) {
  122.             $user->addAddress($deliveryAddress);
  123.             $cart->setDeliveryAddress($deliveryAddress);
  124.             $this->em->persist($cart);
  125.             $this->em->persist($user);
  126.         } else {
  127.             $cart->setDeliveryAddress($addressExist);
  128.             $this->em->persist($cart);
  129.         }
  130.         // Billing Address
  131.         if ($sameBillingAsDelivery) {
  132.             $cart->setBillingAddress($cart->getDeliveryAddress());
  133.         } else {
  134.             $billingAddress = new Address();
  135.             $billingAddress->setCivility($data['form']['billingAddress']['civility']);
  136.             $billingAddress->setLastname($data['form']['billingAddress']['lastname']);
  137.             $billingAddress->setFirstname($data['form']['billingAddress']['firstname']);
  138.             $billingAddress->setCompany($data['form']['billingAddress']['company']);
  139.             $billingAddress->setStreet($data['form']['billingAddress']['street']);
  140.             $billingAddress->setAdditional($data['form']['billingAddress']['additional']);
  141.             $billingAddress->setPostalCode($data['form']['billingAddress']['postalCode']);
  142.             $billingAddress->setCity($data['form']['billingAddress']['city']);
  143.             $billingAddress->setPhone($data['form']['billingAddress']['phone']);
  144.             /** @var Country $country */
  145.             $country = $this->em->getRepository(Country::class)->find($data['form']['deliveryAddress']['country']);
  146.             $billingAddress->setCountry($country);
  147.             $billingAddressSerialized = $this->serializeService->serializeAddress($billingAddress);
  148.             unset($billingAddressSerialized['id']);
  149.             unset($billingAddressSerialized['name']);
  150.             unset($billingAddressSerialized['isDefault']);
  151.             $addressExist = null;
  152.             foreach ($cart->getUser()->getAddresses() as $address) {
  153.                 $addressSerialized = $this->serializeService->serializeAddress($address);
  154.                 unset($addressSerialized['id']);
  155.                 unset($addressSerialized['name']);
  156.                 unset($addressSerialized['isDefault']);
  157.                 if ($billingAddressSerialized === $addressSerialized) {
  158.                     $addressExist = $address;
  159.                 }
  160.             }
  161.             if (is_null($addressExist)) {
  162.                 $user->addAddress($billingAddress);
  163.                 $cart->setBillingAddress($billingAddress);
  164.                 $this->em->persist($cart);
  165.                 $this->em->persist($user);
  166.             } else {
  167.                 $cart->setBillingAddress($addressExist);
  168.                 $this->em->persist($cart);
  169.             }
  170.         }
  171.         $this->em->flush();
  172.         return new JsonResponse([
  173.             'success' => true,
  174.         ]);
  175.     }
  176.     /**
  177.      * @param Request $request
  178.      * @return JsonResponse
  179.      * @Route("/payment-methods", name="payment_methods")
  180.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  181.      */
  182.     public function findPaymentMethod(Request $request): JsonResponse
  183.     {
  184.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  185.             return new JsonResponse([
  186.                 'success' => false,
  187.                 'message' => $this->translator->trans('global.invalidToken'),
  188.             ]);
  189.         }
  190.         $data = [];
  191.         /** @var Cart $cart */
  192.         $cart = $this->cartService->getCurrentCart();
  193.         if ($cart) {
  194.             /** @var PaymentMethod[] $paymentMethods */
  195.             $paymentMethods = $this->em->getRepository(PaymentMethod::class)->findBy(['active' => true], ['position' => 'ASC']);
  196.             foreach ($paymentMethods as $paymentMethod) {
  197.                 $data[] = $this->serializeService->serializePaymentMethod($paymentMethod);
  198.             }
  199.         }
  200.         return new JsonResponse([
  201.             'success' => true,
  202.             'paymentMethods' => $data,
  203.         ]);
  204.     }
  205.     /**
  206.      * @param $reference
  207.      * @param Request $request
  208.      * @return JsonResponse
  209.      * @throws Exception
  210.      * @Route("/validated/{reference}", name="validated")
  211.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  212.      */
  213.     public function validated($reference, Request $request): JsonResponse
  214.     {
  215.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  216.             return new JsonResponse([
  217.                 'success' => false,
  218.                 'message' => $this->translator->trans('global.invalidToken'),
  219.             ]);
  220.         }
  221.         $cartId = (int)str_replace(Payment::PREFIX_REFERENCE_CART, '', $reference);
  222.         /** @var Cart $cart */
  223.         $cart = $this->em->getRepository(Cart::class)->find($cartId);
  224.         if (!$cart or $cart->getUser() !== $this->getUser() or !$cart->getDone()) {
  225.             return new JsonResponse([
  226.                 'error' => true,
  227.             ]);
  228.         }
  229.         $orders = [];
  230.         $payment = null;
  231.         /** @var CartElement $cartElement */
  232.         foreach ($cart->getCartElements() as $cartElement) {
  233.             $orders[] = $this->serializeService->serializeOrder($cartElement->getOrder());
  234.             $payment = $this->serializeService->serializePayment($cartElement->getOrder()->getPayment());
  235.         }
  236.         return new JsonResponse([
  237.             'success' => true,
  238.             'orders' => $orders,
  239.             'payment' => $payment,
  240.         ]);
  241.     }
  242.     /**
  243.      * @param Request $request
  244.      * @return JsonResponse
  245.      * @throws Exception
  246.      * @Route("/add/promotion", name="add_promotion")
  247.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  248.      */
  249.     public function addPromotion(Request $request): JsonResponse
  250.     {
  251.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  252.             return new JsonResponse([
  253.                 'success' => false,
  254.                 'message' => $this->translator->trans('global.invalidToken'),
  255.             ]);
  256.         }
  257.         $data = Tools::getRequestData($request);
  258.         // Vérification si le code promotion est valide
  259.         $promotionAddData = $this->cartService->addCodePromotion($this->cartService->getCurrentCart(), $data['form']['promotion']['code']);
  260.         if (!$promotionAddData['success']) {
  261.             return new JsonResponse([
  262.                 'success' => true,
  263.                 'message' => $promotionAddData['message'],
  264.                 'cart' => $this->serializeService->serializeCart($this->cartService->getCurrentCart()),
  265.                 'prices' => $this->cartService->getPrices($this->cartService->getCurrentCart())
  266.             ]);
  267.         }
  268.         /** @var Cart $cart */
  269.         $cart = $this->cartService->getCurrentCart();
  270.         $prices = [];
  271.         if ($cart) {
  272.             $prices = $this->cartService->getPrices($cart);
  273.         }
  274.         return new JsonResponse([
  275.             'success' => true,
  276.             'cart' => $this->serializeService->serializeCart($cart),
  277.             'prices' => $prices
  278.         ]);
  279.     }
  280.     /**
  281.      * @param Request $request
  282.      * @return JsonResponse
  283.      * @throws Exception
  284.      * @Route("/remove/promotion", name="remove_promotion")
  285.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  286.      */
  287.     public function removePromotion(Request $request): JsonResponse
  288.     {
  289.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  290.             return new JsonResponse([
  291.                 'success' => false,
  292.                 'message' => $this->translator->trans('global.invalidToken'),
  293.             ]);
  294.         }
  295.         /** @var Cart $cart */
  296.         $cart = $this->cartService->getCurrentCart();
  297.         foreach ($cart->getCartElements() as $cartElement) {
  298.             $cartElement->setPromotion(null);
  299.             $this->em->persist($cartElement);
  300.         }
  301.         $this->em->flush();
  302.         return new JsonResponse([
  303.             'success' => true,
  304.             'cart' => $this->serializeService->serializeCart($cart),
  305.             'prices' => $this->cartService->getPrices($cart)
  306.         ]);
  307.     }
  308. }