AbstractInformation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core;
  4. use ApiPlatform\Metadata\ApiResource;
  5. // use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
  6. use App\Enum\Core\NotificationTypeEnum;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JetBrains\PhpStorm\Pure;
  11. use Symfony\Component\Serializer\Annotation\Context;
  12. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  13. /**
  14. * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Notification, et supprimer l'attribut discr.
  15. *
  16. * Classe Notification qui permet de gérer les notifications aux utilisateurs.
  17. */
  18. #[ApiResource(operations: [])]
  19. // #[Auditable]
  20. #[ORM\Entity]
  21. #[ORM\Table(name: 'Information')]
  22. #[ORM\InheritanceType('SINGLE_TABLE')]
  23. #[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
  24. #[ORM\DiscriminatorMap([
  25. 'notification' => Notification::class,
  26. 'tips' => Tips::class,
  27. ])]
  28. class AbstractInformation
  29. {
  30. #[ORM\Id]
  31. #[ORM\Column]
  32. #[ORM\GeneratedValue]
  33. protected ?int $id = null;
  34. #[ORM\Column(length: 40, nullable: true)]
  35. protected ?string $name = null;
  36. #[ORM\Column(type: 'datetime', nullable: true)]
  37. protected ?\DateTimeInterface $createDate;
  38. #[ORM\Column(type: 'datetime', nullable: true)]
  39. protected ?\DateTimeInterface $updateDate;
  40. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  41. protected mixed $message = [];
  42. #[ORM\Column(length: 50, nullable: true, enumType: NotificationTypeEnum::class)]
  43. protected ?NotificationTypeEnum $type = null;
  44. #[ORM\Column(length: 255, nullable: true)]
  45. protected ?string $link = null;
  46. #[ORM\Column(type: 'date', nullable: true)]
  47. #[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
  48. protected ?\DateTimeInterface $availabilityDate = null;
  49. /** @var Collection<int, NotificationUser> */
  50. #[ORM\OneToMany(targetEntity: NotificationUser::class, mappedBy: 'notification', cascade: ['persist'], orphanRemoval: true)]
  51. protected Collection $notificationUsers;
  52. #[Pure]
  53. public function __construct()
  54. {
  55. $this->notificationUsers = new ArrayCollection();
  56. }
  57. public function getId(): ?int
  58. {
  59. return $this->id;
  60. }
  61. public function setName(?string $name): self
  62. {
  63. $this->name = $name;
  64. return $this;
  65. }
  66. public function getName(): ?string
  67. {
  68. return $this->name;
  69. }
  70. public function getCreateDate(): ?\DateTimeInterface
  71. {
  72. return $this->createDate;
  73. }
  74. public function setCreateDate(?\DateTimeInterface $createDate): self
  75. {
  76. $this->createDate = $createDate;
  77. return $this;
  78. }
  79. public function getUpdateDate(): ?\DateTimeInterface
  80. {
  81. return $this->updateDate;
  82. }
  83. public function setUpdateDate(?\DateTimeInterface $updateDate): self
  84. {
  85. $this->updateDate = $updateDate;
  86. return $this;
  87. }
  88. public function setMessage(mixed $message): self
  89. {
  90. $this->message = $message;
  91. return $this;
  92. }
  93. public function getMessage(): mixed
  94. {
  95. if (!is_array($this->message)) {
  96. return ['about' => $this->message];
  97. }
  98. return $this->message;
  99. }
  100. public function setType(?NotificationTypeEnum $type): self
  101. {
  102. $this->type = $type;
  103. return $this;
  104. }
  105. public function getType(): ?NotificationTypeEnum
  106. {
  107. return $this->type;
  108. }
  109. public function setLink(?string $link): self
  110. {
  111. $this->link = $link;
  112. return $this;
  113. }
  114. public function getLink(): ?string
  115. {
  116. return $this->link;
  117. }
  118. public function getAvailabilityDate(): ?\DateTimeInterface
  119. {
  120. return $this->availabilityDate;
  121. }
  122. public function setAvailabilityDate(?\DateTime $availabilityDate = null): self
  123. {
  124. if ($availabilityDate == null) {
  125. $availabilityDate = new \DateTime();
  126. }
  127. $this->availabilityDate = $availabilityDate;
  128. return $this;
  129. }
  130. public function getNotificationUsers(): Collection
  131. {
  132. return $this->notificationUsers;
  133. }
  134. public function addNotificationUser(NotificationUser $notificationUsers): self
  135. {
  136. if (!$this->notificationUsers->contains($notificationUsers)) {
  137. $this->notificationUsers[] = $notificationUsers;
  138. $notificationUsers->setNotification($this);
  139. }
  140. return $this;
  141. }
  142. public function removeNotificationUser(NotificationUser $notificationUsers): self
  143. {
  144. if ($this->notificationUsers->removeElement($notificationUsers)) {
  145. // set the owning side to null (unless already changed)
  146. if ($notificationUsers->getNotification() === $this) {
  147. $notificationUsers->setNotification(null);
  148. }
  149. }
  150. return $this;
  151. }
  152. }