<?php
namespace App\Entity;
use App\Repository\CartRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use DateTimeImmutable;
#[ORM\Entity(repositoryClass: CartRepository::class)]
#[ORM\Table(name: '`cart`')]
class Cart
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'carts')]
#[ORM\JoinColumn(nullable: true)]
private ?User $user = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $sessionId = null;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable: true)]
private ?string $totalAmount = null;
#[ORM\Column]
private ?int $itemCount = 0;
#[ORM\Column(nullable: true)]
private ?DateTimeImmutable $lastActivity = null;
#[ORM\Column]
private ?bool $isActive = true;
#[ORM\Column(nullable: true)]
private ?DateTimeImmutable $createdAt = null;
#[ORM\Column(nullable: true)]
private ?DateTimeImmutable $updatedAt = null;
#[ORM\OneToMany(targetEntity: CartItem::class, mappedBy: 'cart', cascade: ['persist', 'remove'])]
private Collection $items;
public function __construct()
{
$this->items = new ArrayCollection();
$this->createdAt = new DateTimeImmutable();
$this->updatedAt = new DateTimeImmutable();
$this->lastActivity = new DateTimeImmutable();
$this->isActive = true;
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getSessionId(): ?string
{
return $this->sessionId;
}
public function setSessionId(?string $sessionId): self
{
$this->sessionId = $sessionId;
return $this;
}
public function getTotalAmount(): ?string
{
return $this->totalAmount;
}
public function setTotalAmount(?string $totalAmount): self
{
$this->totalAmount = $totalAmount;
return $this;
}
public function getItemCount(): ?int
{
return $this->itemCount;
}
public function setItemCount(int $itemCount): self
{
$this->itemCount = $itemCount;
return $this;
}
public function getLastActivity(): ?DateTimeImmutable
{
return $this->lastActivity;
}
public function setLastActivity(DateTimeImmutable $lastActivity): self
{
$this->lastActivity = $lastActivity;
return $this;
}
public function isActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, CartItem>
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(CartItem $item): self
{
if (!$this->items->contains($item)) {
$this->items->add($item);
$item->setCart($this);
}
$this->updateTotals();
return $this;
}
public function removeItem(CartItem $item): self
{
if ($this->items->removeElement($item)) {
if ($item->getCart() === $this) {
$item->setCart(null);
}
}
$this->updateTotals();
return $this;
}
public function updateTotals(): void
{
$totalAmount = 0;
$itemCount = 0;
foreach ($this->items as $item) {
$totalAmount += $item->getTotalPrice();
$itemCount += $item->getQuantity();
}
$this->totalAmount = number_format($totalAmount, 2, '.', '');
$this->itemCount = $itemCount;
$this->updatedAt = new DateTimeImmutable();
$this->lastActivity = new DateTimeImmutable();
}
public function clear(): self
{
$this->items->clear();
$this->updateTotals();
return $this;
}
public function hasProduct(Product $product): bool
{
foreach ($this->items as $item) {
if ($item->getProduct() === $product) {
return true;
}
}
return false;
}
public function getItemByProduct(Product $product): ?CartItem
{
foreach ($this->items as $item) {
if ($item->getProduct() === $product) {
return $item;
}
}
return null;
}
}