src/Domain/Notification/Model/Notification.php line 16

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Notification\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Ramsey\Uuid\Uuid;
  6. use Whater\Domain\Common\Exception\InvalidUUIDException;
  7. use Whater\Domain\Notification\Event\NotificationWasCreated;
  8. use Whater\Domain\User\Model\User;
  9. /**
  10. *
  11. * @package Whater\Domain\Notification\Model
  12. */
  13. class Notification implements ContainsRecordedEvents
  14. {
  15. use EventRecorderCapabilities;
  16. /**
  17. * @var string
  18. */
  19. private $uuid;
  20. /**
  21. * @var User
  22. */
  23. private $user;
  24. /**
  25. * @var string
  26. */
  27. private $title;
  28. /**
  29. * @var string
  30. */
  31. private $message;
  32. /**
  33. * @var string
  34. */
  35. private $link;
  36. /**
  37. * @var string
  38. */
  39. private $checksum;
  40. /**
  41. * @var \DateTime
  42. */
  43. private $createdAt;
  44. /**
  45. * @var \DateTime
  46. */
  47. private $readAt;
  48. public function __construct(
  49. string $notificationId = null,
  50. User $user = null,
  51. string $message,
  52. string $title = null,
  53. string $link = null
  54. ) {
  55. try {
  56. $this->uuid = Uuid::fromString($notificationId ?: Uuid::uuid4())->toString();
  57. } catch (\InvalidArgumentException $e) {
  58. throw new InvalidUUIDException();
  59. }
  60. $this->user = $user;
  61. $this->message = $message;
  62. $this->title = $title;
  63. $this->link = $link;
  64. $this->createdAt = new \DateTime();
  65. //Calculate checksum
  66. $notificationDate = clone $this->createdAt();
  67. $notificationDate->setTimezone(new \DateTimeZone('UTC'));
  68. $hash = md5($notificationDate->format('dmYHis'));
  69. $hash = md5($hash . $this->title());
  70. $hash = md5($hash . $this->user());
  71. $this->checksum = $hash;
  72. $this->record(new NotificationWasCreated($this));
  73. }
  74. public function readNotification(): Notification
  75. {
  76. if ($this->readAt() == null) {
  77. $this->readAt = new \DateTime();
  78. }
  79. return $this;
  80. }
  81. public function unreadNotification(): Notification
  82. {
  83. $this->readAt = null;
  84. return $this;
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function id(): string
  90. {
  91. return $this->uuid;
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function message(): string
  97. {
  98. return $this->message;
  99. }
  100. /**
  101. * @return string|null
  102. */
  103. public function title(): ?string
  104. {
  105. return $this->title;
  106. }
  107. /**
  108. * @return string|null
  109. */
  110. public function link(): ?string
  111. {
  112. return $this->link;
  113. }
  114. /**
  115. * @return string|null
  116. */
  117. public function checksum(): ?string
  118. {
  119. return $this->checksum;
  120. }
  121. /**
  122. * @return User
  123. */
  124. public function user(): User
  125. {
  126. return $this->user;
  127. }
  128. /**
  129. * @return \DateTime
  130. */
  131. public function createdAt(): \DateTime
  132. {
  133. return $this->createdAt;
  134. }
  135. /**
  136. * @return \DateTime|null
  137. */
  138. public function readAt(): ?\DateTime
  139. {
  140. return $this->readAt;
  141. }
  142. }