vendor/roothirsch/tuer24-bundle/src/Entity/Tuer24Cart.php line 13

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\Tuer24Bundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Roothirsch\Tuer24Bundle\Repository\Tuer24CartRepository;
  7. #[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24CartRepository")]
  8. #[ORM\Table(name: 'roothirsch_tuer24_cart')]
  9. #[ORM\HasLifecycleCallbacks]
  10. class Tuer24Cart
  11. {
  12. #[ORM\Id]
  13. #[ORM\GeneratedValue]
  14. #[ORM\Column]
  15. private ?int $id = null;
  16. #[ORM\Column(length: 255)]
  17. private ?string $sessionId = null;
  18. #[ORM\Column(nullable: true)]
  19. private ?int $userId = null;
  20. #[ORM\Column(nullable: true)]
  21. private ?int $companyId = null;
  22. #[ORM\OneToMany(mappedBy: 'cart', targetEntity: Tuer24CartItem::class, orphanRemoval: true, cascade: ['persist', 'remove'])]
  23. private Collection $items;
  24. #[ORM\Column]
  25. private ?\DateTimeImmutable $createdAt = null;
  26. #[ORM\Column]
  27. private ?\DateTimeImmutable $updatedAt = null;
  28. public function __construct()
  29. {
  30. $this->items = new ArrayCollection();
  31. $this->createdAt = new \DateTimeImmutable();
  32. $this->updatedAt = new \DateTimeImmutable();
  33. }
  34. #[ORM\PreUpdate]
  35. public function preUpdate(): void
  36. {
  37. $this->updatedAt = new \DateTimeImmutable();
  38. }
  39. public function getId(): ?int
  40. {
  41. return $this->id;
  42. }
  43. public function getSessionId(): ?string
  44. {
  45. return $this->sessionId;
  46. }
  47. public function setSessionId(string $sessionId): self
  48. {
  49. $this->sessionId = $sessionId;
  50. return $this;
  51. }
  52. public function getUserId(): ?int
  53. {
  54. return $this->userId;
  55. }
  56. public function setUserId(?int $userId): self
  57. {
  58. $this->userId = $userId;
  59. return $this;
  60. }
  61. public function getCompanyId(): ?int
  62. {
  63. return $this->companyId;
  64. }
  65. public function setCompanyId(?int $companyId): self
  66. {
  67. $this->companyId = $companyId;
  68. return $this;
  69. }
  70. /**
  71. * @return Collection<int, Tuer24CartItem>
  72. */
  73. public function getItems(): Collection
  74. {
  75. return $this->items;
  76. }
  77. public function addItem(Tuer24CartItem $item): self
  78. {
  79. if (!$this->items->contains($item)) {
  80. $this->items->add($item);
  81. $item->setCart($this);
  82. }
  83. return $this;
  84. }
  85. public function removeItem(Tuer24CartItem $item): self
  86. {
  87. if ($this->items->removeElement($item)) {
  88. if ($item->getCart() === $this) {
  89. $item->setCart(null);
  90. }
  91. }
  92. return $this;
  93. }
  94. public function hasItem(int $productId): bool
  95. {
  96. foreach ($this->items as $item) {
  97. if ($item->getProductId() === $productId) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. public function getItem(int $productId): ?Tuer24CartItem
  104. {
  105. foreach ($this->items as $item) {
  106. if ($item->getProductId() === $productId) {
  107. return $item;
  108. }
  109. }
  110. return null;
  111. }
  112. public function clearItems(): self
  113. {
  114. $this->items->clear();
  115. return $this;
  116. }
  117. public function countItems(): int
  118. {
  119. return $this->items->count();
  120. }
  121. public function getTotalQuantity(): int
  122. {
  123. $quantity = 0;
  124. foreach ($this->items as $item) {
  125. $quantity += $item->getQuantity();
  126. }
  127. return $quantity;
  128. }
  129. public function getTotalPrice(): float
  130. {
  131. $total = 0.0;
  132. foreach ($this->items as $item) {
  133. $total += $item->getTotal();
  134. }
  135. return $total;
  136. }
  137. public function getCreatedAt(): ?\DateTimeImmutable
  138. {
  139. return $this->createdAt;
  140. }
  141. public function setCreatedAt(\DateTimeImmutable $createdAt): self
  142. {
  143. $this->createdAt = $createdAt;
  144. return $this;
  145. }
  146. public function getUpdatedAt(): ?\DateTimeImmutable
  147. {
  148. return $this->updatedAt;
  149. }
  150. public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  151. {
  152. $this->updatedAt = $updatedAt;
  153. return $this;
  154. }
  155. }