src/Products/NotificationsBundle/Entity/Recipients/PhoneRecipient.php line 23

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity\Recipients;
  3. use App\Service\Data\PhoneNumberService;
  4. use App\Validator\Constraints\PhoneRecipientCompatibleMethod;
  5. use DateTime;
  6. use Products\NotificationsBundle\Entity\AbstractRecipient;
  7. use Products\NotificationsBundle\Model\Contacts\PhoneContactInterface;
  8. use Reinder83\BinaryFlags\Bits;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * Class PhoneRecipient
  12.  * @package Products\NotificationsBundle\Entity\Recipients
  13.  *
  14.  * @ORM\Entity(
  15.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Recipients\PhoneRecipientRepository",
  16.  * )
  17.  *
  18.  * @PhoneRecipientCompatibleMethod()
  19.  */
  20. class PhoneRecipient extends AbstractRecipient implements PhoneContactInterface
  21. {
  22.     const DISCR 'phone';
  23.     const TYPES = [
  24.         null => self::TYPES__UNKNOWN,
  25.         'landline' => self::TYPES__LANDLINE,
  26.         'mobile' => self::TYPES__MOBILE,
  27.         'voip' => self::TYPES__VOIP,
  28.     ];
  29.     const TYPES__UNKNOWN 0;
  30.     const TYPES__LANDLINE Bits::BIT_1;
  31.     const TYPES__MOBILE Bits::BIT_2;
  32.     const TYPES__VOIP Bits::BIT_3;
  33.     const METHODS = [
  34.         'sms' => self::METHODS__SMS,
  35.         'voice' => self::METHODS__VOICE,
  36.         'hybrid' => self::METHODS__HYBRID,
  37.     ];
  38.     const METHODS__UNSET 0;
  39.     const METHODS__SMS Bits::BIT_1;
  40.     const METHODS__VOICE Bits::BIT_2;
  41.     const METHODS__HYBRID Bits::BIT_3;
  42.     const SMS_METHODS = [
  43.         self::METHODS__SMS,
  44.         self::METHODS__HYBRID,
  45.     ];
  46.     const VOICE_METHODS = [
  47.         self::METHODS__UNSET,
  48.         self::METHODS__VOICE,
  49.         self::METHODS__HYBRID,
  50.     ];
  51.     const HYBRID_METHODS = [
  52.         self::METHODS__HYBRID,
  53.     ];
  54.     const LOOKUPS__MISSING null;
  55.     const LOOKUPS__SUCCESSFUL true;
  56.     const LOOKUPS__FAILED false;
  57.     /**
  58.      * @var array
  59.      *
  60.      * @ORM\Column(
  61.      *     type = "json",
  62.      *     nullable = false,
  63.      * )
  64.      */
  65.     protected array $lookup = [];
  66.     /**
  67.      * @var DateTime|null
  68.      *
  69.      * @ORM\Column(
  70.      *     type = "datetime",
  71.      *     nullable = true,
  72.      * )
  73.      */
  74.     protected ?DateTime $lookupTimestamp null;
  75.     /**
  76.      * @var bool|null
  77.      *
  78.      * @ORM\Column(
  79.      *     type = "boolean",
  80.      *     nullable = true,
  81.      * )
  82.      */
  83.     protected ?bool $lookupStatus self::LOOKUPS__MISSING;
  84.     /**
  85.      * @var int
  86.      *
  87.      * @ORM\Column(
  88.      *     type = "integer",
  89.      *     nullable = false,
  90.      *     options = {
  91.      *         "default" = PhoneRecipient::TYPES__UNKNOWN,
  92.      *         "unsigned" = true,
  93.      *     },
  94.      * )
  95.      */
  96.     protected int $type self::TYPES__UNKNOWN;
  97.     /**
  98.      * @var int
  99.      *
  100.      * @ORM\Column(
  101.      *     type = "integer",
  102.      *     nullable = false,
  103.      *     options = {
  104.      *         "default" = PhoneRecipient::METHODS__UNSET,
  105.      *         "unsigned" = true,
  106.      *     },
  107.      * )
  108.      */
  109.     protected int $method self::METHODS__UNSET;
  110.     /**
  111.      * @var bool|null
  112.      *
  113.      * @ORM\Column(
  114.      *     type = "boolean",
  115.      *     nullable = false,
  116.      *     options = {
  117.      *         "default" = false,
  118.      *     },
  119.      * )
  120.      */
  121.     protected bool $blocked false;
  122.     /**
  123.      * Whether the phone supports texting and voice.
  124.      *
  125.      * @return bool
  126.      */
  127.     public function isHybrid(): bool
  128.     {
  129.         return in_array($this->getMethod(), self::HYBRID_METHODS);
  130.     }
  131.     /**
  132.      * Whether the phone supports texting.
  133.      *
  134.      * @return bool
  135.      */
  136.     public function isSms(): bool
  137.     {
  138.         return in_array($this->getMethod(), self::SMS_METHODS);
  139.     }
  140.     /**
  141.      * Whether the phone prefers voice communication.
  142.      * Since any phone type can support voice, but not texting, this is the default type if none is set.
  143.      *
  144.      * @return bool
  145.      */
  146.     public function isVoice(): bool
  147.     {
  148.         return in_array($this->getMethod(), self::VOICE_METHODS);
  149.     }
  150.     /**
  151.      * @return int
  152.      */
  153.     public function getType(): int
  154.     {
  155.         return $this->type;
  156.     }
  157.     /**
  158.      * @param int $type
  159.      * @return $this
  160.      */
  161.     public function setType(int $type): self
  162.     {
  163.         $this->type $type;
  164.         return $this;
  165.     }
  166.     /**
  167.      * @return int
  168.      */
  169.     public function getMethod(): int
  170.     {
  171.         return $this->method;
  172.     }
  173.     /**
  174.      * @param int $method
  175.      * @return $this
  176.      */
  177.     public function setMethod(int $method): self
  178.     {
  179.         $this->method $method;
  180.         return $this;
  181.     }
  182.     /**
  183.      * @param int $method
  184.      * @param bool $toggle
  185.      * @return $this
  186.      */
  187.     public function toggleMethod(int $methodbool $toggle): self
  188.     {
  189.         static $alts = [
  190.             self::METHODS__SMS => self::METHODS__VOICE,
  191.             self::METHODS__VOICE => self::METHODS__SMS,
  192.         ];
  193.         switch (true) {
  194.             // if nothing is set, just set it
  195.             case $toggle && $this->getMethod() === self::METHODS__UNSET:
  196.             // if something is set, and it is us, can just set us again
  197.             case $toggle && $this->getMethod() === $method:
  198.                 return $this->setMethod($method);
  199.             // if something is set, and it is not hybrid, make it hybrid
  200.             case $toggle && $this->getMethod() !== self::METHODS__HYBRID:
  201.             // or, we are already hybrid
  202.             case $toggle && $this->getMethod() === self::METHODS__HYBRID:
  203.                 return $this->setMethod(self::METHODS__HYBRID);
  204.             // toggling off and we are hybid
  205.             case ! $toggle && $this->getMethod() === self::METHODS__HYBRID:
  206.                 return $this->setMethod($alts[$method]);
  207.             // toggling off, and we are not what is toggled on, just pass through
  208.             case ! $toggle && $this->getMethod() !== $method:
  209.                 return $this;
  210.             // toggling off, and we are what is toggled on, so just unset the method
  211.             case ! $toggle && $this->getMethod() === $method:
  212.                 // toggling off, and nothing is already set
  213.             case ! $toggle && $this->getMethod() === self::METHODS__UNSET:
  214.                 return $this->setMethod(self::METHODS__UNSET);
  215.         }
  216.         throw new \Exception();
  217.     }
  218.     /**
  219.      * {@inheritDoc}
  220.      */
  221.     public function getKind(): string
  222.     {
  223.         switch (true) {
  224.             case $this->isHybrid():
  225.                 return AbstractRecipient::KINDS__HYBRID;
  226.             case $this->isSms():
  227.                 return AbstractRecipient::KINDS__SMS;
  228.             case $this->isVoice():
  229.                 return AbstractRecipient::KINDS__VOICE;
  230.         }
  231.         throw new \Exception();
  232.     }
  233.     /**
  234.      * {@inheritDoc}
  235.      */
  236.     public function getPhoneNumber(): string
  237.     {
  238.         return $this->getContact();
  239.     }
  240.     /**
  241.      * @return array|null
  242.      */
  243.     public function getLookup(): ?array
  244.     {
  245.         return $this->lookup ?: null;
  246.     }
  247.     /**
  248.      * @param array|null $lookup
  249.      * @return $this
  250.      */
  251.     public function setLookup(?array $lookup): self
  252.     {
  253.         $this->lookup $lookup ?: [];
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return DateTime|null
  258.      */
  259.     public function getLookupTimestamp(): ?DateTime
  260.     {
  261.         return $this->lookupTimestamp;
  262.     }
  263.     /**
  264.      * @param DateTime|null $lookupTimestamp
  265.      * @return $this
  266.      */
  267.     public function setLookupTimestamp(?DateTime $lookupTimestamp): self
  268.     {
  269.         $this->lookupTimestamp $lookupTimestamp;
  270.         return $this;
  271.     }
  272.     /**
  273.      * @return bool|null
  274.      */
  275.     public function getLookupStatus(): ?bool
  276.     {
  277.         return $this->lookupStatus;
  278.     }
  279.     /**
  280.      * @param bool|null $lookupStatus
  281.      * @return $this
  282.      */
  283.     public function setLookupStatus(?bool $lookupStatus): self
  284.     {
  285.         $this->lookupStatus $lookupStatus;
  286.         return $this;
  287.     }
  288.     /**
  289.      * {@inheritDoc}
  290.      */
  291.     public function getDisplayName(): string
  292.     {
  293.         return PhoneNumberService::getInstance()->national(
  294.             $this->getPhoneNumber()
  295.         );
  296.     }
  297.     /**
  298.      * @return bool
  299.      */
  300.     public function isBlocked(): bool
  301.     {
  302.         return $this->blocked;
  303.     }
  304.     /**
  305.      * @param bool $blocked
  306.      * @return self
  307.      */
  308.     public function setBlocked(bool $blocked): self
  309.     {
  310.         $this->blocked $blocked;
  311.         return $this;
  312.     }
  313. }