src/Service/BasketService.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\BasketItem;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. class BasketService
  8. {
  9. private $session;
  10. private $entityManager;
  11. public function __construct(SessionInterface $session, EntityManagerInterface $entityManager)
  12. {
  13. $this->session = $session;
  14. $this->entityManager = $entityManager;
  15. }
  16. public function getBasket(){
  17. return $this->session->get("basket", []);
  18. }
  19. public function getRefreshedBasket(){
  20. $basket = $this->getBasket();
  21. foreach($basket as $basketItem){
  22. $basketItem->refresh($this->entityManager);
  23. }
  24. $this->setBasket($basket);
  25. return $basket;
  26. }
  27. public function hasItem(): ?bool
  28. {
  29. return count($this->getBasket()) > 0;
  30. }
  31. public function setBasket($basket){
  32. $this->session->set("basket", $basket);
  33. }
  34. public function add(BasketItem $basketItem){
  35. $basket = $this->getBasket();
  36. $index = $this->indexOf($basketItem->getProduct()->getId());
  37. if($index!=-1){
  38. $qty = $basket[$index]->getQuantity() + $basketItem->getQuantity();
  39. $basketItem->setQuantity($qty);
  40. $this->update($basketItem->getProduct()->getId(), $basketItem);
  41. } else{
  42. $this->checkBasketItem($basketItem);
  43. $basket[] = $basketItem;
  44. $this->setBasket($basket);
  45. }
  46. $this->refreshTotalCost();
  47. }
  48. public function refreshTotalCost(){
  49. $totalCost = $this->getTotalCost();
  50. $basket = $this->getBasket();
  51. $totalCostWithTVA = $this->getTotalCostAndTVAWithBasket($basket);
  52. $this->session->set("basketTotalCost", $totalCost);
  53. $this->session->set("totalCostWithTVA", $totalCostWithTVA);
  54. }
  55. public function indexOf($productId){
  56. $basket = $this->getBasket();
  57. $index = -1;
  58. for($i=0; $i<count($basket) ; $i++){
  59. if($basket[$i]->getProduct()->getId()==$productId) {
  60. $index = $i;
  61. break;
  62. }
  63. }
  64. return $index;
  65. }
  66. public function getTotalCost(){
  67. $basket = $this->getBasket();
  68. return $this->getTotalCostWithBasket($basket);
  69. }
  70. public function getTotalCostWithBasket($basket){
  71. $cost = 0;
  72. for($i=0; $i<count($basket) ; $i++){
  73. $cost += $basket[$i]->getCost();
  74. }
  75. return $cost;
  76. }
  77. public function getTotalCostAndTVAWithBasket($basket){
  78. $cost = 0;
  79. $tva = 0;
  80. for($i=0; $i<count($basket) ; $i++){
  81. $cost += $basket[$i]->getCost();
  82. $tva += ProductService::calculateTvaFromTTC($basket[$i]->getCost(), $basket[$i]->getProduct()->getTva());
  83. }
  84. $ht = $cost - $tva;
  85. return [
  86. "cost" => $cost,
  87. "tva" => $tva,
  88. "ht" => $ht,
  89. ];
  90. }
  91. public function checkBasketItem(BasketItem $basketItem){
  92. $basketItem->refresh($this->entityManager);
  93. $basketItem->getProduct()->checkQty($basketItem->getQuantity());
  94. return true;
  95. }
  96. public function update($productId, $basketItem){
  97. $this->checkBasketItem($basketItem);
  98. $basket = $this->getBasket();
  99. $index = $this->indexOf($productId);
  100. if($index!=-1) array_splice($basket, $index, 1);
  101. $basket[] = $basketItem;
  102. $this->setBasket($basket);
  103. $this->refreshTotalCost();
  104. }
  105. public function remove($productId){
  106. $basket = $this->getBasket();
  107. $index = $this->indexOf($productId);
  108. if($index!=-1) array_splice($basket, $index, 1);
  109. $this->setBasket($basket);
  110. $this->refreshTotalCost();
  111. }
  112. public function removeBasket(){
  113. $this->session->remove("basket");
  114. $this->refreshTotalCost();
  115. }
  116. }