<?php
namespace Cms\SystemBundle\Entity\Announcement;
use Cms\CoreBundle\Entity\SystemEntity;
use Cms\CoreBundle\Util\DateTimeUtils;
use Cms\TenantBundle\Entity\Tenant;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class SystemAnnouncement
* @package Cms\SystemBundle\Entity\Announcement
*
* @ORM\Entity(
* repositoryClass = "Cms\SystemBundle\Doctrine\Announcement\SystemAnnouncementRepository"
* )
* @ORM\HasLifecycleCallbacks
* @ORM\Table(
* name = "cms__system__announcements"
* )
*/
class SystemAnnouncement extends SystemEntity
{
const STATES = [
self::STATES__SUCCESS,
self::STATES__INFO,
self::STATES__WARNING,
self::STATES__DANGER,
];
const STATES__SUCCESS = 'success';
const STATES__INFO = 'info';
const STATES__WARNING = 'warning';
const STATES__DANGER = 'danger';
/**
* @var Collection|Tenant[]
*
* @ORM\ManyToMany(
* targetEntity = "Cms\TenantBundle\Entity\Tenant",
* )
* @ORM\JoinTable(
* name = "cms__tenant__system__announcement",
* joinColumns = {
* @ORM\JoinColumn(
* name = "announcement_id",
* referencedColumnName = "id",
* onDelete = "CASCADE",
* )
* },
* inverseJoinColumns = {
* @ORM\JoinColumn(
* name = "tenant_id",
* referencedColumnName = "id",
* onDelete = "CASCADE",
* )
* },
* )
*/
protected Collection $tenants;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $title = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "text",
* nullable = true,
* )
*/
protected ?string $description = null;
/**
* @var DateTimeInterface|null
*
* @ORM\Column(
* type = "datetime",
* nullable = false,
* )
*/
protected ?DateTimeInterface $startTimestamp;
/**
* @var DateTimeInterface|null
*
* @ORM\Column(
* type = "datetime",
* nullable = false,
* )
*/
protected ?DateTimeInterface $endTimestamp = null;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = false,
* options = {
* "default" = self::STATES__INFO,
* },
* )
*/
protected string $state = self::STATES__INFO;
/**
*
*/
public function __construct()
{
$this->tenants = new ArrayCollection();
}
/**
* Returns a list of related tenants
*
* @return Collection|Tenant[]
*/
public function getTenants(): Collection
{
return $this->tenants;
}
/**
* Sets up tenants
*
* @param Collection|Tenant[] $tenants
* @return $this
*/
public function setTenants(Collection $tenants): self
{
$this->tenants = $tenants;
return $this;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @return DateTimeInterface|null
*/
public function getStartTimestamp(): ?DateTimeInterface
{
return $this->startTimestamp;
}
/**
* @return DateTimeInterface|null
*/
public function getEndTimestamp(): ?DateTimeInterface
{
return $this->endTimestamp;
}
/**
* @return string
*/
public function getState(): string
{
return $this->state;
}
/**
* @param string|null $title
* @return $this
*/
public function setTitle(?string $title): self
{
$this->title = $title ?: null;
return $this;
}
/**
* @param string|null $description
* @return $this
*/
public function setDescription(?string $description): self
{
$this->description = $description ?: null;
return $this;
}
/**
* @param DateTimeInterface|null $startTimestamp
* @return $this
*/
public function setStartTimestamp(?DateTimeInterface $startTimestamp): self
{
$this->startTimestamp = $startTimestamp;
return $this;
}
/**
* @param DateTimeInterface|null $endTimestamp
* @return $this
*/
public function setEndTimestamp(?DateTimeInterface $endTimestamp): self
{
$this->endTimestamp = $endTimestamp;
return $this;
}
/**
* @param string $state
* @return $this
* @throws \Exception
*/
public function setState(string $state): self
{
if ( ! in_array(strtolower($state), self::STATES)) {
throw new \Exception(sprintf(
'Invalid state of "%s".',
$state
));
}
$this->state = $state;
return $this;
}
/**
* @return bool
*/
public function isValid(): bool
{
$now = DateTimeUtils::now();
return ($this->startTimestamp <= $now && $this->endTimestamp >= $now);
}
}