<?php
namespace App\Controller\Api;
use App\Entity\Address;
use App\Entity\Cart;
use App\Entity\CartElement;
use App\Entity\Country;
use App\Entity\Payment;
use App\Entity\PaymentMethod;
use App\Entity\User;
use App\Service\CartService;
use App\Service\Rsa;
use App\Service\SerializeService;
use App\Service\Tools;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class CartController
* @package App\Controller\Api
* @Route(path="/api/cart", name="api_cart")
*/
class CartController extends AbstractController
{
private EntityManagerInterface $em;
private SerializeService $serializeService;
private Rsa $rsa;
private TranslatorInterface $translator;
private CartService $cartService;
public function __construct(
EntityManagerInterface $em,
SerializeService $serializeService,
Rsa $rsa,
TranslatorInterface $translator,
CartService $cartService
) {
$this->em = $em;
$this->serializeService = $serializeService;
$this->rsa = $rsa;
$this->translator = $translator;
$this->cartService = $cartService;
}
/**
* @param Request $request
* @return JsonResponse
* @throws Exception
* @Route("", name="")
*/
public function find(Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
/** @var Cart $cart */
$cart = $this->cartService->getCurrentCart();
$prices = [];
if ($cart) {
$prices = $this->cartService->getPrices($cart);
}
return new JsonResponse([
'success' => true,
'cart' => $this->serializeService->serializeCart($cart),
'prices' => $prices,
]);
}
/**
* @param Request $request
* @return JsonResponse
* @Route("/update/address", name="update_address")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function updateAddress(Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
/** @var Cart $cart */
$cart = $this->cartService->getCurrentCart();
/** @var User $user */
$user = $cart->getUser();
$data = Tools::getRequestData($request);
$sameBillingAsDelivery = $data['sameBillingAsDelivery'];
// Delivery Address
$deliveryAddress = new Address();
$deliveryAddress->setCivility($data['form']['deliveryAddress']['civility']);
$deliveryAddress->setLastname($data['form']['deliveryAddress']['lastname']);
$deliveryAddress->setFirstname($data['form']['deliveryAddress']['firstname']);
$deliveryAddress->setCompany($data['form']['deliveryAddress']['company']);
$deliveryAddress->setStreet($data['form']['deliveryAddress']['street']);
$deliveryAddress->setAdditional($data['form']['deliveryAddress']['additional']);
$deliveryAddress->setPostalCode($data['form']['deliveryAddress']['postalCode']);
$deliveryAddress->setCity($data['form']['deliveryAddress']['city']);
$deliveryAddress->setPhone($data['form']['deliveryAddress']['phone']);
/** @var Country $country */
$country = $this->em->getRepository(Country::class)->find($data['form']['deliveryAddress']['country']);
$deliveryAddress->setCountry($country);
$deliveryAddressSerialized = $this->serializeService->serializeAddress($deliveryAddress);
unset($deliveryAddressSerialized['id']);
unset($deliveryAddressSerialized['name']);
unset($deliveryAddressSerialized['isDefault']);
$addressExist = null;
foreach ($cart->getUser()->getAddresses() as $address) {
$addressSerialized = $this->serializeService->serializeAddress($address);
unset($addressSerialized['id']);
unset($addressSerialized['name']);
unset($addressSerialized['isDefault']);
if ($deliveryAddressSerialized === $addressSerialized) {
$addressExist = $address;
}
}
if (is_null($addressExist)) {
$user->addAddress($deliveryAddress);
$cart->setDeliveryAddress($deliveryAddress);
$this->em->persist($cart);
$this->em->persist($user);
} else {
$cart->setDeliveryAddress($addressExist);
$this->em->persist($cart);
}
// Billing Address
if ($sameBillingAsDelivery) {
$cart->setBillingAddress($cart->getDeliveryAddress());
} else {
$billingAddress = new Address();
$billingAddress->setCivility($data['form']['billingAddress']['civility']);
$billingAddress->setLastname($data['form']['billingAddress']['lastname']);
$billingAddress->setFirstname($data['form']['billingAddress']['firstname']);
$billingAddress->setCompany($data['form']['billingAddress']['company']);
$billingAddress->setStreet($data['form']['billingAddress']['street']);
$billingAddress->setAdditional($data['form']['billingAddress']['additional']);
$billingAddress->setPostalCode($data['form']['billingAddress']['postalCode']);
$billingAddress->setCity($data['form']['billingAddress']['city']);
$billingAddress->setPhone($data['form']['billingAddress']['phone']);
/** @var Country $country */
$country = $this->em->getRepository(Country::class)->find($data['form']['deliveryAddress']['country']);
$billingAddress->setCountry($country);
$billingAddressSerialized = $this->serializeService->serializeAddress($billingAddress);
unset($billingAddressSerialized['id']);
unset($billingAddressSerialized['name']);
unset($billingAddressSerialized['isDefault']);
$addressExist = null;
foreach ($cart->getUser()->getAddresses() as $address) {
$addressSerialized = $this->serializeService->serializeAddress($address);
unset($addressSerialized['id']);
unset($addressSerialized['name']);
unset($addressSerialized['isDefault']);
if ($billingAddressSerialized === $addressSerialized) {
$addressExist = $address;
}
}
if (is_null($addressExist)) {
$user->addAddress($billingAddress);
$cart->setBillingAddress($billingAddress);
$this->em->persist($cart);
$this->em->persist($user);
} else {
$cart->setBillingAddress($addressExist);
$this->em->persist($cart);
}
}
$this->em->flush();
return new JsonResponse([
'success' => true,
]);
}
/**
* @param Request $request
* @return JsonResponse
* @Route("/payment-methods", name="payment_methods")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function findPaymentMethod(Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
$data = [];
/** @var Cart $cart */
$cart = $this->cartService->getCurrentCart();
if ($cart) {
/** @var PaymentMethod[] $paymentMethods */
$paymentMethods = $this->em->getRepository(PaymentMethod::class)->findBy(['active' => true], ['position' => 'ASC']);
foreach ($paymentMethods as $paymentMethod) {
$data[] = $this->serializeService->serializePaymentMethod($paymentMethod);
}
}
return new JsonResponse([
'success' => true,
'paymentMethods' => $data,
]);
}
/**
* @param $reference
* @param Request $request
* @return JsonResponse
* @throws Exception
* @Route("/validated/{reference}", name="validated")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function validated($reference, Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
$cartId = (int)str_replace(Payment::PREFIX_REFERENCE_CART, '', $reference);
/** @var Cart $cart */
$cart = $this->em->getRepository(Cart::class)->find($cartId);
if (!$cart or $cart->getUser() !== $this->getUser() or !$cart->getDone()) {
return new JsonResponse([
'error' => true,
]);
}
$orders = [];
$payment = null;
/** @var CartElement $cartElement */
foreach ($cart->getCartElements() as $cartElement) {
$orders[] = $this->serializeService->serializeOrder($cartElement->getOrder());
$payment = $this->serializeService->serializePayment($cartElement->getOrder()->getPayment());
}
return new JsonResponse([
'success' => true,
'orders' => $orders,
'payment' => $payment,
]);
}
/**
* @param Request $request
* @return JsonResponse
* @throws Exception
* @Route("/add/promotion", name="add_promotion")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function addPromotion(Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
$data = Tools::getRequestData($request);
// Vérification si le code promotion est valide
$promotionAddData = $this->cartService->addCodePromotion($this->cartService->getCurrentCart(), $data['form']['promotion']['code']);
if (!$promotionAddData['success']) {
return new JsonResponse([
'success' => true,
'message' => $promotionAddData['message'],
'cart' => $this->serializeService->serializeCart($this->cartService->getCurrentCart()),
'prices' => $this->cartService->getPrices($this->cartService->getCurrentCart())
]);
}
/** @var Cart $cart */
$cart = $this->cartService->getCurrentCart();
$prices = [];
if ($cart) {
$prices = $this->cartService->getPrices($cart);
}
return new JsonResponse([
'success' => true,
'cart' => $this->serializeService->serializeCart($cart),
'prices' => $prices
]);
}
/**
* @param Request $request
* @return JsonResponse
* @throws Exception
* @Route("/remove/promotion", name="remove_promotion")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function removePromotion(Request $request): JsonResponse
{
if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
return new JsonResponse([
'success' => false,
'message' => $this->translator->trans('global.invalidToken'),
]);
}
/** @var Cart $cart */
$cart = $this->cartService->getCurrentCart();
foreach ($cart->getCartElements() as $cartElement) {
$cartElement->setPromotion(null);
$this->em->persist($cartElement);
}
$this->em->flush();
return new JsonResponse([
'success' => true,
'cart' => $this->serializeService->serializeCart($cart),
'prices' => $this->cartService->getPrices($cart)
]);
}
}