src/Domain/Product/Model/WhatercoinCoupon.php line 16

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Product\Model;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Ramsey\Uuid\Uuid;
  6. use Whater\Domain\Common\Exception\InvalidUUIDException;
  7. use Whater\Domain\Product\Exception\InvalidCouponException;
  8. use Whater\Domain\Whater\Model\WhaterOrganization;
  9. /**
  10. *
  11. * @package Whater\Domain\Product\Model
  12. */
  13. class WhatercoinCoupon
  14. {
  15. const COUPON_DEFAULT_DAYS = 180;
  16. /**
  17. * @var string
  18. */
  19. protected $uuid;
  20. /**
  21. * @var string
  22. */
  23. private $code;
  24. /**
  25. * @var WhaterOrganization
  26. */
  27. private $whaterOrganization;
  28. /**
  29. * @var float
  30. */
  31. private $whatercoinsPerUser;
  32. /**
  33. * @var float
  34. */
  35. private $maxWhatercoins;
  36. /**
  37. * @var string
  38. */
  39. private $message;
  40. /**
  41. * @var Collection
  42. */
  43. private $users;
  44. /**
  45. * @var \DateTime
  46. */
  47. private $createdAt;
  48. /**
  49. * @var \DateTime
  50. */
  51. private $expiredAt;
  52. public function __construct(
  53. string $uuid = null,
  54. WhaterOrganization $whaterOrganization,
  55. string $code = null,
  56. string $message = null,
  57. float $maxWhatercoins,
  58. float $whatercoinsPerUser,
  59. \DateTime $expiredAt = null
  60. ) {
  61. try {
  62. $this->uuid = Uuid::fromString($uuid ?: Uuid::uuid4())->toString();
  63. } catch (\InvalidArgumentException $e) {
  64. throw new InvalidUUIDException();
  65. }
  66. $this->whaterOrganization = $whaterOrganization;
  67. $this->createdAt = new \DateTime();
  68. $this->users = new ArrayCollection();
  69. $this->message = $message;
  70. if (!is_null($code)) {
  71. $this->code = strtoupper($code);
  72. } else {
  73. $this->code = strtoupper(substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10));
  74. }
  75. if ($maxWhatercoins > 0) {
  76. $this->maxWhatercoins = $maxWhatercoins;
  77. } else {
  78. throw new InvalidCouponException();
  79. }
  80. if ($whatercoinsPerUser > 0) {
  81. $this->whatercoinsPerUser = $whatercoinsPerUser;
  82. } else if ($whatercoinsPerUser > $maxWhatercoins) {
  83. throw new InvalidCouponException();
  84. } else {
  85. throw new InvalidCouponException();
  86. }
  87. if (!is_null($expiredAt)) {
  88. $this->expiredAt = $expiredAt;
  89. } else {
  90. $this->expiredAt = clone $this->createdAt;
  91. $this->expiredAt->modify('+' . WhatercoinCoupon::COUPON_DEFAULT_DAYS . ' days');
  92. }
  93. }
  94. public function isValid(): bool
  95. {
  96. if (($this->users()->count() * $this->whatercoinsPerUser()) >= $this->maxWhatercoins()) {
  97. //check max number of uses
  98. return false;
  99. }
  100. $now = new \DateTime();
  101. if ($this->expiredAt() < $now) {
  102. //check expiration date
  103. return false;
  104. }
  105. return true;
  106. }
  107. public function updateWhatercoinCoupon(
  108. string $code = null,
  109. string $message = null,
  110. float $maxWhatercoins,
  111. float $whatercoinsPerUser,
  112. \DateTime $expiredAt = null
  113. ): WhatercoinCoupon {
  114. $this->code = strtoupper($code);
  115. if ($maxWhatercoins > 0) {
  116. $this->maxWhatercoins = $maxWhatercoins;
  117. } else {
  118. throw new InvalidCouponException();
  119. }
  120. if ($whatercoinsPerUser > 0) {
  121. $this->whatercoinsPerUser = $whatercoinsPerUser;
  122. } else if ($whatercoinsPerUser > $maxWhatercoins) {
  123. throw new InvalidCouponException();
  124. } else {
  125. throw new InvalidCouponException();
  126. }
  127. $this->message = $message;
  128. $today = new \DateTime();
  129. if ($expiredAt >= $today) {
  130. $this->expiredAt = $expiredAt;
  131. }
  132. return $this;
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function id(): string
  138. {
  139. return $this->uuid;
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function code(): string
  145. {
  146. return $this->code;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function message(): ?string
  152. {
  153. return $this->message;
  154. }
  155. /**
  156. * @return WhaterOrganization
  157. */
  158. public function whaterOrganization(): WhaterOrganization
  159. {
  160. return $this->whaterOrganization;
  161. }
  162. /**
  163. * @return float
  164. */
  165. public function whatercoinsPerUser(): float
  166. {
  167. return $this->whatercoinsPerUser;
  168. }
  169. /**
  170. * @return float
  171. */
  172. public function maxWhatercoins(): float
  173. {
  174. return $this->maxWhatercoins;
  175. }
  176. /**
  177. * @return Collection
  178. */
  179. public function users(): Collection
  180. {
  181. return $this->users;
  182. }
  183. /**
  184. * @return \DateTime
  185. */
  186. public function expiredAt(): \DateTime
  187. {
  188. return $this->expiredAt;
  189. }
  190. /**
  191. * @return \DateTime
  192. */
  193. public function createdAt(): \DateTime
  194. {
  195. return $this->createdAt;
  196. }
  197. }