src/Cms/SystemBundle/Entity/Announcement/SystemAnnouncement.php line 25

Open in your IDE?
  1. <?php
  2. namespace Cms\SystemBundle\Entity\Announcement;
  3. use Cms\CoreBundle\Entity\SystemEntity;
  4. use Cms\CoreBundle\Util\DateTimeUtils;
  5. use Cms\TenantBundle\Entity\Tenant;
  6. use DateTimeInterface;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. /**
  11.  * Class SystemAnnouncement
  12.  * @package Cms\SystemBundle\Entity\Announcement
  13.  *
  14.  * @ORM\Entity(
  15.  *     repositoryClass = "Cms\SystemBundle\Doctrine\Announcement\SystemAnnouncementRepository"
  16.  * )
  17.  * @ORM\HasLifecycleCallbacks
  18.  * @ORM\Table(
  19.  *     name = "cms__system__announcements"
  20.  * )
  21.  */
  22. class SystemAnnouncement extends SystemEntity
  23. {
  24.     const STATES = [
  25.         self::STATES__SUCCESS,
  26.         self::STATES__INFO,
  27.         self::STATES__WARNING,
  28.         self::STATES__DANGER,
  29.     ];
  30.     const STATES__SUCCESS 'success';
  31.     const STATES__INFO 'info';
  32.     const STATES__WARNING 'warning';
  33.     const STATES__DANGER 'danger';
  34.     /**
  35.      * @var Collection|Tenant[]
  36.      *
  37.      * @ORM\ManyToMany(
  38.      *     targetEntity = "Cms\TenantBundle\Entity\Tenant",
  39.      * )
  40.      * @ORM\JoinTable(
  41.      *     name = "cms__tenant__system__announcement",
  42.      *     joinColumns = {
  43.      *         @ORM\JoinColumn(
  44.      *             name = "announcement_id",
  45.      *             referencedColumnName = "id",
  46.      *             onDelete = "CASCADE",
  47.      *         )
  48.      *     },
  49.      *     inverseJoinColumns = {
  50.      *         @ORM\JoinColumn(
  51.      *             name = "tenant_id",
  52.      *             referencedColumnName = "id",
  53.      *             onDelete = "CASCADE",
  54.      *         )
  55.      *     },
  56.      * )
  57.      */
  58.     protected Collection $tenants;
  59.     /**
  60.      * @var string|null
  61.      *
  62.      * @ORM\Column(
  63.      *     type = "string",
  64.      *     nullable = false,
  65.      * )
  66.      */
  67.     protected ?string $title null;
  68.     /**
  69.      * @var string|null
  70.      *
  71.      * @ORM\Column(
  72.      *     type = "text",
  73.      *     nullable = true,
  74.      * )
  75.      */
  76.     protected ?string $description null;
  77.     /**
  78.      * @var DateTimeInterface|null
  79.      *
  80.      * @ORM\Column(
  81.      *     type = "datetime",
  82.      *     nullable = false,
  83.      * )
  84.      */
  85.     protected ?DateTimeInterface $startTimestamp;
  86.     /**
  87.      * @var DateTimeInterface|null
  88.      *
  89.      * @ORM\Column(
  90.      *     type = "datetime",
  91.      *     nullable = false,
  92.      * )
  93.      */
  94.     protected ?DateTimeInterface $endTimestamp null;
  95.     /**
  96.      * @var string
  97.      *
  98.      * @ORM\Column(
  99.      *     type = "string",
  100.      *     nullable = false,
  101.      *     options = {
  102.      *         "default" = self::STATES__INFO,
  103.      *     },
  104.      * )
  105.      */
  106.     protected string $state self::STATES__INFO;
  107.     /**
  108.      *
  109.      */
  110.     public function __construct()
  111.     {
  112.         $this->tenants = new ArrayCollection();
  113.     }
  114.     /**
  115.      * Returns a list of related tenants
  116.      *
  117.      * @return Collection|Tenant[]
  118.      */
  119.     public function getTenants(): Collection
  120.     {
  121.         return $this->tenants;
  122.     }
  123.     /**
  124.      * Sets up tenants
  125.      *
  126.      * @param Collection|Tenant[] $tenants
  127.      * @return $this
  128.      */
  129.     public function setTenants(Collection $tenants): self
  130.     {
  131.         $this->tenants $tenants;
  132.         return $this;
  133.     }
  134.     /**
  135.      * @return string|null
  136.      */
  137.     public function getTitle(): ?string
  138.     {
  139.         return $this->title;
  140.     }
  141.     /**
  142.      * @return string|null
  143.      */
  144.     public function getDescription(): ?string
  145.     {
  146.         return $this->description;
  147.     }
  148.     /**
  149.      * @return DateTimeInterface|null
  150.      */
  151.     public function getStartTimestamp(): ?DateTimeInterface
  152.     {
  153.         return $this->startTimestamp;
  154.     }
  155.     /**
  156.      * @return DateTimeInterface|null
  157.      */
  158.     public function getEndTimestamp(): ?DateTimeInterface
  159.     {
  160.         return $this->endTimestamp;
  161.     }
  162.     /**
  163.      * @return string
  164.      */
  165.     public function getState(): string
  166.     {
  167.         return $this->state;
  168.     }
  169.     /**
  170.      * @param string|null $title
  171.      * @return $this
  172.      */
  173.     public function setTitle(?string $title): self
  174.     {
  175.         $this->title $title ?: null;
  176.         return $this;
  177.     }
  178.     /**
  179.      * @param string|null $description
  180.      * @return $this
  181.      */
  182.     public function setDescription(?string $description): self
  183.     {
  184.         $this->description $description ?: null;
  185.         return $this;
  186.     }
  187.     /**
  188.      * @param DateTimeInterface|null $startTimestamp
  189.      * @return $this
  190.      */
  191.     public function setStartTimestamp(?DateTimeInterface $startTimestamp): self
  192.     {
  193.         $this->startTimestamp $startTimestamp;
  194.         return $this;
  195.     }
  196.     /**
  197.      * @param DateTimeInterface|null $endTimestamp
  198.      * @return $this
  199.      */
  200.     public function setEndTimestamp(?DateTimeInterface $endTimestamp): self
  201.     {
  202.         $this->endTimestamp $endTimestamp;
  203.         return $this;
  204.     }
  205.     /**
  206.      * @param string $state
  207.      * @return $this
  208.      * @throws \Exception
  209.      */
  210.     public function setState(string $state): self
  211.     {
  212.         if ( ! in_array(strtolower($state), self::STATES)) {
  213.             throw new \Exception(sprintf(
  214.                 'Invalid state of "%s".',
  215.                 $state
  216.             ));
  217.         }
  218.         $this->state $state;
  219.         return $this;
  220.     }
  221.     /**
  222.      * @return bool
  223.      */
  224.     public function isValid(): bool
  225.     {
  226.         $now DateTimeUtils::now();
  227.         return ($this->startTimestamp <= $now && $this->endTimestamp >= $now);
  228.     }
  229. }