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

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