src/Products/NotificationsBundle/Entity/Recipients/AppRecipient.php line 19

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity\Recipients;
  3. use DateTime;
  4. use Products\NotificationsBundle\Entity\AbstractRecipient;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Products\NotificationsBundle\Model\Contacts\AppContactInterface;
  7. use Reinder83\BinaryFlags\Bits;
  8. /**
  9.  * Class AppRecipient
  10.  * @package Products\NotificationsBundle\Entity\Recipients
  11.  *
  12.  * @ORM\Entity(
  13.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Recipients\AppRecipientRepository",
  14.  * )
  15.  */
  16. class AppRecipient extends AbstractRecipient implements AppContactInterface
  17. {
  18.     const DISCR 'app';
  19.     const PLATFORMS = [
  20.         'ios' => self::PLATFORMS__IOS,
  21.         'android' => self::PLATFORMS__ANDROID,
  22.         // TODO: for testing only
  23.         'web' => self::PLATFORMS__WEB,
  24.     ];
  25.     const PLATFORMS__UNKNOWN 0;
  26.     const PLATFORMS__IOS Bits::BIT_1;
  27.     const PLATFORMS__ANDROID Bits::BIT_2;
  28.     const PLATFORMS__WEB Bits::BIT_3;
  29.     /**
  30.      * The type of OS that runs the device (iOS, etc).
  31.      *
  32.      * @var int
  33.      *
  34.      * @ORM\Column(
  35.      *     type = "integer",
  36.      *     nullable = false,
  37.      *     options = {
  38.      *         "default" = AppRecipient::PLATFORMS__UNKNOWN,
  39.      *         "unsigned" = true,
  40.      *     },
  41.      * )
  42.      */
  43.     protected int $platform self::PLATFORMS__UNKNOWN;
  44.     /**
  45.      * On install or first run the app, a unique and random UUID value will be generated.
  46.      * This UUID value should be persisted by the app until it is uninstalled, storage is cleared, etc.
  47.      * It is improbable that two different UUIDs would ever be generated, at least not for the same user.
  48.      * This UUID value is what will allow us to allow multiple app installs across devices (such as having a phone and a tablet).
  49.      *
  50.      * TODO: is this the "Firebase installtion ID"; if not can it be?
  51.      *
  52.      * @var string|null
  53.      *
  54.      * @ORM\Column(
  55.      *     type = "string",
  56.      *     nullable = false,
  57.      * )
  58.      */
  59.     protected ?string $installation null;
  60.     /**
  61.      * A human-readable "name" or description to help people understand which device is which when using multiple.
  62.      *
  63.      * @var string|null
  64.      *
  65.      * @ORM\Column(
  66.      *     type = "string",
  67.      *     nullable = true,
  68.      * )
  69.      */
  70.     protected ?string $name null;
  71.     /**
  72.      * When this data was last updated via app APIs.
  73.      *
  74.      * @var DateTime|null
  75.      *
  76.      * @ORM\Column(
  77.      *     type = "datetime",
  78.      *     nullable = true,
  79.      * )
  80.      */
  81.     protected ?DateTime $refreshedAt null;
  82.     /**
  83.      * @var array|null
  84.      *
  85.      * @ORM\Column(
  86.      *     type = "json",
  87.      *     nullable = true,
  88.      * )
  89.      */
  90.     protected ?array $firebase = [];
  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     public function getKind(): string
  95.     {
  96.         return AbstractRecipient::KINDS__APP;
  97.     }
  98.     /**
  99.      * {@inheritDoc}
  100.      */
  101.     public function getDeviceToken(): string
  102.     {
  103.         return $this->getContact();
  104.     }
  105.     /**
  106.      * @return int
  107.      */
  108.     public function getPlatform(): int
  109.     {
  110.         return $this->platform;
  111.     }
  112.     /**
  113.      * @return string|null
  114.      */
  115.     public function getPlatformName(): ?string
  116.     {
  117.         if ( ! ($name array_search($this->getPlatform(), self::PLATFORMS))) {
  118.             return null;
  119.         }
  120.         return $name;
  121.     }
  122.     /**
  123.      * @param int|string $platform
  124.      * @return $this
  125.      */
  126.     public function setPlatform($platform): self
  127.     {
  128.         if (is_string($platform)) {
  129.             if ( ! array_key_exists($platformself::PLATFORMS)) {
  130.                 throw new \Exception();
  131.             }
  132.             $platform self::PLATFORMS[$platform];
  133.         }
  134.         if ( ! is_int($platform)) {
  135.             throw new \Exception();
  136.         }
  137.         $this->platform $platform;
  138.         return $this;
  139.     }
  140.     /**
  141.      * @return string|null
  142.      */
  143.     public function getInstallation(): ?string
  144.     {
  145.         return $this->installation;
  146.     }
  147.     /**
  148.      * @param string|null $installation
  149.      * @return $this
  150.      */
  151.     public function setInstallation(?string $installation): self
  152.     {
  153.         $this->installation $installation;
  154.         return $this;
  155.     }
  156.     /**
  157.      * @return string|null
  158.      */
  159.     public function getName(): ?string
  160.     {
  161.         return $this->name;
  162.     }
  163.     /**
  164.      * @param string|null $name
  165.      * @return $this
  166.      */
  167.     public function setName(?string $name): self
  168.     {
  169.         $this->name $name;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @return DateTime|null
  174.      */
  175.     public function getRefreshedAt(): ?DateTime
  176.     {
  177.         return $this->refreshedAt;
  178.     }
  179.     /**
  180.      * @param DateTime|null $refreshedAt
  181.      * @return $this
  182.      */
  183.     public function setRefreshedAt(?DateTime $refreshedAt): self
  184.     {
  185.         $this->refreshedAt $refreshedAt;
  186.         return $this;
  187.     }
  188.     /**
  189.      * @return array
  190.      */
  191.     public function getFirebase(): array
  192.     {
  193.         return $this->firebase ?: [];
  194.     }
  195.     /**
  196.      * @param array|null $firebase
  197.      * @return $this
  198.      */
  199.     public function setFirebase(?array $firebase): self
  200.     {
  201.         $firebase $firebase ?: [];
  202.         ksort($firebase);
  203.         $this->firebase $firebase;
  204.         return $this;
  205.     }
  206.     /**
  207.      * {@inheritDoc}
  208.      */
  209.     public function ui(): string
  210.     {
  211.         if ($this->getName()) {
  212.             return $this->getName();
  213.         }
  214.         if ($this->getPlatform() && $this->getInstallation()) {
  215.             return implode(' ', [
  216.                 strtoupper($this->getPlatformName()),
  217.                 $this->getInstallation(),
  218.             ]);
  219.         }
  220.         if (str_contains(parent::ui(), ':')) {
  221.             return explode(':'parent::ui())[0];
  222.         }
  223.         return parent::ui();
  224.     }
  225.     /**
  226.      * {@inheritDoc}
  227.      */
  228.     public function getDisplayName(): string
  229.     {
  230.         return $this->getName() ?: $this->getInstallation();
  231.     }
  232.     /**
  233.      * {@inheritDoc}
  234.      */
  235.     public function getInformation(): array
  236.     {
  237.         return [];
  238.     }
  239. }