lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Model/CartModel.php line 17

Open in your IDE?
  1. <?php
  2. /**
  3.  * Cart Model
  4.  *
  5.  * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6.  */
  7. namespace VisualMedia\OrderBundle\Model;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use VisualMedia\OrderBundle\Entity\BaseOrderItem;
  10. /**
  11.  * Cart Model
  12.  */
  13. class CartModel
  14. {
  15.     /**
  16.      * Items
  17.      * @var ArrayCollection
  18.      */
  19.     protected $items;
  20.     /**
  21.      * Amount
  22.      * @var float
  23.      */
  24.     protected $amount;
  25.     /**
  26.      * Subtotal
  27.      * @var float
  28.      */
  29.     protected $subtotal;
  30.     /**
  31.      * Shipping
  32.      * @var float
  33.      */
  34.     protected $shipping;
  35.     /**
  36.      * Payment
  37.      * @var float
  38.      */
  39.     protected $payment;
  40.     /**
  41.      * Total
  42.      * @var float
  43.      */
  44.     protected $total;
  45.     /**
  46.      * Constructor
  47.      */
  48.     public function __construct()
  49.     {
  50.         $this->items = new ArrayCollection();
  51.     }
  52.     /**
  53.      * Get Items
  54.      *
  55.      * @return ArrayCollection
  56.      */
  57.     public function getItems(): ArrayCollection
  58.     {
  59.         return $this->items;
  60.     }
  61.     /**
  62.      * Set Items
  63.      *
  64.      * @param ArrayCollection $items
  65.      *
  66.      * @return void
  67.      */
  68.     public function setItems(ArrayCollection $items): void
  69.     {
  70.         $this->items $items;
  71.     }
  72.     /**
  73.      * Add
  74.      *
  75.      * @param BaseOrderItem $item
  76.      *
  77.      * @return void
  78.      */
  79.     public function add(BaseOrderItem $item): void
  80.     {
  81.         if (null === $existing $this->contains($item)) {
  82.             $this->items->add($item);
  83.         }
  84.         else {
  85.             $existing->setAmount($existing->getAmount() + 1);
  86.         }
  87.     }
  88.     /**
  89.      * Contains
  90.      *
  91.      * @param BaseOrderItem $new
  92.      *
  93.      * @return BaseOrderItem
  94.      */
  95.     public function contains(BaseOrderItem $new): ?BaseOrderItem
  96.     {
  97.         $newHash $new->getItemHash();
  98.         foreach ($this->getItems() as $existing) {
  99.             if ($newHash === $existingHash $existing->getItemHash()) {
  100.                 return $existing;
  101.             }
  102.         }
  103.         return null;
  104.     }
  105.     /**
  106.      * Update Item Amount
  107.      *
  108.      * @param string $hash
  109.      * @param int $amount
  110.      *
  111.      * @return void
  112.      */
  113.     public function updateItemAmount(string $hashint $amount): void
  114.     {
  115.         foreach ($this->getItems() as $item) {
  116.             if ($item->getItemHash() === $hash) {
  117.                 $item->setAmount($amount);
  118.             }
  119.         }
  120.     }
  121.     /**
  122.      * Remove Item
  123.      *
  124.      * @param string $hash
  125.      *
  126.      * @return void
  127.      */
  128.     public function removeItem(string $hash): void
  129.     {
  130.         foreach ($this->getItems() as $item) {
  131.             if ($item->getItemHash() === $hash) {
  132.                 $this->getItems()->removeElement($item);
  133.             }
  134.         }
  135.     }
  136.     /**
  137.      * Getc Item By Hash
  138.      *
  139.      * @param string $hash
  140.      *
  141.      * @return BaseOrderItem|null
  142.      */
  143.     public function getItemByHash(string $hash): ?BaseOrderItem
  144.     {
  145.         foreach ($this->getItems() as $item) {
  146.             if ($item->getItemHash() === $hash) {
  147.                 return $item;
  148.             }
  149.         }
  150.         return null;
  151.     }
  152.     /**
  153.      * Get Amount
  154.      *
  155.      * @return int
  156.      */
  157.     public function getAmount(): int
  158.     {
  159.         $amount 0;
  160.         foreach ($this->getItems() as $item) {
  161.             $amount += $item->getAmount();
  162.         }
  163.         return $amount;
  164.     }
  165.     /**
  166.      * @return float
  167.      */
  168.     public function getSubtotal(): float
  169.     {
  170.         $this->subtotal 0;
  171.         foreach ($this->getItems() as $item) {
  172.             if ($item->isFree()) {
  173.                 continue;
  174.             }
  175.             $this->subtotal += $item->getTotal();
  176.         }
  177.         return $this->subtotal;
  178.     }
  179. }