<?php/** * Cart * * @author Bertin van den Ham <b.vandenham@visualmedia.nl> * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl> */namespace VisualMedia\OrderBundle\Component;use Doctrine\Common\Collections\ArrayCollection;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpFoundation\Session\SessionInterface;use VisualMedia\DiscountBundle\Discount\DiscountFactoryInterface;use VisualMedia\DiscountBundle\Entity\Discount;use VisualMedia\DiscountBundle\Entity\DiscountRegistry;use VisualMedia\DiscountBundle\Service\DiscountProvider;use VisualMedia\LisaBundle\Component\SerializerInterface;use VisualMedia\OrderBundle\Component\Interfaces\CartInterface;use VisualMedia\OrderBundle\Entity\BaseOrder;use VisualMedia\OrderBundle\Entity\BaseOrderItem;use VisualMedia\OrderBundle\Model\CartModel;use VisualMedia\OrderBundle\Provider\ShippingMethodProvider;use VisualMedia\PaymentBundle\Provider\PaymentMethodProvider;/** * Cart */abstract class Cart implements CartInterface{ /** * @var SerializerInterface */ protected $serializer; /** * @var SessionInterface */ protected $session; /** * @var ShippingMethodProvider */ protected $shippingMethodProvider; /** * @var PaymentMethodProvider */ protected $paymentMethodProvider; /** * @var BaseOrder */ protected $order; /** * @var CartModel */ protected $cart; /** * @param SerializerInterface $serializer * @param SessionInterface $session * @param ShippingMethodProvider $shippingMethodProvider * @param PaymentMethodProvider $paymentMethodProvider */ public function __construct(SerializerInterface $serializer, SessionInterface $session, ShippingMethodProvider $shippingMethodProvider, PaymentMethodProvider $paymentMethodProvider) { $this->serializer = $serializer; $this->session = $session; $this->shippingMethodProvider = $shippingMethodProvider; $this->paymentMethodProvider = $paymentMethodProvider; } /** * @return void */ public function clear(): void { $this->session->remove(static::ORDER); $this->session->remove(static::CART); } /** * @return BaseOrder */ public function getOrder(): ?BaseOrder { if ($this->order !== null) { return $this->order; } $json = $this->session->get(static::ORDER); if (null !== $this->order = $this->serializer->deserialize($json)) { return $this->order; } return $this->order; } /** * @return CartModel */ public function getCart(): CartModel { if ($this->cart !== null) { return $this->cart; } $json = $this->session->get(static::CART); if (null === $this->cart = $this->serializer->deserialize($json)) { $class = static::getCartModelClass(); $this->cart = new $class(); } return $this->cart; } /** * @return void */ public function save() { $orderJson = $this->serializer->serialize($this->order); $cartJson = $this->serializer->serialize($this->cart); $this->session->set(static::ORDER, $orderJson); $this->session->set(static::CART, $cartJson); } /** * @param BaseOrder $order * @return void */ public function setOrder(?BaseOrder $order): void { $this->order = $order; } /** * @return ArrayCollection */ public function getItems(): ArrayCollection { return $this->getCart()->getItems(); } /** * @param BaseOrderItem $item * @return void */ public function add(BaseOrderItem $item): void { $this->getCart()->add($item); } /** * @return bool */ public function isEmpty(): bool { return $this->getCart()->getItems()->count() === 0; } /** * @param string $hash * @param int $amount * @return void */ public function updateItemAmount(string $hash, int $amount): void { $this->getCart()->updateItemAmount($hash, $amount); $this->save(); } /** * @param string $hash * @return void */ public function removeItem(string $hash): void { $this->getCart()->removeItem($hash); $this->save(); } /** * @param string $hash * @return BaseOrderItem|null */ public function getItem(string $hash): ?BaseOrderItem { return $this->getCart()->getItemByHash($hash); } /** * @return int */ public function getAmount(): int { return $this->getCart()->getAmount(); } /** * @return float */ public function getSubtotal(): float { return $this->getCart()->getSubtotal(); } /** * @return null|float */ public function getShippingCosts(): ?float { if ($this->order === null) { return 0; } if (null === $shippingMethod = $this->shippingMethodProvider->get($this->order->getShippingMethod())) { return 0; } return $shippingMethod->getCosts(); } /** * @return null|float */ public function getDiscountPrice(): ?float { if ($this->order === null) { return 0; } if (null === $discountRegistry = $this->order->getDiscountRegistry()) { return 0; } if ($discountRegistry->isDonated()) { return 0; } return $discountRegistry->getAmount(); } /** * @return null|float */ public function getPaymentCosts(): ?float { if ($this->order === null) { return 0; } if (null === $paymentMethod = $this->paymentMethodProvider->get($this->order->getPaymentMethod())) { return 0; } return $paymentMethod->getCosts(); } /** * @return float */ public function getTotal(): float { $total = $this->getSubtotal(); $total += $this->getShippingCosts(); $total += $this->getPaymentCosts(); $total -= $this->getDiscountPrice(); return $total; }}