lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Component/Cart.php line 95

Open in your IDE?
  1. <?php
  2. /**
  3. * Cart
  4. *
  5. * @author Bertin van den Ham <b.vandenham@visualmedia.nl>
  6. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  7. */
  8. namespace VisualMedia\OrderBundle\Component;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use VisualMedia\DiscountBundle\Discount\DiscountFactoryInterface;
  13. use VisualMedia\DiscountBundle\Entity\Discount;
  14. use VisualMedia\DiscountBundle\Entity\DiscountRegistry;
  15. use VisualMedia\DiscountBundle\Service\DiscountProvider;
  16. use VisualMedia\LisaBundle\Component\SerializerInterface;
  17. use VisualMedia\OrderBundle\Component\Interfaces\CartInterface;
  18. use VisualMedia\OrderBundle\Entity\BaseOrder;
  19. use VisualMedia\OrderBundle\Entity\BaseOrderItem;
  20. use VisualMedia\OrderBundle\Model\CartModel;
  21. use VisualMedia\OrderBundle\Provider\ShippingMethodProvider;
  22. use VisualMedia\PaymentBundle\Provider\PaymentMethodProvider;
  23. /**
  24. * Cart
  25. */
  26. abstract class Cart implements CartInterface
  27. {
  28. /**
  29. * @var SerializerInterface
  30. */
  31. protected $serializer;
  32. /**
  33. * @var SessionInterface
  34. */
  35. protected $session;
  36. /**
  37. * @var ShippingMethodProvider
  38. */
  39. protected $shippingMethodProvider;
  40. /**
  41. * @var PaymentMethodProvider
  42. */
  43. protected $paymentMethodProvider;
  44. /**
  45. * @var BaseOrder
  46. */
  47. protected $order;
  48. /**
  49. * @var CartModel
  50. */
  51. protected $cart;
  52. /**
  53. * @param SerializerInterface $serializer
  54. * @param SessionInterface $session
  55. * @param ShippingMethodProvider $shippingMethodProvider
  56. * @param PaymentMethodProvider $paymentMethodProvider
  57. */
  58. public function __construct(SerializerInterface $serializer, SessionInterface $session, ShippingMethodProvider $shippingMethodProvider, PaymentMethodProvider $paymentMethodProvider)
  59. {
  60. $this->serializer = $serializer;
  61. $this->session = $session;
  62. $this->shippingMethodProvider = $shippingMethodProvider;
  63. $this->paymentMethodProvider = $paymentMethodProvider;
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function clear(): void
  69. {
  70. $this->session->remove(static::ORDER);
  71. $this->session->remove(static::CART);
  72. }
  73. /**
  74. * @return BaseOrder
  75. */
  76. public function getOrder(): ?BaseOrder
  77. {
  78. if ($this->order !== null) {
  79. return $this->order;
  80. }
  81. $json = $this->session->get(static::ORDER);
  82. if (null !== $this->order = $this->serializer->deserialize($json)) {
  83. return $this->order;
  84. }
  85. return $this->order;
  86. }
  87. /**
  88. * @return CartModel
  89. */
  90. public function getCart(): CartModel
  91. {
  92. if ($this->cart !== null) {
  93. return $this->cart;
  94. }
  95. $json = $this->session->get(static::CART);
  96. if (null === $this->cart = $this->serializer->deserialize($json)) {
  97. $class = static::getCartModelClass();
  98. $this->cart = new $class();
  99. }
  100. return $this->cart;
  101. }
  102. /**
  103. * @return void
  104. */
  105. public function save()
  106. {
  107. $orderJson = $this->serializer->serialize($this->order);
  108. $cartJson = $this->serializer->serialize($this->cart);
  109. $this->session->set(static::ORDER, $orderJson);
  110. $this->session->set(static::CART, $cartJson);
  111. }
  112. /**
  113. * @param BaseOrder $order
  114. * @return void
  115. */
  116. public function setOrder(?BaseOrder $order): void
  117. {
  118. $this->order = $order;
  119. }
  120. /**
  121. * @return ArrayCollection
  122. */
  123. public function getItems(): ArrayCollection
  124. {
  125. return $this->getCart()->getItems();
  126. }
  127. /**
  128. * @param BaseOrderItem $item
  129. * @return void
  130. */
  131. public function add(BaseOrderItem $item): void
  132. {
  133. $this->getCart()->add($item);
  134. }
  135. /**
  136. * @return bool
  137. */
  138. public function isEmpty(): bool
  139. {
  140. return $this->getCart()->getItems()->count() === 0;
  141. }
  142. /**
  143. * @param string $hash
  144. * @param int $amount
  145. * @return void
  146. */
  147. public function updateItemAmount(string $hash, int $amount): void
  148. {
  149. $this->getCart()->updateItemAmount($hash, $amount);
  150. $this->save();
  151. }
  152. /**
  153. * @param string $hash
  154. * @return void
  155. */
  156. public function removeItem(string $hash): void
  157. {
  158. $this->getCart()->removeItem($hash);
  159. $this->save();
  160. }
  161. /**
  162. * @param string $hash
  163. * @return BaseOrderItem|null
  164. */
  165. public function getItem(string $hash): ?BaseOrderItem
  166. {
  167. return $this->getCart()->getItemByHash($hash);
  168. }
  169. /**
  170. * @return int
  171. */
  172. public function getAmount(): int
  173. {
  174. return $this->getCart()->getAmount();
  175. }
  176. /**
  177. * @return float
  178. */
  179. public function getSubtotal(): float
  180. {
  181. return $this->getCart()->getSubtotal();
  182. }
  183. /**
  184. * @return null|float
  185. */
  186. public function getShippingCosts(): ?float
  187. {
  188. if ($this->order === null) {
  189. return 0;
  190. }
  191. if (null === $shippingMethod = $this->shippingMethodProvider->get($this->order->getShippingMethod())) {
  192. return 0;
  193. }
  194. return $shippingMethod->getCosts();
  195. }
  196. /**
  197. * @return null|float
  198. */
  199. public function getDiscountPrice(): ?float
  200. {
  201. if ($this->order === null) {
  202. return 0;
  203. }
  204. if (null === $discountRegistry = $this->order->getDiscountRegistry()) {
  205. return 0;
  206. }
  207. if ($discountRegistry->isDonated()) {
  208. return 0;
  209. }
  210. return $discountRegistry->getAmount();
  211. }
  212. /**
  213. * @return null|float
  214. */
  215. public function getPaymentCosts(): ?float
  216. {
  217. if ($this->order === null) {
  218. return 0;
  219. }
  220. if (null === $paymentMethod = $this->paymentMethodProvider->get($this->order->getPaymentMethod())) {
  221. return 0;
  222. }
  223. return $paymentMethod->getCosts();
  224. }
  225. /**
  226. * @return float
  227. */
  228. public function getTotal(): float
  229. {
  230. $total = $this->getSubtotal();
  231. $total += $this->getShippingCosts();
  232. $total += $this->getPaymentCosts();
  233. $total -= $this->getDiscountPrice();
  234. return $total;
  235. }
  236. }