src/Domain/Product/Model/CartOrderItem.php line 14

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Product\Model;
  3. use Ramsey\Uuid\Uuid;
  4. use Whater\Domain\Common\Exception\InvalidUUIDException;
  5. use Whater\Domain\Product\Exception\InvalidOrderItemUpdateException;
  6. /**
  7. * Class CartOrderItem
  8. *
  9. * @package Whater\Domain\Product\Model
  10. */
  11. class CartOrderItem
  12. {
  13. /**
  14. * @var string
  15. */
  16. private $uuid;
  17. /**
  18. * @var CartOrder
  19. */
  20. private $cartOrder;
  21. /**
  22. * @var CartOrderPackage
  23. */
  24. private $cartOrderPackage;
  25. /**
  26. * @var Product
  27. */
  28. private $product;
  29. /**
  30. * @var string
  31. */
  32. private $itemDescription;
  33. /**
  34. * @var float
  35. */
  36. private $pricePerUnit;
  37. /**
  38. * @var float
  39. */
  40. private $taxesPercent;
  41. /**
  42. * @var int
  43. */
  44. private $units;
  45. /**
  46. * @var array
  47. */
  48. private $meta;
  49. /**
  50. * @var int
  51. */
  52. private $lineOrder;
  53. /**
  54. * @var ProductVariation
  55. */
  56. private $productVariation;
  57. /**
  58. * @var ProductValoration
  59. */
  60. private $productValoration;
  61. /**
  62. * @var \DateTime
  63. */
  64. private $createdAt;
  65. /**
  66. * @var null|\DateTime
  67. */
  68. private $updatedAt;
  69. /**
  70. * OrderItem constructor.
  71. */
  72. public function __construct(
  73. ?string $cartOrderItemId = null,
  74. CartOrder $cartOrder,
  75. Product $product,
  76. float $pricePerUnit,
  77. float $taxesPercent,
  78. int $units,
  79. ?string $itemDescription = null,
  80. int $lineOrder = 1,
  81. ?ProductVariation $productVariation = null
  82. ) {
  83. try {
  84. $this->uuid = Uuid::fromString($cartOrderItemId ?: Uuid::uuid4())->toString();
  85. } catch (\InvalidArgumentException $e) {
  86. throw new InvalidUUIDException();
  87. }
  88. $this->cartOrder = $cartOrder;
  89. $this->product = $product;
  90. $this->pricePerUnit = $pricePerUnit;
  91. $this->taxesPercent = $taxesPercent;
  92. $this->units = $units;
  93. $this->lineOrder = $lineOrder;
  94. $this->itemDescription = $itemDescription;
  95. $this->meta = [];
  96. $this->productVariation = $productVariation;
  97. $this->createdAt = new \DateTime();
  98. $this->updatedAt = new \DateTime();
  99. }
  100. public function updatePrice(
  101. float $pricePerUnit,
  102. float $taxesPercent,
  103. int $units
  104. ) {
  105. if ($this->cartOrder()->status() == CartOrder::ORDER_STATUS_PENDING) {
  106. $this->pricePerUnit = $pricePerUnit;
  107. $this->taxesPercent = $taxesPercent;
  108. $this->units = $units;
  109. $this->updatedAt = new \DateTime();
  110. } else {
  111. throw new InvalidOrderItemUpdateException();
  112. }
  113. return $this;
  114. }
  115. public function updateMeta(
  116. array $meta
  117. ) {
  118. $this->meta = $meta;
  119. $this->updatedAt = new \DateTime();
  120. }
  121. public function updateDescription(
  122. ?string $itemDescription = null
  123. ) {
  124. $this->itemDescription = $itemDescription;
  125. $this->updatedAt = new \DateTime();
  126. return $this;
  127. }
  128. public function updateCartOrderPackage(
  129. ?CartOrderPackage $cartOrderPackage = null
  130. ) {
  131. $this->cartOrderPackage = $cartOrderPackage;
  132. }
  133. /**
  134. * @return string
  135. */
  136. public function id(): string
  137. {
  138. return $this->uuid;
  139. }
  140. /**
  141. * @return CartOrder
  142. */
  143. public function cartOrder(): CartOrder
  144. {
  145. return $this->cartOrder;
  146. }
  147. /**
  148. * @return Product
  149. */
  150. public function product(): Product
  151. {
  152. return $this->product;
  153. }
  154. /**
  155. * @return ProductValoration
  156. */
  157. public function productValoration(): ?ProductValoration
  158. {
  159. return $this->productValoration;
  160. }
  161. /**
  162. * @return CartOrderPackage
  163. */
  164. public function cartOrderPackage(): CartOrderPackage
  165. {
  166. return $this->cartOrderPackage;
  167. }
  168. /**
  169. * @return float
  170. */
  171. public function pricePerUnit(): float
  172. {
  173. return $this->pricePerUnit;
  174. }
  175. /**
  176. * @return float
  177. */
  178. public function taxesPercent(): float
  179. {
  180. return $this->taxesPercent;
  181. }
  182. /**
  183. * @return float
  184. */
  185. public function itemDescription(): string
  186. {
  187. return $this->itemDescription;
  188. }
  189. /**
  190. * @return int
  191. */
  192. public function lineOrder(): int
  193. {
  194. return $this->lineOrder;
  195. }
  196. /**
  197. * @return int
  198. */
  199. public function units(): int
  200. {
  201. return $this->units;
  202. }
  203. /**
  204. * @return array
  205. */
  206. public function meta(): ?array
  207. {
  208. return $this->meta;
  209. }
  210. /**
  211. * @return ProductVariation
  212. */
  213. public function productVariation(): ?ProductVariation
  214. {
  215. return $this->productVariation;
  216. }
  217. /**
  218. * @return \DateTime
  219. */
  220. public function createdAt(): \DateTime
  221. {
  222. return $this->createdAt;
  223. }
  224. /**
  225. * @return \DateTime|null
  226. */
  227. public function updatedAt()
  228. {
  229. return $this->updatedAt;
  230. }
  231. /**
  232. * @return bool
  233. */
  234. public function equals(CartOrderItem $cartOrderItem)
  235. {
  236. return $this->id() === $cartOrderItem->id();
  237. }
  238. }