src/Domain/User/Model/UserNfc.php line 17

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\User\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\User\Exception\InvalidNfcClassException;
  8. use Whater\Domain\Whater\Model\WhaterOrganization;
  9. /**
  10. * Class UserNfc
  11. *
  12. * @package Whater\Domain\User\Model
  13. */
  14. class UserNfc
  15. {
  16. public const NFC_CLASS_BOTTLE = 'NFC_CLASS_BOTTLE';
  17. public const NFC_CLASS_BRACELET = 'NFC_CLASS_BRACELET';
  18. public const NFC_CLASS_SMARTPHONE = 'NFC_CLASS_SMARTPHONE';
  19. public const NFC_CLASS_SMARTWATCH = 'NFC_CLASS_SMARTWATCH';
  20. public const NFC_CLASS_CARD = 'NFC_CLASS_CARD';
  21. /**
  22. * @var string
  23. */
  24. private $uuid;
  25. /**
  26. * @var string
  27. */
  28. private $nfcId;
  29. /**
  30. * @var string
  31. */
  32. private $nfcLabel;
  33. /**
  34. * @var string
  35. */
  36. private $nfcClass;
  37. /**
  38. * @var WhaterOrganization
  39. */
  40. private $whaterOrganization;
  41. /**
  42. * @var Collection
  43. */
  44. private $whaterRefills;
  45. /**
  46. * @var User
  47. */
  48. private $user;
  49. /**
  50. * @var \DateTime
  51. */
  52. private $createdAt;
  53. /**
  54. * @var \DateTime
  55. */
  56. private $updatedAt;
  57. public function __construct(
  58. ?string $userNfcId = null,
  59. WhaterOrganization $whaterOrganization,
  60. User $user,
  61. string $nfcId,
  62. string $nfcClass = UserNfc::NFC_CLASS_BOTTLE
  63. ) {
  64. try {
  65. $this->uuid = Uuid::fromString($userNfcId ?: Uuid::uuid4())->toString();
  66. } catch (\InvalidArgumentException $e) {
  67. throw new InvalidUUIDException();
  68. }
  69. $this->whaterOrganization = $whaterOrganization;
  70. $this->user = $user;
  71. $this->nfcId = $nfcId;
  72. switch ($nfcClass) {
  73. case UserNfc::NFC_CLASS_BOTTLE:
  74. case UserNfc::NFC_CLASS_BRACELET:
  75. case UserNfc::NFC_CLASS_CARD:
  76. case UserNfc::NFC_CLASS_SMARTWATCH:
  77. case UserNfc::NFC_CLASS_SMARTPHONE:
  78. $this->nfcClass = $nfcClass;
  79. break;
  80. default:
  81. throw new InvalidNfcClassException();
  82. }
  83. $this->whaterRefills = new ArrayCollection();
  84. $this->createdAt = new \DateTime();
  85. $this->updatedAt = $this->createdAt;
  86. }
  87. public function updateNfc(
  88. string $nfcId,
  89. string $nfcClass,
  90. ?string $nfcLabel = null,
  91. ?User $user = null,
  92. ?WhaterOrganization $whaterOrganization = null
  93. ) {
  94. $this->nfcId = $nfcId;
  95. $this->nfcClass = $nfcClass;
  96. $this->nfcLabel = $nfcLabel;
  97. if ($user != null) {
  98. $this->user = $user;
  99. }
  100. if ($whaterOrganization != null) {
  101. $this->whaterOrganization = $whaterOrganization;
  102. }
  103. $this->updatedAt = new \DateTime();
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function id(): string
  109. {
  110. return $this->uuid;
  111. }
  112. /**
  113. * @return string
  114. */
  115. public function nfcId(): string
  116. {
  117. return $this->nfcId;
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function nfcClass(): string
  123. {
  124. return $this->nfcClass;
  125. }
  126. /**
  127. * @return WhaterOrganization
  128. */
  129. public function whaterOrganization(): WhaterOrganization
  130. {
  131. return $this->whaterOrganization;
  132. }
  133. /**
  134. * @return User
  135. */
  136. public function user(): User
  137. {
  138. return $this->user;
  139. }
  140. /**
  141. * @return Collection
  142. */
  143. public function whaterRefills(): Collection
  144. {
  145. return $this->whaterRefills;
  146. }
  147. /**
  148. * @return string
  149. */
  150. public function nfcLabel(): ?string
  151. {
  152. return $this->nfcLabel;
  153. }
  154. /**
  155. * @return \DateTime
  156. */
  157. public function createdAt(): \DateTime
  158. {
  159. return $this->createdAt;
  160. }
  161. /**
  162. * @return \DateTime
  163. */
  164. public function updatedAt(): \DateTime
  165. {
  166. return $this->updatedAt;
  167. }
  168. }