<?php
namespace Boab\EcommerceBundle\Controller;
use Boab\CmsBundle\Controller\BaseController;
use Boab\CmsBundle\Exception\BoabCmsException;
use Boab\EcommerceBundle\Form\CartType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Boab\EcommerceBundle\Manager\CartManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Serializer\SerializerInterface;
use Fbeen\SettingsBundle\Service\SettingsHelper;
class CartController extends BaseController
{
private $cartManager;
private $serializer;
private $settingsHelper;
public function __construct(
CartManagerInterface $cartManager,
SerializerInterface $serializer,
SettingsHelper $settingsHelper
)
{
$this->cartManager = $cartManager;
$this->serializer = $serializer;
$this->settingsHelper = $settingsHelper;
}
public function indexAction(Request $request)
{
$cart = $this->cartManager->getCart();
$form = $this->createForm(CartType::class, $cart,[
"action" => $this->router->generate("shopping_cart"),
]);
$form->handleRequest($request);
//dump($form);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->getClickedButton() === $form->get('clear')){
$this->cartManager->destroyCart();
return $this->redirectToRoute('shopping_cart');
}
$cart = $form->getData();
$this->cartManager->save($cart);
return $this->redirectToRoute('shopping_checkout');
}
//dump($form);
$taxRate = $this->settingsHelper->getSetting('app_tax');
return $this->render('@Ecommerce/Main/cart.html.twig', [
'cart' => $cart,
'form' => $form->createView(),
'tax'=> $taxRate,
'pageTitle' => 'Cart',
'sub_total' => $this->cartManager->getSubtotal(),
'tax_charges' => $this->cartManager->getCharges(),
'grand_total' => $this->cartManager->getGrandTotal()
]);
}
public function addToCartAction(Request $request)
{
$productId = $request->get('productId');
try{
if(!$this->cartManager->itemExists($productId)){
$this->cartManager->addToCart($productId, (int)$request->get('qty'));
}else{
$this->cartManager->updateItem($productId, (int)$request->get('qty'));
}
$cart = $this->cartManager->getCart();
$data = $this->serializer->normalize($cart);
if($request->isXmlHttpRequest()){
return new JsonResponse([
'status' => 'success',
'message' => 'Cart updated successfully',
...$data
]);
}
return $this->redirect($this->router->generate('shopping_cart'));
}catch(\Exception $e){
throw new BoabCmsException($e->getMessage(), 500);
}
}
public function updateAction(Request $request)
{
$cart = $this->cartManager->getCart();
if($cart->isEmpty()){
throw new HttpException(400, 'The cart session has expired');
}
try{
$products = $request->get("products");
$this->cartManager->updateItems($products);
//$data = $this->serializer->normalize($cart, 'json');
return $this->redirect($this->router->generate('shopping_cart'));
}catch(\Exception $e){
throw new BoabCmsException($e->getMessage(), 500);
}
}
public function removeAction(Request $request, string $productId)
{
if(!$this->cartManager->itemExists($productId)){
throw new NotFoundHttpException();
}
$this->cartManager->removeItem($productId);
if(!$this->cartManager->hasItems()){
$this->cartManager->destroyCart();
}
$cart = $this->cartManager->getCart();
$data = $this->serializer->normalize($cart);
return new JsonResponse([
"status" => "success",
"message"=>"Product deleted fro the cart successfully",
...$data
]);
}
public function emptyAction(Request $request)
{
$this->cartManager->destroyCart();
return $this->redirect($this->router->generate('shopping_cart'));
}
public function getCart(Request $request)
{
$cart = $this->cartManager->getCart();
$data = $this->serializer->normalize($cart);
return new JsonResponse($data);
}
public function updateProductAction(Request $request, string $productId)
{
$cart = $this->cartManager->getCart();
if($cart->isEmpty()){
throw new HttpException(400, 'The cart session has expired');
}
if(!$this->cartManager->itemExists($productId)){
throw new HttpException(400, 'The product does not extsts in the cart');
}
try{
$postData = json_decode($request->getContent(), true);
$this->cartManager->updateItem($productId, $postData['qty']??1);
$data = $this->serializer->normalize($cart);
return new JsonResponse([
'status' => 'sucess',
'message' => 'Product updated successfully',
...$data,
]);
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['eception'=>$e]);
throw new HttpException(400, 'An unknown error occured while updating product.');
}
}
}