src/Order/Entity/Cart.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Order\Entity;
  3. use App\Customer\Entity\BillingAddress;
  4. use App\Customer\Entity\Customer;
  5. use App\Customer\Entity\StreetAddress;
  6. use App\Delivery\Entity\DeliverySchedule;
  7. use App\Framework\Entity\BlameableEntityTrait;
  8. use App\Framework\Entity\TimestampableEntityTrait;
  9. use App\Product\Entity\Product;
  10. use DateTime;
  11. use DateTimeInterface;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. /**
  16.  * @ORM\Entity
  17.  * @ORM\Table
  18.  */
  19. class Cart
  20. {
  21.     use BlameableEntityTrait;
  22.     use TimestampableEntityTrait;
  23.     /**
  24.      * @ORM\Column(type="integer")
  25.      * @ORM\Id
  26.      * @ORM\GeneratedValue(strategy="AUTO")
  27.      */
  28.     private int $id;
  29.     /**
  30.      * @ORM\Column(type="string", length=100, options={"default" : "normal"})
  31.      */
  32.     private string $orderType;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity="App\Customer\Entity\Customer")
  35.      */
  36.     protected ?Customer $customer null;
  37.     /**
  38.      * @ORM\ManyToOne(targetEntity="App\Customer\Entity\StreetAddress")
  39.      */
  40.     protected ?StreetAddress $shippingAddress null;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="App\Customer\Entity\BillingAddress")
  43.      */
  44.     protected ?BillingAddress $billingAddress null;
  45.     /**
  46.      * @ORM\ManyToOne(targetEntity="App\Delivery\Entity\DeliverySchedule")
  47.      */
  48.     protected ?DeliverySchedule $deliverySchedule null;
  49.     /**
  50.      * @ORM\Column(type="datetime", nullable=true)
  51.      */
  52.     protected ?DateTime $deliveryDate null;
  53.     /**
  54.      * @ORM\Column(type="time", nullable=true)
  55.      */
  56.     private ?DateTime $deliveryStartTime null;
  57.     /**
  58.      * @ORM\Column(type="time", nullable=true)
  59.      */
  60.     private ?DateTime $deliveryEndTime null;
  61.     /**
  62.      * @ORM\OneToMany(targetEntity="App\Order\Entity\CartItem", mappedBy="cart", fetch="EAGER", cascade={"persist", "remove"}, orphanRemoval=true)
  63.      **/
  64.     private Collection $items;
  65.     /**
  66.      * @ORM\ManyToOne(targetEntity="App\Product\Entity\Product")
  67.      */
  68.     private ?Product $shippingProduct null;
  69.     public function __construct()
  70.     {
  71.         $this->items = new ArrayCollection();
  72.         $this->orderType 'normal';
  73.     }
  74.     public function getId(): int
  75.     {
  76.         return $this->id;
  77.     }
  78.     public function getOrderType(): string
  79.     {
  80.         return $this->orderType;
  81.     }
  82.     public function setOrderType(string $type): void
  83.     {
  84.         $this->orderType $type;
  85.     }
  86.     /**
  87.      * @return CartItem[]
  88.      */
  89.     public function getItems(): array
  90.     {
  91.         return array_values($this->items->toArray());
  92.     }
  93.     /**
  94.      * @param CartItem[] $items
  95.      */
  96.     public function setItems(array $items): void
  97.     {
  98.         $this->items $items;
  99.     }
  100.     public function addItem(CartItem $item): static
  101.     {
  102.         $this->items[] = $item;
  103.         $item->setCart($this);
  104.         return $this;
  105.     }
  106.     /**
  107.      * @return mixed|null
  108.      */
  109.     public function getItem(Product $product)
  110.     {
  111.         foreach ($this->items as $i) {
  112.             if ($i->getProduct()->getId() === $product->getId()) {
  113.                 return $i;
  114.             }
  115.         }
  116.         return null;
  117.     }
  118.     public function removeItem(CartItem $item): void
  119.     {
  120.         $this->items->removeElement($item);
  121.     }
  122.     public function getCustomer(): ?Customer
  123.     {
  124.         return $this->customer;
  125.     }
  126.     public function setCustomer(?Customer $customer): void
  127.     {
  128.         $this->customer $customer;
  129.     }
  130.     public function getShippingAddress(): ?StreetAddress
  131.     {
  132.         return $this->shippingAddress;
  133.     }
  134.     public function setShippingAddress(?StreetAddress $address): void
  135.     {
  136.         $this->shippingAddress $address;
  137.     }
  138.     public function getBillingAddress(): ?BillingAddress
  139.     {
  140.         return $this->billingAddress;
  141.     }
  142.     public function setBillingAddress(?BillingAddress $billingAddress): void
  143.     {
  144.         $this->billingAddress $billingAddress;
  145.     }
  146.     public function getDeliverySchedule(): ?DeliverySchedule
  147.     {
  148.         return $this->deliverySchedule;
  149.     }
  150.     public function setDeliverySchedule(?DeliverySchedule $deliverySchedule): void
  151.     {
  152.         $this->deliverySchedule $deliverySchedule;
  153.     }
  154.     public function getDeliveryDate(): ?DateTime
  155.     {
  156.         return $this->deliveryDate;
  157.     }
  158.     /**
  159.      * @param class-string<DateTime>|null $deliveryDate
  160.      */
  161.     public function setDeliveryDate(?DateTime $deliveryDate): void
  162.     {
  163.         $this->deliveryDate $deliveryDate;
  164.     }
  165.     public function getDeliveryStartTime(): ?DateTimeInterface
  166.     {
  167.         return $this->deliveryStartTime;
  168.     }
  169.     /**
  170.      * @param class-string<DateTime>|null $start
  171.      */
  172.     public function setDeliveryStartTime(?DateTime $start): void
  173.     {
  174.         $this->deliveryStartTime $start;
  175.     }
  176.     public function getDeliveryEndTime(): ?DateTimeInterface
  177.     {
  178.         return $this->deliveryEndTime;
  179.     }
  180.     /**
  181.      * @param class-string<DateTime>|null $end
  182.      */
  183.     public function setDeliveryEndTime(?DateTime $end): void
  184.     {
  185.         $this->deliveryEndTime $end;
  186.     }
  187.     public function getShippingCartProduct(): ?Product
  188.     {
  189.         return $this->shippingProduct;
  190.     }
  191.     public function setShippingCartProduct(?Product $shippingProduct): void
  192.     {
  193.         $this->shippingProduct $shippingProduct;
  194.     }
  195.     public function getSubtotal(): float
  196.     {
  197.         $total 0;
  198.         foreach ($this->items as $item) {
  199.             $total += $item->getQuantity() * $item->getProduct()->getPrice();
  200.         }
  201.         return $total;
  202.     }
  203.     public function getProductsSubtotal(): float
  204.     {
  205.         $total 0;
  206.         foreach ($this->items as $item) {
  207.             if ($item->getProduct()->getType() !== 'product') {
  208.                 continue;
  209.             }
  210.             $total += $item->getQuantity() * $item->getProduct()->getPrice();
  211.         }
  212.         return $total;
  213.     }
  214.     public function getShippingCost(): float
  215.     {
  216.         return $this->shippingProduct !== null $this->shippingProduct->getPrice() : 0;
  217.     }
  218. }