src/Entity/Cart.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CartRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use DateTimeImmutable;
  8. #[ORM\Entity(repositoryClassCartRepository::class)]
  9. #[ORM\Table(name'`cart`')]
  10. class Cart
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'carts')]
  17.     #[ORM\JoinColumn(nullabletrue)]
  18.     private ?User $user null;
  19.     #[ORM\Column(length255nullabletrue)]
  20.     private ?string $sessionId null;
  21.     #[ORM\Column(type'decimal'precision10scale2nullabletrue)]
  22.     private ?string $totalAmount null;
  23.     #[ORM\Column]
  24.     private ?int $itemCount 0;
  25.     #[ORM\Column(nullabletrue)]
  26.     private ?DateTimeImmutable $lastActivity null;
  27.     #[ORM\Column]
  28.     private ?bool $isActive true;
  29.     #[ORM\Column(nullabletrue)]
  30.     private ?DateTimeImmutable $createdAt null;
  31.     #[ORM\Column(nullabletrue)]
  32.     private ?DateTimeImmutable $updatedAt null;
  33.     #[ORM\OneToMany(targetEntityCartItem::class, mappedBy'cart'cascade: ['persist''remove'])]
  34.     private Collection $items;
  35.     public function __construct()
  36.     {
  37.         $this->items = new ArrayCollection();
  38.         $this->createdAt = new DateTimeImmutable();
  39.         $this->updatedAt = new DateTimeImmutable();
  40.         $this->lastActivity = new DateTimeImmutable();
  41.         $this->isActive true;
  42.     }
  43.     public function getId(): ?int
  44.     {
  45.         return $this->id;
  46.     }
  47.     public function getUser(): ?User
  48.     {
  49.         return $this->user;
  50.     }
  51.     public function setUser(?User $user): self
  52.     {
  53.         $this->user $user;
  54.         return $this;
  55.     }
  56.     public function getSessionId(): ?string
  57.     {
  58.         return $this->sessionId;
  59.     }
  60.     public function setSessionId(?string $sessionId): self
  61.     {
  62.         $this->sessionId $sessionId;
  63.         return $this;
  64.     }
  65.     public function getTotalAmount(): ?string
  66.     {
  67.         return $this->totalAmount;
  68.     }
  69.     public function setTotalAmount(?string $totalAmount): self
  70.     {
  71.         $this->totalAmount $totalAmount;
  72.         return $this;
  73.     }
  74.     public function getItemCount(): ?int
  75.     {
  76.         return $this->itemCount;
  77.     }
  78.     public function setItemCount(int $itemCount): self
  79.     {
  80.         $this->itemCount $itemCount;
  81.         return $this;
  82.     }
  83.     public function getLastActivity(): ?DateTimeImmutable
  84.     {
  85.         return $this->lastActivity;
  86.     }
  87.     public function setLastActivity(DateTimeImmutable $lastActivity): self
  88.     {
  89.         $this->lastActivity $lastActivity;
  90.         return $this;
  91.     }
  92.     public function isActive(): ?bool
  93.     {
  94.         return $this->isActive;
  95.     }
  96.     public function setIsActive(bool $isActive): self
  97.     {
  98.         $this->isActive $isActive;
  99.         return $this;
  100.     }
  101.     public function getCreatedAt(): ?DateTimeImmutable
  102.     {
  103.         return $this->createdAt;
  104.     }
  105.     public function getUpdatedAt(): ?DateTimeImmutable
  106.     {
  107.         return $this->updatedAt;
  108.     }
  109.     public function setUpdatedAt(DateTimeImmutable $updatedAt): self
  110.     {
  111.         $this->updatedAt $updatedAt;
  112.         return $this;
  113.     }
  114.     /**
  115.      * @return Collection<int, CartItem>
  116.      */
  117.     public function getItems(): Collection
  118.     {
  119.         return $this->items;
  120.     }
  121.     public function addItem(CartItem $item): self
  122.     {
  123.         if (!$this->items->contains($item)) {
  124.             $this->items->add($item);
  125.             $item->setCart($this);
  126.         }
  127.         $this->updateTotals();
  128.         return $this;
  129.     }
  130.     public function removeItem(CartItem $item): self
  131.     {
  132.         if ($this->items->removeElement($item)) {
  133.             if ($item->getCart() === $this) {
  134.                 $item->setCart(null);
  135.             }
  136.         }
  137.         $this->updateTotals();
  138.         return $this;
  139.     }
  140.     public function updateTotals(): void
  141.     {
  142.         $totalAmount 0;
  143.         $itemCount 0;
  144.         foreach ($this->items as $item) {
  145.             $totalAmount += $item->getTotalPrice();
  146.             $itemCount += $item->getQuantity();
  147.         }
  148.         $this->totalAmount number_format($totalAmount2'.''');
  149.         $this->itemCount $itemCount;
  150.         $this->updatedAt = new DateTimeImmutable();
  151.         $this->lastActivity = new DateTimeImmutable();
  152.     }
  153.     public function clear(): self
  154.     {
  155.         $this->items->clear();
  156.         $this->updateTotals();
  157.         return $this;
  158.     }
  159.     public function hasProduct(Product $product): bool
  160.     {
  161.         foreach ($this->items as $item) {
  162.             if ($item->getProduct() === $product) {
  163.                 return true;
  164.             }
  165.         }
  166.         return false;
  167.     }
  168.     public function getItemByProduct(Product $product): ?CartItem
  169.     {
  170.         foreach ($this->items as $item) {
  171.             if ($item->getProduct() === $product) {
  172.                 return $item;
  173.             }
  174.         }
  175.         return null;
  176.     }
  177. }