src/Products/NotificationsBundle/Entity/Notifications/Template.php line 32

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity\Notifications;
  3. use App\Entity\System\SocialAccounts\FacebookSocialAccount;
  4. use App\Entity\System\SocialAccounts\InstagramSocialAccount;
  5. use App\Entity\System\SocialAccounts\TwitterSocialAccount;
  6. use Cms\ContainerBundle\Entity\Containers\GenericContainer;
  7. use Cms\CoreBundle\Model\Interfaces\Loggable\LoggableInterface;
  8. use Cms\TenantBundle\Entity\TenantedEntity;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Products\NotificationsBundle\Entity\AbstractList;
  13. use Products\NotificationsBundle\Entity\Notifications\Channels\ChannelsInterface;
  14. use Products\NotificationsBundle\Entity\Notifications\Channels\ServiceChannelsInterface;
  15. use Products\NotificationsBundle\Entity\Notifications\Channels\ServiceChannelsTrait;
  16. use Products\NotificationsBundle\Entity\Notifications\Channels\TransactionalChannelsInterface;
  17. use Products\NotificationsBundle\Entity\Notifications\Channels\TransactionalChannelsTrait;
  18. use Products\NotificationsBundle\Entity\Notifications\Translations\Translation;
  19. use Reinder83\BinaryFlags\Bits;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. /**
  22.  * @ORM\Entity(
  23.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Notifications\TemplateRepository",
  24.  * )
  25.  * @ORM\Table(
  26.  *     name = "notis__template",
  27.  * )
  28.  */
  29. class Template extends TenantedEntity implements NotificationInterfaceTransactionalChannelsInterfaceServiceChannelsInterfaceLoggableInterface
  30. {
  31.     public const DEFAULT_PREFIX 'DEFAULT ';
  32.     public const DEFAULT_GENERAL_NAME self::DEFAULT_PREFIX 'GENERAL';
  33.     public const DEFAULT_URGENT_NAME self::DEFAULT_PREFIX 'URGENT';
  34.     public const STATUSES = [
  35.         'draft' => self::STATUSES__DRAFT,
  36.         'sendable' => self::STATUSES__SENDABLE,
  37.     ];
  38.     public const STATUSES__DRAFT 0;
  39.     public const STATUSES__SENDABLE Bits::BIT_1;
  40.     public const TYPES = [
  41.         'general' => self::TYPES__GENERAL,
  42.         'urgent' => self::TYPES__URGENT,
  43.         'automation' => self::TYPES__AUTOMATION,
  44.     ];
  45.     public const TYPES__GENERAL Bits::BIT_1;
  46.     public const TYPES__URGENT Bits::BIT_2;
  47.     public const TYPES__AUTOMATION Bits::BIT_3;
  48.     public const VALID_CHANNELS = [
  49.         self::TYPES__GENERAL => [
  50.             ChannelsInterface::CHANNELS__FACEBOOK,
  51.             ChannelsInterface::CHANNELS__TWITTER,
  52.             ChannelsInterface::CHANNELS__INSTAGRAM,
  53.             ChannelsInterface::CHANNELS__WEBSITE,
  54.             ChannelsInterface::CHANNELS__EMAIL,
  55.             ChannelsInterface::CHANNELS__SMS,
  56.             ChannelsInterface::CHANNELS__APP,
  57.         ],
  58.         self::TYPES__URGENT => [
  59.             ChannelsInterface::CHANNELS__FACEBOOK,
  60.             ChannelsInterface::CHANNELS__TWITTER,
  61.             ChannelsInterface::CHANNELS__INSTAGRAM,
  62.             ChannelsInterface::CHANNELS__WEBSITE,
  63.             ChannelsInterface::CHANNELS__EMAIL,
  64.             ChannelsInterface::CHANNELS__SMS,
  65.             ChannelsInterface::CHANNELS__APP,
  66.             ChannelsInterface::CHANNELS__VOICE,
  67.         ],
  68.         self::TYPES__AUTOMATION => [
  69.             ChannelsInterface::CHANNELS__EMAIL,
  70.             ChannelsInterface::CHANNELS__SMS,
  71.             ChannelsInterface::CHANNELS__APP,
  72.             ChannelsInterface::CHANNELS__VOICE,
  73.         ],
  74.     ];
  75.     use NotificationTrait;
  76.     use TransactionalChannelsTrait;
  77.     use ServiceChannelsTrait;
  78.     /**
  79.      * @var Collection|AbstractList[]|null
  80.      *
  81.      * @ORM\ManyToMany(
  82.      *     targetEntity = "Products\NotificationsBundle\Entity\AbstractList",
  83.      * )
  84.      * @ORM\JoinTable(
  85.      *     name = "notis__template_x_list",
  86.      *     joinColumns = {
  87.      *         @ORM\JoinColumn(
  88.      *             name = "template",
  89.      *             referencedColumnName = "id",
  90.      *             onDelete = "CASCADE",
  91.      *         ),
  92.      *     },
  93.      *     inverseJoinColumns = {
  94.      *         @ORM\JoinColumn(
  95.      *             name = "list",
  96.      *             referencedColumnName = "id",
  97.      *             onDelete = "CASCADE",
  98.      *         ),
  99.      *     },
  100.      * )
  101.      *
  102.      * @Groups("template_details")
  103.      */
  104.     protected ?Collection $lists null;
  105.     /**
  106.      * @var Collection|GenericContainer[]|null
  107.      *
  108.      * @ORM\ManyToMany(
  109.      *     targetEntity = "Cms\ContainerBundle\Entity\Containers\GenericContainer",
  110.      * )
  111.      * @ORM\JoinTable(
  112.      *     name = "notis__template_x_department",
  113.      *     joinColumns = {
  114.      *         @ORM\JoinColumn(
  115.      *             name = "template",
  116.      *             referencedColumnName = "id",
  117.      *             onDelete = "CASCADE",
  118.      *         ),
  119.      *     },
  120.      *     inverseJoinColumns = {
  121.      *         @ORM\JoinColumn(
  122.      *             name = "department",
  123.      *             referencedColumnName = "id",
  124.      *             onDelete = "CASCADE",
  125.      *         ),
  126.      *     },
  127.      * )
  128.      *
  129.      * @Groups("template_details")
  130.      */
  131.     protected ?Collection $websiteDepartments null;
  132.     /**
  133.      * @var Collection|FacebookSocialAccount[]|null
  134.      *
  135.      * @ORM\ManyToMany(
  136.      *     targetEntity = "App\Entity\System\SocialAccounts\FacebookSocialAccount",
  137.      * )
  138.      * @ORM\JoinTable(
  139.      *     name = "notis__template_x_facebook_social_account",
  140.      *     joinColumns = {
  141.      *         @ORM\JoinColumn(
  142.      *             name = "template",
  143.      *             referencedColumnName = "id",
  144.      *             onDelete = "CASCADE",
  145.      *         ),
  146.      *     },
  147.      *     inverseJoinColumns = {
  148.      *         @ORM\JoinColumn(
  149.      *             name = "facebookSocialAccount",
  150.      *             referencedColumnName = "id",
  151.      *             onDelete = "CASCADE",
  152.      *         ),
  153.      *     },
  154.      * )
  155.      *
  156.      * @Groups("template_details")
  157.      */
  158.     protected ?Collection $facebookSocialAccounts null;
  159.     /**
  160.      * @var Collection|InstagramSocialAccount[]|null
  161.      *
  162.      * @ORM\ManyToMany(
  163.      *     targetEntity = "App\Entity\System\SocialAccounts\InstagramSocialAccount",
  164.      * )
  165.      * @ORM\JoinTable(
  166.      *     name = "notis__template_x_instagram_social_account",
  167.      *     joinColumns = {
  168.      *         @ORM\JoinColumn(
  169.      *             name = "template",
  170.      *             referencedColumnName = "id",
  171.      *             onDelete = "CASCADE",
  172.      *         ),
  173.      *     },
  174.      *     inverseJoinColumns = {
  175.      *         @ORM\JoinColumn(
  176.      *             name = "instagramSocialAccount",
  177.      *             referencedColumnName = "id",
  178.      *             onDelete = "CASCADE",
  179.      *         ),
  180.      *     },
  181.      * )
  182.      *
  183.      * @Groups("template_details")
  184.      */
  185.     protected ?Collection $instagramSocialAccounts null;
  186.     /**
  187.      * @var Collection|TwitterSocialAccount[]|null
  188.      *
  189.      * @ORM\ManyToMany(
  190.      *     targetEntity = "App\Entity\System\SocialAccounts\TwitterSocialAccount",
  191.      * )
  192.      * @ORM\JoinTable(
  193.      *     name = "notis__template_x_twitter_social_account",
  194.      *     joinColumns = {
  195.      *         @ORM\JoinColumn(
  196.      *             name = "template",
  197.      *             referencedColumnName = "id",
  198.      *             onDelete = "CASCADE",
  199.      *         ),
  200.      *     },
  201.      *     inverseJoinColumns = {
  202.      *         @ORM\JoinColumn(
  203.      *             name = "twitterSocialAccount",
  204.      *             referencedColumnName = "id",
  205.      *             onDelete = "CASCADE",
  206.      *         ),
  207.      *     },
  208.      * )
  209.      *
  210.      * @Groups("template_details")
  211.      */
  212.     protected ?Collection $twitterSocialAccounts null;
  213.     /**
  214.      * @var bool
  215.      *
  216.      * @ORM\Column(
  217.      *     type = "boolean",
  218.      *     nullable = false,
  219.      *     options = {
  220.      *         "default" = false,
  221.      *     },
  222.      * )
  223.      */
  224.     protected bool $urgent false;
  225.     /**
  226.      * @var int
  227.      *
  228.      * @ORM\Column(
  229.      *     type = "bigint",
  230.      *     nullable = false,
  231.      *     options = {
  232.      *         "default" = 0,
  233.      *     },
  234.      * )
  235.      *
  236.      * @Groups("template_details")
  237.      */
  238.     protected int $status 0;
  239.     /**
  240.      * @var string|null
  241.      *
  242.      * @ORM\Column(
  243.      *     type = "string",
  244.      *     nullable = false,
  245.      * )
  246.      *
  247.      * @Groups("template_details")
  248.      */
  249.     private ?string $name null;
  250.     /**
  251.      * @var bool
  252.      *
  253.      * @ORM\Column(
  254.      *     type = "boolean",
  255.      *     nullable = false,
  256.      *     options = {
  257.      *         "default" = false,
  258.      *     },
  259.      * )
  260.      */
  261.     private bool $forAutomations false;
  262.     /**
  263.      * @var Collection|Translation[]
  264.      *
  265.      * @ORM\OneToMany(
  266.      *     targetEntity = Translation::class,
  267.      *     mappedBy = "template",
  268.      * )
  269.      */
  270.     protected Collection $translations;
  271.     public function __construct()
  272.     {
  273.         $this->lists = new ArrayCollection();
  274.         $this->facebookSocialAccounts = new ArrayCollection();
  275.         $this->instagramSocialAccounts = new ArrayCollection();
  276.         $this->twitterSocialAccounts = new ArrayCollection();
  277.         $this->websiteDepartments = new ArrayCollection();
  278.     }
  279.     /**
  280.      * @return int
  281.      */
  282.     public function getType(): int
  283.     {
  284.         switch (true) {
  285.             case $this->isForAutomations():
  286.                 return self::TYPES__AUTOMATION;
  287.             case $this->isUrgent():
  288.                 return self::TYPES__URGENT;
  289.             case !$this->isUrgent():
  290.                 return self::TYPES__GENERAL;
  291.             default:
  292.                 throw new \RuntimeException();
  293.         }
  294.     }
  295.     /**
  296.      * @return string
  297.      */
  298.     public function getTypeName(): string
  299.     {
  300.         $name array_search($this->getType(), self::TYPEStrue);
  301.         if ( ! $name) {
  302.             throw new \RuntimeException();
  303.         }
  304.         return $name;
  305.     }
  306.     /**
  307.      * @return bool
  308.      */
  309.     public function isForAutomations(): bool
  310.     {
  311.         return $this->forAutomations;
  312.     }
  313.     /**
  314.      * @param bool $forAutomations
  315.      * @return $this
  316.      */
  317.     public function setForAutomations(bool $forAutomations): self
  318.     {
  319.         $this->forAutomations $forAutomations;
  320.         return $this;
  321.     }
  322.     /**
  323.      * @return bool
  324.      */
  325.     public function isUrgent(): bool
  326.     {
  327.         return $this->urgent;
  328.     }
  329.     /**
  330.      * @return bool
  331.      */
  332.     public function supportsVoice(): bool
  333.     {
  334.         return $this->isUrgent() || $this->isForAutomations();
  335.     }
  336.     /**
  337.      * @param bool $urgent
  338.      * @return $this
  339.      */
  340.     public function setUrgent(bool $urgent): self
  341.     {
  342.         $this->urgent $urgent;
  343.         return $this;
  344.     }
  345.     /**
  346.      * @return int
  347.      */
  348.     public function getStatus(): int
  349.     {
  350.         return $this->status;
  351.     }
  352.     /**
  353.      * @param int $status
  354.      * @return bool
  355.      */
  356.     public function isStatus(int $status): bool
  357.     {
  358.         if ($status === 0) {
  359.             return ($this->getStatus() === $status);
  360.         } else {
  361.             if ($status 0) {
  362.                 if ($this->getStatus() < 0) {
  363.                     return false;
  364.                 }
  365.                 return (($this->getStatus() & $status) === $status);
  366.             } else {
  367.                 if ($this->getStatus() > 0) {
  368.                     return false;
  369.                 }
  370.                 return ((~$this->getStatus() & ~$status) === ~$status);
  371.             }
  372.         }
  373.     }
  374.     /**
  375.      * @param int $status
  376.      * @return $this
  377.      */
  378.     public function setStatus(int $status): self
  379.     {
  380.         $this->status $status;
  381.         return $this;
  382.     }
  383.     /**
  384.      * @return string|null
  385.      */
  386.     public function getName(): ?string
  387.     {
  388.         return $this->name;
  389.     }
  390.     /**
  391.      * @param string $name
  392.      * @return $this
  393.      */
  394.     public function setName(string $name): self
  395.     {
  396.         $this->name $name;
  397.         return $this;
  398.     }
  399.     /**
  400.      * @return bool
  401.      */
  402.     public function isDraft(): bool
  403.     {
  404.         return $this->isStatus(self::STATUSES__DRAFT);
  405.     }
  406.     /**
  407.      * @return bool
  408.      */
  409.     public function isSendable(): bool
  410.     {
  411.         return $this->isStatus(self::STATUSES__SENDABLE);
  412.     }
  413.     /**
  414.      * @return array<int>
  415.      */
  416.     public function getValidChannels(): array
  417.     {
  418.         return self::VALID_CHANNELS[$this->getType()];
  419.     }
  420.     /**
  421.      * @param int $channel
  422.      * @return bool
  423.      */
  424.     public function isValidChannel(int $channel): bool
  425.     {
  426.         return in_array($channel$this->getValidChannels(), true);
  427.     }
  428.     /**
  429.      * {@inheritDoc}
  430.      */
  431.     public function getLoggableDetails(): array
  432.     {
  433.         return [
  434.             'id' => (string) $this->getId(),
  435.             'title' => $this->getTitle(),
  436.         ];
  437.     }
  438.     /**
  439.      * @return Collection
  440.      */
  441.     public function getTranslations(): Collection
  442.     {
  443.         return $this->translations;
  444.     }
  445.     /**
  446.      * @param Collection $translations
  447.      * @return self
  448.      */
  449.     public function setTranslations(Collection $translations): self
  450.     {
  451.         $this->translations $translations;
  452.         return $this;
  453.     }
  454. }