<?php
namespace Products\NotificationsBundle\Entity\Recipients;
use DateTime;
use Products\NotificationsBundle\Entity\AbstractRecipient;
use Doctrine\ORM\Mapping as ORM;
use Products\NotificationsBundle\Model\Contacts\AppContactInterface;
use Reinder83\BinaryFlags\Bits;
/**
* Class AppRecipient
* @package Products\NotificationsBundle\Entity\Recipients
*
* @ORM\Entity(
* repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Recipients\AppRecipientRepository",
* )
*/
class AppRecipient extends AbstractRecipient implements AppContactInterface
{
const DISCR = 'app';
const PLATFORMS = [
'ios' => self::PLATFORMS__IOS,
'android' => self::PLATFORMS__ANDROID,
// TODO: for testing only
'web' => self::PLATFORMS__WEB,
];
const PLATFORMS__UNKNOWN = 0;
const PLATFORMS__IOS = Bits::BIT_1;
const PLATFORMS__ANDROID = Bits::BIT_2;
const PLATFORMS__WEB = Bits::BIT_3;
/**
* The type of OS that runs the device (iOS, etc).
*
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = AppRecipient::PLATFORMS__UNKNOWN,
* "unsigned" = true,
* },
* )
*/
protected int $platform = self::PLATFORMS__UNKNOWN;
/**
* On install or first run the app, a unique and random UUID value will be generated.
* This UUID value should be persisted by the app until it is uninstalled, storage is cleared, etc.
* It is improbable that two different UUIDs would ever be generated, at least not for the same user.
* This UUID value is what will allow us to allow multiple app installs across devices (such as having a phone and a tablet).
*
* TODO: is this the "Firebase installtion ID"; if not can it be?
*
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $installation = null;
/**
* A human-readable "name" or description to help people understand which device is which when using multiple.
*
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $name = null;
/**
* When this data was last updated via app APIs.
*
* @var DateTime|null
*
* @ORM\Column(
* type = "datetime",
* nullable = true,
* )
*/
protected ?DateTime $refreshedAt = null;
/**
* @var array|null
*
* @ORM\Column(
* type = "json",
* nullable = true,
* )
*/
protected ?array $firebase = [];
/**
* {@inheritDoc}
*/
public function getKind(): string
{
return AbstractRecipient::KINDS__APP;
}
/**
* {@inheritDoc}
*/
public function getDeviceToken(): string
{
return $this->getContact();
}
/**
* @return int
*/
public function getPlatform(): int
{
return $this->platform;
}
/**
* @return string|null
*/
public function getPlatformName(): ?string
{
if ( ! ($name = array_search($this->getPlatform(), self::PLATFORMS))) {
return null;
}
return $name;
}
/**
* @param int|string $platform
* @return $this
*/
public function setPlatform($platform): self
{
if (is_string($platform)) {
if ( ! array_key_exists($platform, self::PLATFORMS)) {
throw new \Exception();
}
$platform = self::PLATFORMS[$platform];
}
if ( ! is_int($platform)) {
throw new \Exception();
}
$this->platform = $platform;
return $this;
}
/**
* @return string|null
*/
public function getInstallation(): ?string
{
return $this->installation;
}
/**
* @param string|null $installation
* @return $this
*/
public function setInstallation(?string $installation): self
{
$this->installation = $installation;
return $this;
}
/**
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string|null $name
* @return $this
*/
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return DateTime|null
*/
public function getRefreshedAt(): ?DateTime
{
return $this->refreshedAt;
}
/**
* @param DateTime|null $refreshedAt
* @return $this
*/
public function setRefreshedAt(?DateTime $refreshedAt): self
{
$this->refreshedAt = $refreshedAt;
return $this;
}
/**
* @return array
*/
public function getFirebase(): array
{
return $this->firebase ?: [];
}
/**
* @param array|null $firebase
* @return $this
*/
public function setFirebase(?array $firebase): self
{
$firebase = $firebase ?: [];
ksort($firebase);
$this->firebase = $firebase;
return $this;
}
/**
* {@inheritDoc}
*/
public function ui(): string
{
if ($this->getName()) {
return $this->getName();
}
if ($this->getPlatform() && $this->getInstallation()) {
return implode(' ', [
strtoupper($this->getPlatformName()),
$this->getInstallation(),
]);
}
if (str_contains(parent::ui(), ':')) {
return explode(':', parent::ui())[0];
}
return parent::ui();
}
/**
* {@inheritDoc}
*/
public function getDisplayName(): string
{
return $this->getName() ?: $this->getInstallation();
}
/**
* {@inheritDoc}
*/
public function getInformation(): array
{
return [];
}
}