src/Entity/Chat.php line 102

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\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  10. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  11. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  12. use App\Repository\ChatRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Mapping\Annotation as Gedmo;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * @ORM\Entity(repositoryClass=ChatRepository::class)
  21.  * @ApiResource(
  22.  *      mercure={
  23.  *          "private"=true,
  24.  *          "normalization_context"={
  25.  *              "groups"={
  26.  *                    "chat:read:id",
  27.  *                    "chat:read:countChatMessages", 
  28.  *                    "chat:read:unreadChatMessageCount", 
  29.  *                    "chat:read:active", 
  30.  *                    "chat:read:read", 
  31.  *                    "chat:read:lastChatMessage", 
  32.  *                    "chat_message:read:id",  
  33.  *                    "chat_message:read:content", 
  34.  *                    "chat_message:read:received",
  35.  *                    "chat_message:read:read",
  36.  *                    "chat_message:read:createdAt",
  37.  *                    "chat_message:read:user", 
  38.  *                        "user:read:id",
  39.  *                        "user:read:firstName",
  40.  *                        "user:read:lastName",
  41.  *                        "user:read:client",
  42.  *                            "client:read:id",
  43.  *                            "client:read:firstName",
  44.  *                            "client:read:lastName",
  45.  *                            "client:read:avatar",
  46.  *                    "chat_message:read:attachments",
  47.  *                        "media_object:read:contentUrl"
  48.  *                  }
  49.  *              }
  50.  *      },
  51.  *      normalizationContext={
  52.  *          "groups"={"chat:read"}
  53.  *      },
  54.  *      denormalizationContext={
  55.  *          "groups"={"chat:write"}
  56.  *      },
  57.  *      itemOperations={
  58.  *          "get"={
  59.  *              "security"="is_granted('ROLE_USER')"
  60.  *          }, 
  61.  *          "patch"={
  62.  *              "security"="is_granted('ROLE_USER')"
  63.  *          }, 
  64.  *          "delete"={
  65.  *              "security"="is_granted('ROLE_USER')"
  66.  *          }
  67.  *      },
  68.  *      collectionOperations={
  69.  *          "get"={
  70.  *              "security"="is_granted('ROLE_USER')"
  71.  *          }, 
  72.  *          "post"={
  73.  *              "security"="is_granted('ROLE_USER')"
  74.  *          },
  75.  *          "get_by_user_and_recipient"={
  76.  *              "security"="is_granted('ROLE_USER')",
  77.  *              "path"="/users/{user}/teams/{team}/recipients/{recipient}/chat",
  78.  *              "method"="GET",
  79.  *              "controller"="App\Controller\Api\ChatController::getChatByTeamAndUserAndRecipient"
  80.  *          }
  81.  *      }
  82.  * )
  83.  * @ApiFilter(SearchFilter::class, properties={"team.id", "active", "company.id", "type", "users.id", "countUsers", "countChatMessages"})
  84.  * @ApiFilter(ExistsFilter::class, properties={"type", "countUsers", "team", "company", "countChatMessages"})
  85.  * @ApiFilter(OrderFilter::class)
  86.  * @ApiFilter(GroupFilter::class, 
  87.  *      arguments={
  88.  *          "parameterName": "groups", 
  89.  *          "overrideDefaultGroups": true
  90.  *      }
  91.  * )
  92.  *  @ApiFilter(PropertyFilter::class, 
  93.  *      arguments={
  94.  *          "parameterName"="fields", 
  95.  *          "overrideDefaultProperties"=true
  96.  *     }
  97.  * )
  98.  */
  99. class Chat
  100. {
  101.     const TYPE_GLOBAL "global";
  102.     const TYPE_TEAM "team";
  103.     const TYPE_MAIN "main";
  104.     const TYPES = [
  105.         self::TYPE_GLOBAL => self::TYPE_GLOBAL,
  106.         self::TYPE_TEAM => self::TYPE_TEAM,
  107.     ];
  108.     /**
  109.      * @ORM\Id
  110.      * @ORM\GeneratedValue
  111.      * @ORM\Column(type="integer")
  112.      * @ApiProperty(identifier=true)
  113.      * @Groups({"chat:read", "chat:read:id", "team:read", "team_user:read"})
  114.      */
  115.     private ?int $id null;
  116.     /**
  117.      * @ORM\Column(type="string", length=255, nullable=true, options={"default": "global"})
  118.      * @Groups({"chat:read", "chat:read:type", "client:read", "user:read", "company:read", "team_user:read", "team:read"})
  119.      * @Assert\NotBlank
  120.      */
  121.     private ?string $type self::TYPE_GLOBAL;
  122.     /**
  123.      * @ORM\ManyToMany(targetEntity=User::class, mappedBy="chats")
  124.      * @Groups({"chat:read", "chat:write", "chat:read:users"})
  125.      */
  126.     private $users;
  127.     /**
  128.      * @ORM\Column(type="integer", nullable=true, options={"default": 0})
  129.      * @Groups({"chat:read", "chat:read:countUsers", "team:read"})
  130.      */
  131.     private ?int $countUsers 0;
  132.     /**
  133.      * @ORM\ManyToOne(targetEntity=Team::class)
  134.      * @Groups({"chat:read", "chat:write", "chat:read:team"})
  135.      */
  136.     private ?Team $team;
  137.     /**
  138.      * @ORM\ManyToOne(targetEntity=Company::class)
  139.      * @Groups({"chat:read", "chat:write", "chat:read:company"})
  140.      */
  141.     private ?Company $company null;
  142.     /**
  143.      * @ORM\OneToMany(targetEntity=ChatMessage::class, mappedBy="chat", orphanRemoval=true)
  144.      * @Groups({"chat:read", "chat:read:chatMessages"})
  145.      */
  146.     private $chatMessages;
  147.     /**
  148.      * @ORM\Column(type="integer", nullable=true, options={"default": 0})
  149.      * @Groups({"chat:read", "chat:read:countChatMessages", "team:read"})
  150.      */
  151.     private ?int $countChatMessages 0;
  152.     /**
  153.      * @ORM\Column(type="boolean", nullable=true, options={"default": 1})
  154.      * @Groups({"chat:read", "chat:write", "chat:read:active", "team:read"})
  155.      */
  156.     private $active true;
  157.     /**
  158.      * @ORM\Column(type="datetime", nullable=true)
  159.      * @Gedmo\Timestampable(on="create")
  160.      * @Groups({"chat:read", "chat:read:createdAt"})
  161.      */
  162.     private $createdAt;
  163.     /**
  164.      * @ORM\Column(type="datetime", nullable=true)
  165.      * @Gedmo\Timestampable(on="update")
  166.      * @Groups({"chat:read", "chat:read:updatedAt"})
  167.      */
  168.     private $updatedAt;
  169.     /**
  170.      * @Groups({"chat:read", "chat:read:lastChatMessage", "team:read"})
  171.      * @var ?ChatMessage
  172.      */
  173.     private $lastChatMessage null;
  174.     /**
  175.      * Custom field
  176.      * @var ?string
  177.      * @Groups({"chat:read", "chat:read:name", "team:read"})
  178.      */
  179.     public $name null;
  180.     /**
  181.      * Custom field
  182.      * @var ?MediaObject
  183.      * @Groups({"chat:read", "chat:read:image", "team:read"})
  184.      */
  185.     public $image null;
  186.     /**
  187.      * Post Custom Field
  188.      * @Groups({"chat:read", "chat:read:read"})
  189.      */
  190.     private bool $read false;
  191.     /**
  192.      * Post Custom Field
  193.      * @Groups({"chat:read", "chat:read:unreadChatMessageCount"})
  194.      */
  195.     private ?int $unreadChatMessageCount null;
  196.     public function __construct()
  197.     {
  198.         $this->users = new ArrayCollection();
  199.         $this->chatMessages = new ArrayCollection();
  200.     }
  201.     public function getId(): ?int
  202.     {
  203.         return $this->id;
  204.     }
  205.     public function getType(): ?string
  206.     {
  207.         return $this->type;
  208.     }
  209.     public function setType(?string $type): self
  210.     {
  211.         $this->type $type;
  212.         return $this;
  213.     }
  214.     /**
  215.      * @return Collection<int, User>
  216.      */
  217.     public function getUsers(): Collection
  218.     {
  219.         return $this->users;
  220.     }
  221.     public function addUser(User $user): self
  222.     {
  223.         if (!$this->users->contains($user)) {
  224.             $this->users[] = $user;
  225.             $user->addChat($this);
  226.         }
  227.         return $this;
  228.     }
  229.     public function removeUser(User $user): self
  230.     {
  231.         if ($this->users->removeElement($user)) {
  232.             $user->removeChat($this);
  233.         }
  234.         return $this;
  235.     }
  236.     public function getCountUsers(): ?int
  237.     {
  238.         return $this->countUsers;
  239.     }
  240.     public function setCountUsers(?int $countUsers): self
  241.     {
  242.         $this->countUsers $countUsers;
  243.         return $this;
  244.     }
  245.     public function getTeam(): ?Team
  246.     {
  247.         return $this->team;
  248.     }
  249.     public function setTeam(?Team $team): self
  250.     {
  251.         $this->team $team;
  252.         return $this;
  253.     }
  254.     public function getCompany(): ?Company
  255.     {
  256.         return $this->company;
  257.     }
  258.     public function setCompany(?Company $company): self
  259.     {
  260.         $this->company $company;
  261.         return $this;
  262.     }
  263.     /**
  264.      * @return Collection|ChatMessage[]
  265.      */
  266.     public function getChatMessages(): Collection
  267.     {
  268.         return $this->chatMessages;
  269.     }
  270.     public function addChatMessage(ChatMessage $chatMessage): self
  271.     {
  272.         if ($chatMessage instanceof ChatMessage) {
  273.             if (!$this->chatMessages->contains($chatMessage)) {
  274.                 $this->chatMessages[] = $chatMessage;
  275.                 $chatMessage->setChat($this);
  276.             }
  277.         }
  278.         return $this;
  279.     }
  280.     public function removeChatMessage(ChatMessage $chatMessage): self
  281.     {
  282.         if ($this->chatMessages->removeElement($chatMessage)) {
  283.             // set the owning side to null (unless already changed)
  284.             if ($chatMessage->getChat() === $this) {
  285.                 $chatMessage->setChat(null);
  286.             }
  287.         }
  288.         return $this;
  289.     }
  290.     public function getCountChatMessages(): ?int
  291.     {
  292.         return $this->countChatMessages;
  293.     }
  294.     public function setCountChatMessages(?int $countChatMessages): self
  295.     {
  296.         $this->countChatMessages $countChatMessages;
  297.         return $this;
  298.     }
  299.     public function getActive(): ?bool
  300.     {
  301.         return $this->active;
  302.     }
  303.     public function isActive(): ?bool
  304.     {
  305.         return $this->active;
  306.     }
  307.     public function setActive(?bool $active): self
  308.     {
  309.         $this->active $active;
  310.         return $this;
  311.     }
  312.     public function getCreatedAt(): ?\DateTimeInterface
  313.     {
  314.         return $this->createdAt;
  315.     }
  316.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  317.     {
  318.         $this->createdAt $createdAt;
  319.         return $this;
  320.     }
  321.     public function getUpdatedAt(): ?\DateTimeInterface
  322.     {
  323.         return $this->updatedAt;
  324.     }
  325.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  326.     {
  327.         $this->updatedAt $updatedAt;
  328.         return $this;
  329.     }
  330.     public function getLastChatMessage(): ?ChatMessage
  331.     {
  332.         if (!$this->chatMessages->isEmpty()) {
  333.             $this->lastChatMessage $this->chatMessages->last();
  334.         }
  335.         return $this->lastChatMessage;
  336.     }
  337.     public function setLastChatMessage(?ChatMessage $lastChatMessage): self
  338.     {
  339.         $this->lastChatMessage $lastChatMessage;
  340.         return $this;
  341.     }
  342.     public function getRead(): ?bool
  343.     {
  344.         return $this->read;
  345.     }
  346.     /**
  347.      * @Groups({"chat:read", "chat:read:isRead"})
  348.      */
  349.     public function isRead(): ?bool
  350.     {
  351.         return $this->read;
  352.     }
  353.     public function setRead(?bool $read): self
  354.     {
  355.         $this->read $read;
  356.         return $this;
  357.     }
  358.     public function getUnreadChatMessageCount(): ?int
  359.     {
  360.         return $this->unreadChatMessageCount;
  361.     }
  362.     public function setUnreadChatMessageCount(?int $unreadChatMessageCount): self
  363.     {
  364.         $this->unreadChatMessageCount $unreadChatMessageCount;
  365.         return $this;
  366.     }
  367. }