AbstractMessage.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Message;
  4. use App\Attribute\OrganizationDefaultValue;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Core\File;
  7. use App\Entity\Core\Tagg;
  8. use App\Entity\Organization\Organization;
  9. use App\Enum\Message\MessageStatusEnum;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Ramsey\Uuid\Doctrine\UuidGenerator;
  14. use Ramsey\Uuid\Uuid;
  15. use Ramsey\Uuid\UuidInterface;
  16. /**
  17. * Classe de base de @see Mail, Sms, Email.
  18. */
  19. #[ORM\Entity]
  20. #[OrganizationDefaultValue(fieldName: 'organization')]
  21. #[ORM\Table(name: 'Message')]
  22. #[ORM\InheritanceType('SINGLE_TABLE')]
  23. #[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
  24. #[ORM\DiscriminatorMap(
  25. [
  26. 'sms' => Sms::class,
  27. 'mail' => Mail::class,
  28. 'email' => Email::class,
  29. ]
  30. )]
  31. abstract class AbstractMessage
  32. {
  33. /**
  34. * IMPORTANT! Cette propriété doit être déclarée avant l'id, pour prévenir un bug doctrine.
  35. *
  36. * @see https://github.com/doctrine/orm/issues/7215
  37. */
  38. #[ORM\Column(type: 'uuid', unique: true)]
  39. #[ORM\GeneratedValue(strategy: 'CUSTOM')]
  40. #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
  41. protected ?UuidInterface $uuid = null;
  42. #[ORM\Id]
  43. #[ORM\Column]
  44. #[ORM\GeneratedValue]
  45. protected ?int $id = null;
  46. #[ORM\ManyToOne(inversedBy: 'emails')]
  47. #[ORM\JoinColumn(nullable: true)]
  48. private ?Access $author = null;
  49. #[ORM\ManyToOne]
  50. #[ORM\JoinColumn(nullable: true)]
  51. protected ?Organization $organization = null;
  52. #[ORM\Column(length: 50, enumType: MessageStatusEnum::class, options: ['default' => 'DRAFT'])]
  53. protected MessageStatusEnum $status = MessageStatusEnum::DRAFT;
  54. // @todo remplacer par le nom sendingDate
  55. #[ORM\Column(type: 'datetime', nullable: true)]
  56. protected ?\DateTimeInterface $dateSent = null;
  57. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  58. protected ?string $about = null;
  59. #[ORM\Column(type: 'text', nullable: true)]
  60. protected ?string $text = null;
  61. /** @var Collection<int, File> */
  62. #[ORM\ManyToMany(targetEntity: File::class, cascade: ['persist'], orphanRemoval: true)]
  63. #[ORM\JoinTable(name: 'messages_files')]
  64. #[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id', onDelete: 'cascade')]
  65. #[ORM\InverseJoinColumn(name: 'file_id', referencedColumnName: 'id', onDelete: 'cascade')]
  66. protected Collection $files;
  67. /** @var Collection<int, ReportMessage> */
  68. #[ORM\OneToMany(targetEntity: ReportMessage::class, mappedBy: 'message', cascade: ['persist'], orphanRemoval: true)]
  69. protected Collection $reportMessage;
  70. /** @var Collection<int, Tagg> */
  71. #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'messages', cascade: ['persist'])]
  72. #[ORM\JoinTable(name: 'tag_message')]
  73. #[ORM\JoinColumn(name: 'message_id', referencedColumnName: 'id', onDelete: 'cascade')]
  74. #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id', onDelete: 'cascade')]
  75. protected Collection $tags;
  76. public function __construct()
  77. {
  78. $this->uuid = Uuid::uuid4()->toRfc4122();
  79. $this->files = new ArrayCollection();
  80. $this->tags = new ArrayCollection();
  81. $this->reportMessage = new ArrayCollection();
  82. }
  83. public function getId(): ?int
  84. {
  85. return $this->id;
  86. }
  87. public function getUuid(): ?UuidInterface
  88. {
  89. return $this->uuid;
  90. }
  91. public function getAuthor(): ?Access
  92. {
  93. return $this->author;
  94. }
  95. public function setAuthor(?Access $author): self
  96. {
  97. $this->author = $author;
  98. return $this;
  99. }
  100. public function setOrganization(Organization $organization): self
  101. {
  102. $this->organization = $organization;
  103. return $this;
  104. }
  105. public function getOrganization(): Organization
  106. {
  107. return $this->organization;
  108. }
  109. public function setAbout(string $about): self
  110. {
  111. $this->about = $about;
  112. return $this;
  113. }
  114. public function getAbout(): string
  115. {
  116. return $this->about;
  117. }
  118. public function setText(string $text): self
  119. {
  120. $this->text = $text;
  121. return $this;
  122. }
  123. public function getText(): string
  124. {
  125. return html_entity_decode($this->text);
  126. }
  127. public function setStatus(MessageStatusEnum $status): self
  128. {
  129. $this->status = $status;
  130. return $this;
  131. }
  132. public function getStatus(): MessageStatusEnum
  133. {
  134. return $this->status;
  135. }
  136. public function setDateSent(\DateTimeInterface $dateSent): self
  137. {
  138. $this->dateSent = $dateSent;
  139. return $this;
  140. }
  141. public function getDateSent(): ?\DateTimeInterface
  142. {
  143. return $this->dateSent;
  144. }
  145. /**
  146. * @return Collection<int, File>
  147. */
  148. public function getFiles(): Collection
  149. {
  150. return $this->files;
  151. }
  152. public function addFile(File $file): self
  153. {
  154. if (!$this->files->contains($file)) {
  155. $this->files[] = $file;
  156. // $file->addXXXX($this); // TODO: quel méthode?
  157. }
  158. return $this;
  159. }
  160. public function removeFile(File $file): self
  161. {
  162. if ($this->files->removeElement($file)) {
  163. // $file->removeXXX($this); // TODO: quel méthode?
  164. }
  165. return $this;
  166. }
  167. public function getReportMessage(): Collection
  168. {
  169. return $this->reportMessage;
  170. }
  171. public function addReportMessage(ReportMessage $reportMessage): self
  172. {
  173. if (!$this->reportMessage->contains($reportMessage)) {
  174. $this->reportMessage[] = $reportMessage;
  175. $reportMessage->setMessage($this);
  176. }
  177. return $this;
  178. }
  179. public function removeReportMessage(ReportMessage $reportMessage): self
  180. {
  181. if ($this->reportMessage->removeElement($reportMessage)) {
  182. $reportMessage->setMessage(null);
  183. }
  184. return $this;
  185. }
  186. public function getTags(): Collection
  187. {
  188. return $this->tags;
  189. }
  190. public function addTag(Tagg $tag): self
  191. {
  192. if (!$this->tags->contains($tag)) {
  193. $this->tags[] = $tag;
  194. }
  195. return $this;
  196. }
  197. public function removeTag(Tagg $tag): self
  198. {
  199. $this->tags->removeElement($tag);
  200. return $this;
  201. }
  202. }