src/Service/ShoppingCartService.php line 196

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use App\Dto\CartItem;
  5. use App\Dto\ShoppingCart;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. class ShoppingCartService {
  9. /** @var EntityManagerInterface */
  10. protected $entityManager;
  11. /** @var ProductService */
  12. protected $productService;
  13. /** @var SessionInterface */
  14. protected $session;
  15. /** @var TenantService */
  16. protected $tenantService;
  17. /**
  18. * @param EntityManagerInterface $entityManager
  19. * @param ProductService $productService
  20. * @param SessionInterface $session
  21. * @param TenantService $tenantService
  22. */
  23. public function __construct(
  24. EntityManagerInterface $entityManager,
  25. ProductService $productService,
  26. SessionInterface $session,
  27. TenantService $tenantService
  28. ) {
  29. $this->entityManager = $entityManager;
  30. $this->productService = $productService;
  31. $this->session = $session;
  32. $this->tenantService = $tenantService;
  33. }
  34. /**
  35. * @return array<int, CartItem>
  36. */
  37. public function getCartItems(): array {
  38. $cart = $this->getCart();
  39. return $cart->getItems();
  40. }
  41. /**
  42. * Returns the number of items in the shopping cart.
  43. *
  44. * @return int
  45. */
  46. public function getNumberOfItems(): int {
  47. $numberOfItems = 0;
  48. $cart = $this->getCart();
  49. foreach ($cart->getItems() as $item) {
  50. $numberOfItems += $item->getQuantity();
  51. }
  52. return $numberOfItems;
  53. }
  54. /**
  55. * Returns true or false depending on wether the shopping cart
  56. * contains products that needs configuration.
  57. *
  58. * @return bool
  59. */
  60. public function hasTiProduct(): bool {
  61. $cart = $this->getCart();
  62. foreach ($cart->getItems() as $item) {
  63. $product = $this->productService->findProduct($item->getProductId());
  64. if ($product->getNeedsConfirmation()) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * Returns the total price of the shopping cart.
  72. *
  73. * @return float
  74. */
  75. public function getTotalPrice(): float {
  76. $totalPrice = 0;
  77. $cart = $this->getCart();
  78. foreach ($cart->getItems() as $item) {
  79. $totalPrice += $item->getPrice() * $item->getQuantity();
  80. }
  81. return $totalPrice;
  82. }
  83. /**
  84. * Returns the total recurring price of the shopping cart.
  85. *
  86. * @return float
  87. */
  88. public function getTotalRecurringPrice(): float {
  89. $totalRecurringPrice = 0;
  90. $cart = $this->getCart();
  91. foreach ($cart->getItems() as $item) {
  92. $totalRecurringPrice += $item->getRecurringPrice();
  93. }
  94. return $totalRecurringPrice;
  95. }
  96. /**
  97. * Adds an item to the shopping cart.
  98. *
  99. * @param CartItem $cartItem
  100. *
  101. * @return bool
  102. */
  103. public function addItem(CartItem $cartItem): bool {
  104. $currentCart = $this->getCart();
  105. $newCart = $currentCart->addCartItem($cartItem);
  106. return $this->setCart($newCart);
  107. }
  108. /**
  109. * Removes an item from the shopping cart.
  110. *
  111. * @param CartItem $cartItem
  112. *
  113. * @return bool
  114. */
  115. public function removeItem(CartItem $cartItem): bool {
  116. $cart = $this->getCart();
  117. $cart = $cart->removeCartItem($cartItem);
  118. return $this->setCart($cart);
  119. }
  120. /**
  121. * Decreases the quantity of an item in the shopping cart.
  122. * If the quantity is already 1, the item is removed from the shopping cart.
  123. *
  124. * @param CartItem $cartItem
  125. *
  126. * @return bool
  127. */
  128. public function decreaseQuantity(CartItem $cartItem): bool {
  129. $cart = $this->getCart();
  130. $cart = $cart->decreaseQuantity($cartItem);
  131. return $this->setCart($cart);
  132. }
  133. /**
  134. * Increases the quantity of an item in the shopping cart.
  135. *
  136. * @param CartItem $cartItem
  137. *
  138. * @return bool
  139. */
  140. public function increaseQuantity(CartItem $cartItem): bool {
  141. $cart = $this->getCart();
  142. $cart = $cart->increaseQuantity($cartItem);
  143. return $this->setCart($cart);
  144. }
  145. /**
  146. * Removes all items from the shopping cart.
  147. */
  148. public function clear(): void {
  149. $this->setCart(new ShoppingCart());
  150. }
  151. /**
  152. * Returns the shopping cart.
  153. *
  154. * @return ShoppingCart
  155. */
  156. public function getCart(): ShoppingCart {
  157. return $this->session->get('cart', new ShoppingCart());
  158. }
  159. /**
  160. * Sets the shopping cart.
  161. *
  162. * @param ShoppingCart $cart
  163. *
  164. * @return bool
  165. */
  166. public function setCart(ShoppingCart $cart): bool {
  167. $currentCart = $this->getCart();
  168. $this->session->set('cart', $cart);
  169. $response = false;
  170. if (count($currentCart->getItems()) === 0) {
  171. return true;
  172. }
  173. $response = $this->isEqual($currentCart, $cart);
  174. return $response;
  175. }
  176. /**
  177. * Returns true if the given shopping carts are equal.
  178. *
  179. * @return bool
  180. */
  181. public function isEqual(ShoppingCart $cart1, ShoppingCart $cart2): bool {
  182. if ($cart1->getItems() == $cart2->getItems()) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. }