<?php
namespace Boab\EcommerceBundle\Entity;
use Boab\EcommerceBundle\Model\ItemInterface;
use Boab\EcommerceBundle\Repository\CartRepository;
use Boab\EcommerceBundle\Doctrine\CartSetItemsListener;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CartRepository::class)
* @ORM\EntityListeners({CartSetItemsListener::class})
*/
class Cart implements CartInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=6)
*/
private $uniqueId;
/**
* @ORM\Column(type="json")
*/
private $items = [];
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="string", length=10)
*/
private $status;
public function __construct(array $items=[])
{
$this->items = $items;
}
public function getId(): ?int
{
return $this->id;
}
public function getUniqueId(): ?string
{
return $this->uniqueId;
}
public function setUniqueId(string $uniqueId): self
{
$this->uniqueId = $uniqueId;
return $this;
}
public function getItems(): ?array
{
return $this->items;
}
public function setItems(array $items): self
{
$this->items = $items;
return $this;
}
public function setItem(ItemInterface $item)
{
$this->items[$item->getId()] = $item;
return $this;
}
public function removeItem(string $itemId)
{
unset($this->items[$itemId]);
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getItemsCount():int
{
return count($this->items);
}
public function getTotalAmount():int
{
$total = 0;
foreach($this->items as $key => $item){
$total = $total + $item->getAmount();
}
return $total;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function isEmpty():bool
{
return count($this->items) == 0;
}
public function updateItemLicense($itemId, $license, $price)
{
$this->items[$itemId]->setLicense($license);
$this->items[$itemId]->setPrice($price);
}
public function updateItem(string $itemId, int $qty)
{
$this->items[$itemId]->setQty($qty);
}
}