src/Entity/ChatMessage.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Annotation\ApiSubresource;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  10. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  11. use App\Repository\ChatMessageRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * @ORM\Entity(repositoryClass=ChatMessageRepository::class)
  20.  * @ApiResource(
  21.  *      mercure={
  22.  *          "private"=true,
  23.  *          "normalization_context"={
  24.  *              "groups"={
  25.  *                  "chat_message:read:id",  
  26.  *                    "chat_message:read:content", 
  27.  *                    "chat_message:read:received",
  28.  *                    "chat_message:read:read",
  29.  *                    "chat_message:read:createdAt",
  30.  *                    "chat_message:read:user", 
  31.  *                        "user:read:id",
  32.  *                        "user:read:firstName",
  33.  *                         "user:read:lastName",
  34.  *                        "user:read:client",
  35.  *                            "client:read:id",
  36.  *                            "client:read:firstName",
  37.  *                            "client:read:lastName",
  38.  *                            "client:read:avatar",
  39.  *                    "chat_message:read:attachments",
  40.  *                        "media_object:read:contentUrl"
  41.  *                }
  42.  *            }
  43.  *      },
  44.  *      order={"createdAt": "DESC"},
  45.  *      normalizationContext={
  46.  *          "groups"={"chat_message:read"}
  47.  *      },
  48.  *      denormalizationContext={
  49.  *          "groups"={
  50.  *              "chat_message:read",
  51.  *              "chat_message:read:attachments",
  52.  *              "media_object:read:contentUrl"
  53.  *          }
  54.  *      },
  55.  *      itemOperations={
  56.  *          "get"={
  57.  *              "security"="is_granted('ROLE_USER')"
  58.  *          }, 
  59.  *          "patch"={
  60.  *              "security"="is_granted('ROLE_USER')"
  61.  *          }, 
  62.  *          "delete"={
  63.  *              "security"="is_granted('ROLE_USER')"
  64.  *          }
  65.  *      },
  66.  *      collectionOperations={
  67.  *          "get"={
  68.  *              "security"="is_granted('ROLE_USER')"
  69.  *          }, 
  70.  *          "post"={
  71.  *              "security"="is_granted('ROLE_USER')",
  72.  *              "path"="/chat_messages",
  73.  *              "method"="POST",
  74.  *              "controller"="App\Controller\Api\ChatMessageController::post"
  75.  *          },
  76.  *          "get_by_chat"={
  77.  *             "description"="Get ChatMessages by Chat.",
  78.  *             "path"="/chats/{id}/chat_messages",
  79.  *             "method"="GET",
  80.  *             "controller"="App\Controller\Api\ChatMessageController::getChatMessagesByChat",
  81.  *             "security"="is_granted('ROLE_USER')"
  82.  *          }
  83.  *      }
  84.  * )
  85.  * @ApiFilter(SearchFilter::class, properties={
  86.  *      "chat.id", "user.id", "user.active"
  87.  * })
  88.  * @ApiFilter(OrderFilter::class)
  89.  * @ApiFilter(GroupFilter::class, 
  90.  *      arguments={
  91.  *          "parameterName": "groups", 
  92.  *          "overrideDefaultGroups": true
  93.  *      }
  94.  * )
  95.  *  @ApiFilter(PropertyFilter::class, 
  96.  *      arguments={
  97.  *          "parameterName"="fields", 
  98.  *          "overrideDefaultProperties"=true
  99.  *     }
  100.  * )
  101.  */
  102. class ChatMessage
  103. {
  104.     /**
  105.      * @ORM\Id
  106.      * @ORM\GeneratedValue
  107.      * @ORM\Column(type="integer")
  108.      * @ApiProperty(identifier=true)
  109.      * @Groups({"chat_message:read", "chat_message:read:id", "chat:read"})
  110.      */
  111.     private ?int $id null;
  112.     /**
  113.      * @ORM\ManyToOne(targetEntity=Chat::class, inversedBy="chatMessages", cascade={"persist", "refresh"})
  114.      * @Groups({"chat_message:read", "chat_message:write", "chat_message:read:chat"})
  115.      */
  116.     private ?Chat $chat null;
  117.     /**
  118.      * @ORM\ManyToOne(targetEntity=User::class)
  119.      * @Assert\NotNull
  120.      * @Groups({"chat_message:read", "chat_message:write", "chat_message:read:user", "chat:read"})
  121.      */
  122.     private ?User $user;
  123.     /**
  124.      * @ORM\Column(name="`content`", type="text", nullable=true)
  125.      * @Groups({"chat_message:read", "chat_message:write", "chat_message:read:content", "chat:read"})
  126.      */
  127.     private ?string $content "";
  128.     /**
  129.      * @ORM\ManyToMany(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
  130.      * @Groups({"chat_message:read", "chat_message:read:attachments", "chat:read"})
  131.      */
  132.     private $attachments;
  133.     /**
  134.      * @ORM\Column(type="datetime", nullable=true)
  135.      * @Gedmo\Timestampable(on="create")
  136.      * @Groups({"chat_message:read", "chat_message:read:createdAt", "chat:read"})
  137.      */
  138.     private $createdAt;
  139.     /**
  140.      * @ORM\Column(type="datetime", nullable=true)
  141.      * @Gedmo\Timestampable(on="update")
  142.      * @Groups({"chat_message:read", "chat_message:read:updatedAt", "chat:read"})
  143.      */
  144.     private $updatedAt;
  145.     /**
  146.      * Post Custom Field
  147.      * Permet d'envoyer un message à un autre utilisateur.
  148.      * @Groups({"chat_message:read", "chat_message:write"})
  149.      */
  150.     private $recipient;
  151.     /**
  152.      * Post Custom Field
  153.      * Permet d'envoyer un message à une Team.
  154.      * @Groups({"chat_message:read", "chat_message:write"})
  155.      */
  156.     private $team;
  157.     /**
  158.      * Post Custom Field
  159.      * @Groups({"chat_message:read""chat_message:read:received", "chat:read"})
  160.      */
  161.     private $received false;
  162.     /**
  163.      * Post Custom Field
  164.      * @Groups({"chat_message:read""chat_message:read:read", "chat:read"})
  165.      */
  166.     private $read false;
  167.     public function __construct()
  168.     {
  169.         $this->attachments = new ArrayCollection();
  170.     }
  171.     public function getId(): ?int
  172.     {
  173.         return $this->id;
  174.     }
  175.     public function getChat(): ?Chat
  176.     {
  177.         return $this->chat;
  178.     }
  179.     public function setChat(?Chat $chat): self
  180.     {
  181.         $this->chat $chat;
  182.         return $this;
  183.     }
  184.     public function getUser(): ?User
  185.     {
  186.         return $this->user;
  187.     }
  188.     public function setUser(?User $user): self
  189.     {
  190.         $this->user $user;
  191.         return $this;
  192.     }
  193.     public function getContent(): ?string
  194.     {
  195.         return $this->content;
  196.     }
  197.     public function setContent(?string $content): self
  198.     {
  199.         $this->content $content;
  200.         return $this;
  201.     }
  202.     /**
  203.      * @return Collection<int, MediaObject>
  204.      */
  205.     public function getAttachments(): Collection
  206.     {
  207.         return $this->attachments;
  208.     }
  209.     /**
  210.      * @Groups({"chat_message:write"})
  211.      */
  212.     public function setAttachmentFile($file): self
  213.     {
  214.         $mediaObject = new MediaObject();
  215.         $mediaObject->setFile($file);
  216.         $this->addAttachment($mediaObject);
  217.         return $this;
  218.     }
  219.     /**
  220.      * @Groups({"chat_message:write"})
  221.      */
  222.     public function setAttachmentFiles(?array $files): self
  223.     {
  224.         // Supprime tous les MediaObject 
  225.         if (!$this->attachments->isEmpty()) {
  226.             foreach ($this->attachments->toArray() as $attachment) {
  227.                 $this->removeAttachment($attachment);
  228.             }
  229.         }
  230.         foreach ($files as $file) {
  231.             $this->setAttachmentFile($file);
  232.         }
  233.         return $this;
  234.     }
  235.     public function addAttachment(MediaObject $attachment): self
  236.     {
  237.         if (!$this->attachments->contains($attachment)) {
  238.             $this->attachments[] = $attachment;
  239.         }
  240.         return $this;
  241.     }
  242.     public function removeAttachment(MediaObject $attachment): self
  243.     {
  244.         $this->attachments->removeElement($attachment);
  245.         return $this;
  246.     }
  247.     public function getReceived(): ?bool
  248.     {
  249.         return $this->received;
  250.     }
  251.     /**
  252.      * @Groups({"chat_message:read", "chat_message:read:isReceived"})
  253.      */
  254.     public function isReceived(): ?bool
  255.     {
  256.         return $this->received;
  257.     }
  258.     public function setReceived(?bool $received): self
  259.     {
  260.         $this->received $received;
  261.         return $this;
  262.     }
  263.     public function getRead(): ?bool
  264.     {
  265.         return $this->read;
  266.     }
  267.     /**
  268.      * @Groups({"chat_message:read", "chat_message:read:isRead"})
  269.      */
  270.     public function isRead(): ?bool
  271.     {
  272.         return $this->read;
  273.     }
  274.     public function setRead(?bool $read): self
  275.     {
  276.         $this->read $read;
  277.         return $this;
  278.     }
  279.     public function getCreatedAt(): ?\DateTimeInterface
  280.     {
  281.         return $this->createdAt;
  282.     }
  283.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  284.     {
  285.         $this->createdAt $createdAt;
  286.         return $this;
  287.     }
  288.     public function getUpdatedAt(): ?\DateTimeInterface
  289.     {
  290.         return $this->updatedAt;
  291.     }
  292.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  293.     {
  294.         $this->updatedAt $updatedAt;
  295.         return $this;
  296.     }
  297.     public function getTeam(): ?Team
  298.     {
  299.         return $this->team;
  300.     }
  301.     public function setTeam(?Team $team): self
  302.     {
  303.         $this->team $team;
  304.         return $this;
  305.     }
  306.     public function getRecipient(): ?User
  307.     {
  308.         return $this->recipient;
  309.     }
  310.     public function setRecipient(?User $recipient): self
  311.     {
  312.         $this->recipient $recipient;
  313.         return $this;
  314.     }
  315. }