src/Domain/Product/Model/ProductValoration.php line 19

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Product\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Ramsey\Uuid\Uuid;
  8. use Whater\Domain\Common\Exception\InvalidUUIDException;
  9. use Whater\Domain\Product\Model\Product;
  10. use Whater\Domain\User\Model\User;
  11. /**
  12. * Class ProductValoration
  13. *
  14. * @package Whater\Domain\Whater\Model
  15. */
  16. class ProductValoration implements ContainsRecordedEvents
  17. {
  18. use EventRecorderCapabilities;
  19. public const WP_MAX_VALORATION = 5;
  20. public const WP_MIN_VALORATION = 0;
  21. /**
  22. * @var string
  23. */
  24. private $uuid;
  25. /**
  26. * @var Product
  27. */
  28. private $product;
  29. /**
  30. * @var CartOrderItem
  31. */
  32. private $cartOrderItem;
  33. /**
  34. * @var User
  35. */
  36. private $createdBy;
  37. /**
  38. * @var \DateTime
  39. */
  40. private $createdAt;
  41. /**
  42. * @var string
  43. */
  44. private $rating;
  45. public function __construct(
  46. string $productValorationId = null,
  47. CartOrderItem $cartOrderItem,
  48. User $createdBy,
  49. string $rating
  50. ) {
  51. try {
  52. $this->uuid = Uuid::fromString($productValorationId ?: Uuid::uuid4())->toString();
  53. } catch (\InvalidArgumentException $e) {
  54. throw new InvalidUUIDException();
  55. }
  56. $this->cartOrderItem = $cartOrderItem;
  57. $this->product = $cartOrderItem->product();
  58. $this->createdBy = $createdBy;
  59. $this->createdAt = new \DateTime();
  60. $this->updateValoration(
  61. $rating
  62. );
  63. }
  64. public function updateValoration(
  65. string $rating = null
  66. ): ProductValoration {
  67. if (!$this->product()->valorations()->contains($this)) {
  68. $this->product()->valorations()->add($this);
  69. }
  70. if (!is_null($rating)) {
  71. $this->rating = $rating;
  72. }
  73. return $this;
  74. }
  75. /**
  76. * @return string
  77. */
  78. public function id(): string
  79. {
  80. return $this->uuid;
  81. }
  82. /**
  83. * @return ProducCartOrderItemt
  84. */
  85. public function cartOrderItem(): CartOrderItem
  86. {
  87. return $this->cartOrderItem;
  88. }
  89. /**
  90. * @return Product
  91. */
  92. public function product(): Product
  93. {
  94. return $this->product;
  95. }
  96. /**
  97. * @return User
  98. */
  99. public function createdBy(): User
  100. {
  101. return $this->createdBy;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function rating(): ?string
  107. {
  108. return $this->rating;
  109. }
  110. /**
  111. * @return \DateTime
  112. */
  113. public function createdAt(): \DateTime
  114. {
  115. return $this->createdAt;
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function equals(ProductValoration $productValoration)
  121. {
  122. return $this->id() === $productValoration->id();
  123. }
  124. }