<?php
namespace App\Service;
class CartHelper {
/**
* constructor
*
* @param \App\Repository\CourseRepository $courseRepository
* @param \App\Repository\VoucherRepository $voucherRepository
* @param \App\Repository\OrderVoucherRepository $orderVoucherRepository
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $params
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
*/
public function __construct(
\Symfony\Component\Security\Core\Security $security,
\App\Repository\CourseRepository $courseRepository,
\App\Repository\VoucherRepository $voucherRepository,
\App\Repository\OrderVoucherRepository $orderVoucherRepository,
\Symfony\Component\HttpFoundation\RequestStack $requestStack,
\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $params,
\Doctrine\ORM\EntityManagerInterface $entityManager
) {
$this->security = $security;
$this->requestStack = $requestStack;
$this->params = $params;
$this->courseRepository = $courseRepository;
$this->voucherRepository = $voucherRepository;
$this->orderVoucherRepository = $orderVoucherRepository;
$this->entityManager = $entityManager;
}
/**
* Find Valid Voucher for user by code
*
* @param string $code
* @return \App\Entity\Voucher|void
*/
public function checkValidVoucherCode(
string $code
) {
$voucher = $this->voucherRepository->findOneBy(['code' => $code]);
if ($voucher) :
if (!$voucher->getIsEnabled()) return false;
$dateNow = new \DateTime();
if ($voucher->getValidFrom() > $dateNow) return false;
if ($voucher->getValidUntil() < $dateNow) return false;
if ($voucher->getVoucherProductType() == \App\DBAL\Types\VoucherProductType::SELECTED_PRODUCTS) :
$found = false;
$cart = $this->getCart();
$cartIDs = [];
foreach ($cart['products'] as $product) :
$cartIDs[] = $product['id'];
endforeach;
foreach ($voucher->getCourses() as $product) :
if (in_array($product->getID(), $cartIDs)) $found = true;
endforeach;
if (!$found) return false;
endif;
$user = $this->security->getUser();
if ($voucher->getVoucherType() == \App\DBAL\Types\VoucherType::ONE_TOTAL) :
$exist = $this->orderVoucherRepository->findVoucherUsed($voucher);
if ($exist) :
return false;
endif;
elseif ($voucher->getVoucherType() == \App\DBAL\Types\VoucherType::ONE_PER_USER) :
$exist = $this->orderVoucherRepository->findVoucherUsed($voucher, $user);
if ($exist) :
return false;
endif;
endif;
return $voucher;
endif;
return false;
}
/**
* Update Session Cart
*
* @return void
*/
private function updateSessionCart() {
$session = $this->requestStack->getSession();
$products_in_cart = $session->get('cart', []);
$products = [];
$totalQty = 0;
$subtotal = 0.00;
if ($products_in_cart) :
$courses = $this->courseRepository->findBy(["id" => array_keys($products_in_cart), "isEnabled" => true]);
foreach ($courses as $product) :
$products[] = [
"id" => $product->getID(),
"title" => $product->getTitle(),
"price" => $product->getPriceCost(),
"final" => $product->getPriceFinal(),
"qty" => (int)$products_in_cart[$product->getID()],
];
$totalQty += (int)$products_in_cart[$product->getID()];
$subtotal += (float)$product->getPriceFinal() * (int)$products_in_cart[$product->getID()];
endforeach;
endif;
$session->set('minicart', [
"products" => $products,
"subtotal" => $subtotal,
"qty" => $totalQty
]);
}
/**
* Add Product to cart
*
* @param \App\Entity\Course|null $course
* @param Int $quantity
* @return void
*/
public function addProduct(?\App\Entity\Course $course, Int $quantity) {
$session = $this->requestStack->getSession();
$cart = $session->get('cart', []);
if ($course && $quantity > 0) :
if (array_key_exists($course->getID(), $cart)) :
//$cart[$course->getID()] += $quantity;
$cart[$course->getID()] = 1;
else :
//$cart[$course->getID()] = $quantity;
$cart[$course->getID()] = 1;
endif;
$session->set('cart', $cart);
$this->updateSessionCart();
endif;
}
/**
* Remore product from cart
*
* @param \App\Entity\Course|null $course
* @return void
*/
public function removeProduct(?\App\Entity\Course $course) {
$session = $this->requestStack->getSession();
$cart = $session->get('cart', []);
if ($course) :
if (array_key_exists($course->getID(), $cart)) :
unset($cart[$course->getID()]);
$session->set('cart', $cart);
$this->updateSessionCart();
endif;
endif;
}
/**
* Add Voucher to cart
*
* @param \App\Entity\Voucher|null $voucher
* @return void
*/
public function addVoucher(?\App\Entity\Voucher $voucher) {
$session = $this->requestStack->getSession();
$sessionVoucher = $session->get('voucher', null);
if ($voucher) :
if ($sessionVoucher) :
$sessionVoucher = [
"id" => $voucher->getID(),
"title" => $voucher->getTitle(),
"code" => $voucher->getCode()
];
$session->set('voucher', $sessionVoucher);
$this->updateSessionCart();
else :
$sessionVoucher = [
"id" => $voucher->getID(),
"title" => $voucher->getTitle(),
"code" => $voucher->getCode()
];
$session->set('voucher', $sessionVoucher);
$this->updateSessionCart();
endif;
endif;
}
/**
* Remove Voucher from cart
*
* @param \App\Entity\Voucher|null $voucher
* @return void
*/
public function removeVoucher(?\App\Entity\Voucher $voucher) {
$session = $this->requestStack->getSession();
$sessionVoucher = $session->get('voucher', null);
if ($voucher && $sessionVoucher) :
if (isset($sessionVoucher['id']) && $sessionVoucher['id'] == $voucher->getID()) :
$session->remove('voucher');
$this->updateSessionCart();
endif;
endif;
}
/**
* Set unique cart session id
*
* @param string $uniqueID
* @return void
*/
public function setUniqueID(string $uniqueID) {
$session = $this->requestStack->getSession();
$session->set('uniqueCartID', $uniqueID);
}
/**
* Clear Current Cart
*
* @return void
*/
public function clearCart() {
$session = $this->requestStack->getSession();
$session->remove('uniqueCartID');
$session->remove('voucher');
$session->remove('cart');
$session->remove('minicart');
}
public function getVoucher() {
$session = $this->requestStack->getSession();
$sessionVoucher = $session->get('voucher', null);
$voucher = $sessionVoucher?$this->checkValidVoucherCode($sessionVoucher['code']):null;
$voucherData = null;
if ($voucher) :
$voucherData = [
'id' => $voucher->getID(),
'code' => $voucher->getCode(),
'value' => $voucher->getDiscountValue(),
'type' => $voucher->getDiscountType(),
'for' => $voucher->getDiscountFor(),
'productType' => $voucher->getVoucherProductType(),
'products' => []
];
if ($voucher->getVoucherProductType() == \App\DBAL\Types\VoucherProductType::SELECTED_PRODUCTS) :
foreach ($voucher->getCourses() as $course) :
$voucherData['products'][] = $course->getID();
endforeach;
endif;
endif;
return $voucherData;
}
/**
* Get Current Cart
*
* @return array
*/
public function getCart() {
$session = $this->requestStack->getSession();
$products_in_cart = $session->get('cart', []);
$products = [];
$subtotal = 0.00;
$totalQty = 0;
if ($products_in_cart) :
$courses = $this->courseRepository->findBy(["id" => array_keys($products_in_cart), "isEnabled" => true]);
foreach ($courses as $product) :
$products[] = [
"id" => $product->getID(),
"title" => $product->getTitle(),
"price" => $product->getPriceCost(),
"final" => $product->getPriceFinal(),
"qty" => (int)$products_in_cart[$product->getID()]
];
$totalQty += (int)$products_in_cart[$product->getID()];
$subtotal += (float)$product->getPriceFinal() * (int)$products_in_cart[$product->getID()];
endforeach;
endif;
$uniqueID = $session->get('uniqueCartID', null);
return [
"uniqueID" => $uniqueID,
"products" => $products,
"subtotal" => $subtotal,
"qty" => $totalQty
];
}
}