<?php
namespace App\Entity\MobileApp;
use App\Entity\ConversationMessage\ConversationMessage;
use App\Entity\User;
use App\Service\MobileAppPushNotificationService;
use App\Utility\ClassChecker;
use App\Utility\DateTimeUtility;
use App\Utility\ReflectionHelper;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use InvalidArgumentException;
use JanusHercules\Jobradar\Domain\Entity\JobradarMatch;
/**
* @ORM\Entity
*
* @ORM\Table(
* name="mobile_app_push_notifications",
* indexes={
*
* @ORM\Index(name="sent_at", columns={"sent_at"})
* }
* )
*/
class PushNotification
{
public const EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE = 'conversationMessages.receivedNewJobradarMessage';
public const EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE = 'conversationMessages.receivedNewProfileMessage';
public const EVENT_NAME_REDIRECT_TO_WEB = 'redirectToWeb';
public const EVENT_NAME_RECEIVED_NEW_JOBRADAR_MATCH = 'receivedNewJobradarMatch';
public const TYPE_TO_EVENT_NAME = [
MobileAppPushNotificationService::TYPE_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE => self::EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_JOBRADAR_MESSAGE,
MobileAppPushNotificationService::TYPE_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE => self::EVENT_NAME_CONVERSATION_MESSAGES_RECEIVED_NEW_PROFILE_MESSAGE,
MobileAppPushNotificationService::TYPE_REDIRECT_TO_WEB => self::EVENT_NAME_REDIRECT_TO_WEB,
MobileAppPushNotificationService::TYPE_RECEIVED_NEW_JOBRADAR_MATCH => self::EVENT_NAME_RECEIVED_NEW_JOBRADAR_MATCH
];
/**
* @param ConversationMessage[]|array $relatedConversationMessages
* @param JobradarMatch[]|array $relatedJobradarMatches
*
* @throws Exception
*/
public function __construct(
User $user,
int $type,
string $title,
string $body,
array $relatedConversationMessages,
array $relatedJobradarMatches,
array $eventData
) {
ReflectionHelper::hasConstWithValue(MobileAppPushNotificationService::class, 'TYPE_', $type);
$this->id = uuid_create(UUID_TYPE_RANDOM);
$this->user = $user;
$this->type = $type;
$this->eventName = self::TYPE_TO_EVENT_NAME[$type];
if ($type === MobileAppPushNotificationService::TYPE_REDIRECT_TO_WEB) {
if (!array_key_exists('url', $eventData)) {
throw new InvalidArgumentException('Push notification of type "redirectToWeb" requires eventData field "url".');
}
}
$this->title = $title;
$this->body = $body;
ClassChecker::checkAllArrayElementsForOneOfClasses(
$relatedConversationMessages,
[
ConversationMessage::class,
'Proxies\__CG__\\' . ConversationMessage::class
]
);
$this->relatedConversationMessages = $relatedConversationMessages;
ClassChecker::checkAllArrayElementsForOneOfClasses(
$relatedJobradarMatches,
[
JobradarMatch::class,
'Proxies\__CG__\\' . JobradarMatch::class
]
);
$this->relatedJobradarMatches = $relatedJobradarMatches;
$this->createdAt = DateTimeUtility::createDateTimeUtc();
$this->eventData = json_encode($eventData, JSON_UNESCAPED_SLASHES);
}
/**
* @ORM\GeneratedValue(strategy="CUSTOM")
*
* @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
*
* @ORM\Column(name="id", type="guid")
*
* @ORM\Id
*/
private string $id;
public function getId(): string
{
return $this->id;
}
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*
* @ORM\JoinColumn(name="users_id", onDelete="CASCADE", nullable=false)
*/
private User $user;
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @ORM\Column(name="type", type="integer", nullable=false)
*/
private int $type;
public function getType(): int
{
return $this->type;
}
/**
* @ORM\Column(name="title", type="text", length=255, nullable=false)
*/
private string $title;
public function getTitle(): string
{
return $this->title;
}
/**
* @ORM\Column(name="body", type="text", length=8192, nullable=false)
*/
private string $body;
public function getBody(): string
{
return $this->body;
}
/**
* @ORM\Column(name="event_name", type="text", length=255, nullable=false)
*/
private string $eventName;
public function getEventName(): string
{
return $this->eventName;
}
/**
* @ORM\Column(name="event_data", type="text", length=8192, nullable=false)
*/
private string $eventData;
public function getEventData(): string
{
return $this->eventData;
}
/**
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private DateTime $createdAt;
/**
* @ORM\Column(name="sent_at", type="datetime", nullable=true)
*/
private ?DateTime $sentAt;
public function setSentAt(?DateTime $sentAt): void
{
$this->sentAt = $sentAt;
}
/**
* @ORM\Column(name="opened_outside_of_app_at", type="datetime", nullable=true)
*/
private ?DateTime $openedOutsideOfAppAt;
/**
* @ORM\Column(name="received_inside_of_app_at", type="datetime", nullable=true)
*/
private ?DateTime $receivedInsideOfAppAt;
/**
* @var ConversationMessage[]|Collection
*
* @ORM\ManyToMany(targetEntity="App\Entity\ConversationMessage\ConversationMessage", inversedBy="relatedPushNotifications", cascade={"persist"})
*
* @ORM\JoinTable(
* name="conversation_messages_mobile_app_push_notifications",
* joinColumns={
*
* @ORM\JoinColumn(name="mobile_app_push_notifications_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="conversation_messages_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $relatedConversationMessages;
/**
* @var JobradarMatch[]|Collection
*
* @ORM\ManyToMany(targetEntity="JanusHercules\Jobradar\Domain\Entity\JobradarMatch", cascade={"persist"})
*
* @ORM\JoinTable(
* name="jobradar_matches_mobile_app_push_notifications",
* joinColumns={
*
* @ORM\JoinColumn(name="mobile_app_push_notifications_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="jobradar_matches_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $relatedJobradarMatches;
}