src/Products/NotificationsBundle/Entity/Notifications/Translations/Translation.php line 41

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity\Notifications\Translations;
  3. use App\Entity\Shared\FlagsTrait;
  4. use Cms\TenantBundle\Entity\TenantedEntity;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Products\NotificationsBundle\Entity\AbstractNotification;
  7. use Products\NotificationsBundle\Entity\Notifications\Channels\ChannelsInterface;
  8. use Products\NotificationsBundle\Entity\Notifications\Channels\TransactionalChannelsContentTrait;
  9. use Products\NotificationsBundle\Entity\Notifications\NotificationInterface;
  10. use Products\NotificationsBundle\Entity\Notifications\NotificationTrait;
  11. use Products\NotificationsBundle\Entity\Notifications\Template;
  12. use Reinder83\BinaryFlags\Bits;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. /**
  15.  * @ORM\Entity(
  16.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Notifications\Translations\TranslationRepository",
  17.  * )
  18.  * @ORM\Table(
  19.  *     name = "notis__translation",
  20.  *     uniqueConstraints = {
  21.  *         @ORM\UniqueConstraint(
  22.  *             name = "uidx__notification__locale",
  23.  *             columns = {
  24.  *                 "notification",
  25.  *                 "locale",
  26.  *             }
  27.  *         ),
  28.  *         @ORM\UniqueConstraint(
  29.  *             name = "uidx__template__locale",
  30.  *             columns = {
  31.  *                 "template",
  32.  *                 "locale",
  33.  *             }
  34.  *         )
  35.  *     }
  36.  * )
  37.  */
  38. class Translation extends TenantedEntity implements NotificationInterface//, FlagsInterface
  39. {
  40.     public const FLAGS = [
  41.         'starter' => self::FLAGS__STARTER,
  42.         'auto' => self::FLAGS__AUTO,
  43.         'manual' => self::FLAGS__MANUAL,
  44.     ];
  45.     public const FLAGS__STARTER Bits::BIT_1;
  46.     public const FLAGS__AUTO Bits::BIT_2;
  47.     public const FLAGS__MANUAL Bits::BIT_3;
  48.     use NotificationTrait;
  49.     use TransactionalChannelsContentTrait;
  50.     use FlagsTrait;
  51.     /**
  52.      * @var AbstractNotification|null
  53.      *
  54.      * @ORM\ManyToOne(
  55.      *     targetEntity = AbstractNotification::class,
  56.      *     inversedBy = "translations",
  57.      * )
  58.      * @ORM\JoinColumn(
  59.      *     name = "notification",
  60.      *     referencedColumnName = "id",
  61.      *     nullable = true,
  62.      *     onDelete = "CASCADE",
  63.      * )
  64.      */
  65.     protected ?AbstractNotification $notification null;
  66.     /**
  67.      * @var Template|null
  68.      *
  69.      * @ORM\ManyToOne(
  70.      *     targetEntity = Template::class,
  71.      *     inversedBy = "translations",
  72.      * )
  73.      * @ORM\JoinColumn(
  74.      *     name = "template",
  75.      *     referencedColumnName = "id",
  76.      *     nullable = true,
  77.      *     onDelete = "CASCADE",
  78.      * )
  79.      */
  80.     protected ?Template $template null;
  81.     /**
  82.      * @return AbstractNotification|null
  83.      */
  84.     public function getNotification(): ?AbstractNotification
  85.     {
  86.         return $this->notification;
  87.     }
  88.     /**
  89.      * @param AbstractNotification|null $notification
  90.      * @return $this
  91.      */
  92.     public function setNotification(?AbstractNotification $notification): self
  93.     {
  94.         $this->notification $notification;
  95.         return $this;
  96.     }
  97.     /**
  98.      * @return Template|null
  99.      */
  100.     public function getTemplate(): ?Template
  101.     {
  102.         return $this->template;
  103.     }
  104.     /**
  105.      * @param Template|null $template
  106.      * @return self
  107.      */
  108.     public function setTemplate(?Template $template): self
  109.     {
  110.         $this->template $template;
  111.         return $this;
  112.     }
  113.     /**
  114.      * @return AbstractNotification|Template
  115.      */
  116.     public function getEntity(): object
  117.     {
  118.         if ($this->getNotification() && $this->getTemplate()) {
  119.             throw new \RuntimeException();
  120.         }
  121.         if ($notification $this->getNotification()) {
  122.             return $notification;
  123.         }
  124.         if ($template $this->getTemplate()) {
  125.             return $template;
  126.         }
  127.         throw new \LogicException();
  128.     }
  129.     /**
  130.      * @return bool
  131.      */
  132.     public function isUrgent(): bool
  133.     {
  134.         return $this->getEntity() && $this->getEntity()->isUrgent();
  135.     }
  136.     /**
  137.      * @return bool
  138.      */
  139.     public function supportsVoice(): bool
  140.     {
  141.         return $this->getEntity() && $this->getEntity()->supportsVoice();
  142.     }
  143.     /**
  144.      * @return bool
  145.      */
  146.     public function isHtml(): bool
  147.     {
  148.         return $this->getEntity() && $this->getEntity()->isHtml();
  149.     }
  150.     /**
  151.      * @return bool
  152.      */
  153.     public function isReady(): bool
  154.     {
  155.         if ( ! $this->getEntity()) {
  156.             return false;
  157.         }
  158.         if ($this->getEntity()->getTitle() && !$this->getTitle()) {
  159.             return false;
  160.         }
  161.         if ($this->getEntity()->getDescription() && !$this->getDescription()) {
  162.             return false;
  163.         }
  164.         if ($this->getEntity()->hasChannel(ChannelsInterface::CHANNELS__VOICE)) {
  165.             if ($this->getEntity()->getScript() && ! $this->getScript()) {
  166.                 return false;
  167.             }
  168.             if ($this->getEntity()->getRecording() && ! $this->getRecording()) {
  169.                 return false;
  170.             }
  171.             if ($this->getEntity()->getRecording() && ! $this->getRecording()->isRecorded()) {
  172.                 return false;
  173.             }
  174.         }
  175.         return true;
  176.     }
  177.     /**
  178.      * @return Translation
  179.      */
  180.     public function fork(): Translation
  181.     {
  182.         return (new self())
  183.             ->setTenant($this->getTenant())
  184.             ->setLocale($this->getLocale())
  185.             ->setTitle($this->getTitle())
  186.             ->setDescription($this->getDescription())
  187.             ->setRecording($this->getRecording());
  188.     }
  189.     /**
  190.      * {@inheritDoc}
  191.      */
  192.     public function getSmsUrl(): ?string
  193.     {
  194.         return $this->getEntity()->getSmsUrl();
  195.     }
  196. }