src/App/Entity/MobileApp/PushNotification.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Entity\MobileApp;
  3. use App\Entity\ConversationMessage\ConversationMessage;
  4. use App\Entity\User;
  5. use App\Service\MobileAppPushNotificationService;
  6. use App\Utility\ClassChecker;
  7. use App\Utility\DateTimeUtility;
  8. use App\Utility\ReflectionHelper;
  9. use DateTime;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Exception;
  13. use InvalidArgumentException;
  14. use JanusHercules\Jobradar\Domain\Entity\JobradarMatch;
  15. /**
  16. * @ORM\Entity
  17. *
  18. * @ORM\Table(
  19. * name="mobile_app_push_notifications",
  20. * indexes={
  21. *
  22. * @ORM\Index(name="sent_at", columns={"sent_at"})
  23. * }
  24. * )
  25. */
  26. class PushNotification
  27. {
  28. public const EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE = 'conversationMessages.receivedNewJobradarMessage';
  29. public const EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE = 'conversationMessages.receivedNewProfileMessage';
  30. public const EVENT_NAME_REDIRECT_TO_WEB = 'redirectToWeb';
  31. public const EVENT_NAME_RECEIVED_NEW_JOBRADAR_MATCH = 'receivedNewJobradarMatch';
  32. public const TYPE_TO_EVENT_NAME = [
  33. MobileAppPushNotificationService::TYPE_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE => self::EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE,
  34. MobileAppPushNotificationService::TYPE_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE => self::EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE,
  35. MobileAppPushNotificationService::TYPE_REDIRECT_TO_WEB => self::EVENT_NAME_REDIRECT_TO_WEB,
  36. MobileAppPushNotificationService::TYPE_RECEIVED_NEW_JOBRADAR_MATCH => self::EVENT_NAME_RECEIVED_NEW_JOBRADAR_MATCH
  37. ];
  38. /**
  39. * @param ConversationMessage[]|array $relatedConversationMessages
  40. * @param JobradarMatch[]|array $relatedJobradarMatches
  41. *
  42. * @throws Exception
  43. */
  44. public function __construct(
  45. User $user,
  46. int $type,
  47. string $title,
  48. string $body,
  49. array $relatedConversationMessages,
  50. array $relatedJobradarMatches,
  51. array $eventData
  52. ) {
  53. ReflectionHelper::hasConstWithValue(MobileAppPushNotificationService::class, 'TYPE_', $type);
  54. $this->id = uuid_create(UUID_TYPE_RANDOM);
  55. $this->user = $user;
  56. $this->type = $type;
  57. $this->eventName = self::TYPE_TO_EVENT_NAME[$type];
  58. if ($type === MobileAppPushNotificationService::TYPE_REDIRECT_TO_WEB) {
  59. if (!array_key_exists('url', $eventData)) {
  60. throw new InvalidArgumentException('Push notification of type "redirectToWeb" requires eventData field "url".');
  61. }
  62. }
  63. $this->title = $title;
  64. $this->body = $body;
  65. ClassChecker::checkAllArrayElementsForOneOfClasses(
  66. $relatedConversationMessages,
  67. [
  68. ConversationMessage::class,
  69. 'Proxies\__CG__\\' . ConversationMessage::class
  70. ]
  71. );
  72. $this->relatedConversationMessages = $relatedConversationMessages;
  73. ClassChecker::checkAllArrayElementsForOneOfClasses(
  74. $relatedJobradarMatches,
  75. [
  76. JobradarMatch::class,
  77. 'Proxies\__CG__\\' . JobradarMatch::class
  78. ]
  79. );
  80. $this->relatedJobradarMatches = $relatedJobradarMatches;
  81. $this->createdAt = DateTimeUtility::createDateTimeUtc();
  82. $this->eventData = json_encode($eventData, JSON_UNESCAPED_SLASHES);
  83. }
  84. /**
  85. * @ORM\GeneratedValue(strategy="CUSTOM")
  86. *
  87. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  88. *
  89. * @ORM\Column(name="id", type="guid")
  90. *
  91. * @ORM\Id
  92. */
  93. private string $id;
  94. public function getId(): string
  95. {
  96. return $this->id;
  97. }
  98. /**
  99. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  100. *
  101. * @ORM\JoinColumn(name="users_id", onDelete="CASCADE", nullable=false)
  102. */
  103. private User $user;
  104. public function getUser(): User
  105. {
  106. return $this->user;
  107. }
  108. public function setUser(User $user): void
  109. {
  110. $this->user = $user;
  111. }
  112. /**
  113. * @ORM\Column(name="type", type="integer", nullable=false)
  114. */
  115. private int $type;
  116. public function getType(): int
  117. {
  118. return $this->type;
  119. }
  120. /**
  121. * @ORM\Column(name="title", type="text", length=255, nullable=false)
  122. */
  123. private string $title;
  124. public function getTitle(): string
  125. {
  126. return $this->title;
  127. }
  128. /**
  129. * @ORM\Column(name="body", type="text", length=8192, nullable=false)
  130. */
  131. private string $body;
  132. public function getBody(): string
  133. {
  134. return $this->body;
  135. }
  136. /**
  137. * @ORM\Column(name="event_name", type="text", length=255, nullable=false)
  138. */
  139. private string $eventName;
  140. public function getEventName(): string
  141. {
  142. return $this->eventName;
  143. }
  144. /**
  145. * @ORM\Column(name="event_data", type="text", length=8192, nullable=false)
  146. */
  147. private string $eventData;
  148. public function getEventData(): string
  149. {
  150. return $this->eventData;
  151. }
  152. /**
  153. * @ORM\Column(name="created_at", type="datetime", nullable=false)
  154. */
  155. private DateTime $createdAt;
  156. /**
  157. * @ORM\Column(name="sent_at", type="datetime", nullable=true)
  158. */
  159. private ?DateTime $sentAt;
  160. public function setSentAt(?DateTime $sentAt): void
  161. {
  162. $this->sentAt = $sentAt;
  163. }
  164. /**
  165. * @ORM\Column(name="opened_outside_of_app_at", type="datetime", nullable=true)
  166. */
  167. private ?DateTime $openedOutsideOfAppAt;
  168. /**
  169. * @ORM\Column(name="received_inside_of_app_at", type="datetime", nullable=true)
  170. */
  171. private ?DateTime $receivedInsideOfAppAt;
  172. /**
  173. * @var ConversationMessage[]|Collection
  174. *
  175. * @ORM\ManyToMany(targetEntity="App\Entity\ConversationMessage\ConversationMessage", inversedBy="relatedPushNotifications", cascade={"persist"})
  176. *
  177. * @ORM\JoinTable(
  178. * name="conversation_messages_mobile_app_push_notifications",
  179. * joinColumns={
  180. *
  181. * @ORM\JoinColumn(name="mobile_app_push_notifications_id", referencedColumnName="id", onDelete="CASCADE")
  182. * },
  183. * inverseJoinColumns={
  184. * @ORM\JoinColumn(name="conversation_messages_id", referencedColumnName="id", onDelete="CASCADE")
  185. * }
  186. * )
  187. */
  188. private $relatedConversationMessages;
  189. /**
  190. * @var JobradarMatch[]|Collection
  191. *
  192. * @ORM\ManyToMany(targetEntity="JanusHercules\Jobradar\Domain\Entity\JobradarMatch", cascade={"persist"})
  193. *
  194. * @ORM\JoinTable(
  195. * name="jobradar_matches_mobile_app_push_notifications",
  196. * joinColumns={
  197. *
  198. * @ORM\JoinColumn(name="mobile_app_push_notifications_id", referencedColumnName="id", onDelete="CASCADE")
  199. * },
  200. * inverseJoinColumns={
  201. * @ORM\JoinColumn(name="jobradar_matches_id", referencedColumnName="id", onDelete="CASCADE")
  202. * }
  203. * )
  204. */
  205. private $relatedJobradarMatches;
  206. }