<?php
namespace Products\NotificationsBundle\Entity\Notifications\Translations;
use App\Entity\Shared\FlagsTrait;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\ORM\Mapping as ORM;
use Products\NotificationsBundle\Entity\AbstractNotification;
use Products\NotificationsBundle\Entity\Notifications\Channels\ChannelsInterface;
use Products\NotificationsBundle\Entity\Notifications\Channels\TransactionalChannelsContentTrait;
use Products\NotificationsBundle\Entity\Notifications\NotificationInterface;
use Products\NotificationsBundle\Entity\Notifications\NotificationTrait;
use Products\NotificationsBundle\Entity\Notifications\Template;
use Reinder83\BinaryFlags\Bits;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(
* repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Notifications\Translations\TranslationRepository",
* )
* @ORM\Table(
* name = "notis__translation",
* uniqueConstraints = {
* @ORM\UniqueConstraint(
* name = "uidx__notification__locale",
* columns = {
* "notification",
* "locale",
* }
* ),
* @ORM\UniqueConstraint(
* name = "uidx__template__locale",
* columns = {
* "template",
* "locale",
* }
* )
* }
* )
*/
class Translation extends TenantedEntity implements NotificationInterface//, FlagsInterface
{
public const FLAGS = [
'starter' => self::FLAGS__STARTER,
'auto' => self::FLAGS__AUTO,
'manual' => self::FLAGS__MANUAL,
];
public const FLAGS__STARTER = Bits::BIT_1;
public const FLAGS__AUTO = Bits::BIT_2;
public const FLAGS__MANUAL = Bits::BIT_3;
use NotificationTrait;
use TransactionalChannelsContentTrait;
use FlagsTrait;
/**
* @var AbstractNotification|null
*
* @ORM\ManyToOne(
* targetEntity = AbstractNotification::class,
* inversedBy = "translations",
* )
* @ORM\JoinColumn(
* name = "notification",
* referencedColumnName = "id",
* nullable = true,
* onDelete = "CASCADE",
* )
*/
protected ?AbstractNotification $notification = null;
/**
* @var Template|null
*
* @ORM\ManyToOne(
* targetEntity = Template::class,
* inversedBy = "translations",
* )
* @ORM\JoinColumn(
* name = "template",
* referencedColumnName = "id",
* nullable = true,
* onDelete = "CASCADE",
* )
*/
protected ?Template $template = null;
/**
* @return AbstractNotification|null
*/
public function getNotification(): ?AbstractNotification
{
return $this->notification;
}
/**
* @param AbstractNotification|null $notification
* @return $this
*/
public function setNotification(?AbstractNotification $notification): self
{
$this->notification = $notification;
return $this;
}
/**
* @return Template|null
*/
public function getTemplate(): ?Template
{
return $this->template;
}
/**
* @param Template|null $template
* @return self
*/
public function setTemplate(?Template $template): self
{
$this->template = $template;
return $this;
}
/**
* @return AbstractNotification|Template
*/
public function getEntity(): object
{
if ($this->getNotification() && $this->getTemplate()) {
throw new \RuntimeException();
}
if ($notification = $this->getNotification()) {
return $notification;
}
if ($template = $this->getTemplate()) {
return $template;
}
throw new \LogicException();
}
/**
* @return bool
*/
public function isUrgent(): bool
{
return $this->getEntity() && $this->getEntity()->isUrgent();
}
/**
* @return bool
*/
public function supportsVoice(): bool
{
return $this->getEntity() && $this->getEntity()->supportsVoice();
}
/**
* @return bool
*/
public function isHtml(): bool
{
return $this->getEntity() && $this->getEntity()->isHtml();
}
/**
* @return bool
*/
public function isReady(): bool
{
if ( ! $this->getEntity()) {
return false;
}
if ($this->getEntity()->getTitle() && !$this->getTitle()) {
return false;
}
if ($this->getEntity()->getDescription() && !$this->getDescription()) {
return false;
}
if ($this->getEntity()->hasChannel(ChannelsInterface::CHANNELS__VOICE)) {
if ($this->getEntity()->getScript() && ! $this->getScript()) {
return false;
}
if ($this->getEntity()->getRecording() && ! $this->getRecording()) {
return false;
}
if ($this->getEntity()->getRecording() && ! $this->getRecording()->isRecorded()) {
return false;
}
}
return true;
}
/**
* @return Translation
*/
public function fork(): Translation
{
return (new self())
->setTenant($this->getTenant())
->setLocale($this->getLocale())
->setTitle($this->getTitle())
->setDescription($this->getDescription())
->setRecording($this->getRecording());
}
/**
* {@inheritDoc}
*/
public function getSmsUrl(): ?string
{
return $this->getEntity()->getSmsUrl();
}
}