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

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