src/Entity/Team.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use App\Filter\RestrictedGroupFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use App\Repository\TeamRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * POST : entreprise forcée et challenge contrôlé dans App\EventSubscriber\TeamCreateSubscriber.
  20.  * PATCH (membres de la team) : seuls le nom et la photo sont modifiables (groupe team:patch).
  21.  * PATCH et DELETE (compte de l'entreprise de la team, back-office manager) : App\Security\Voter\TeamVoter ;
  22.  * champs du groupe team:manager (App\Serializer\TeamManagerContextBuilder), challenge contrôlé dans TeamCreateSubscriber.
  23.  * Les inscriptions (teamUsers) passent uniquement par /api/team_users.
  24.  *
  25.  * @ApiResource(
  26.  *      normalizationContext={
  27.  *          "groups"={"team:read"}
  28.  *      },
  29.  *      denormalizationContext={
  30.  *          "groups"={"team:write"}
  31.  *      },
  32.  *      itemOperations={
  33.  *          "get"={
  34.  *              "security"="is_granted('ROLE_USER')"
  35.  *          }, 
  36.  *          "patch"={
  37.  *              "security"="is_granted('TEAM_MEMBER', object) or is_granted('TEAM_MANAGE', object)",
  38.  *              "denormalization_context"={"groups"={"team:patch"}}
  39.  *          },
  40.  *          "delete"={
  41.  *              "security"="is_granted('TEAM_MANAGE', object)"
  42.  *          }
  43.  *      },
  44.  *      collectionOperations={
  45.  *          "get"={
  46.  *              "security"="is_granted('ROLE_USER')"
  47.  *          }, 
  48.  *          "post"={
  49.  *              "security"="is_granted('ROLE_USER')"
  50.  *          },
  51.  *          "post_update"={
  52.  *              "security"="is_granted('ROLE_ADMIN')",
  53.  *              "path"="/teams/{id}",
  54.  *              "description"="Update a Team with method POST (using content-type: 'multipart')",
  55.  *              "method"="POST",
  56.  *              "controller"="App\Controller\Api\TeamController::update"
  57.  *          },
  58.  *          "get_team_by_user_teamplay"={
  59.  *              "method"="GET",
  60.  *              "security"="is_granted('ROLE_USER')",
  61.  *              "path"="/users/{id}/teamplays/{teamplay}/team",
  62.  *              "controller"="App\Controller\Api\TeamController::getTeamplayTeamByUser",
  63.  *              "read"=false,
  64.  *              "pagination_enabled"=false,
  65.  *              "fetchEager"=true,
  66.  *              "defaults"={"_api_receive"=false}
  67.  *          }
  68.  *      }
  69.  * )
  70.  * @ORM\Entity(repositoryClass=TeamRepository::class)
  71.  * @ApiFilter(SearchFilter::class, properties={
  72.  *      "teamplay.id", "type", "active", "company.id",
  73.  *      "name": "partial"
  74.  * })
  75.  * @ApiFilter(OrderFilter::class)
  76.  * @ApiFilter(RestrictedGroupFilter::class,
  77.  *      arguments={
  78.  *          "parameterName": "groups",
  79.  *          "overrideDefaultGroups": true,
  80.  *          "whitelist": {"team:read", "team:read:id", "team:read:name", "team:read:active", "team:read:teamplay", "team:read:maxCapacity", "team:read:picture", "teamplay:read:id", "teamplay:read:name", "media_object:read:contentUrl"}
  81.  *      }
  82.  * )
  83.  *  @ApiFilter(PropertyFilter::class, 
  84.  *      arguments={
  85.  *          "parameterName"="fields", 
  86.  *          "overrideDefaultProperties"=true
  87.  *     }
  88.  * )
  89.  */
  90. class Team
  91. {
  92.     const TYPE_TEAMPLAY = "teamplay";
  93.     const TYPES = [
  94.         self::TYPE_TEAMPLAY
  95.     ];
  96.     /**
  97.      * @ORM\Id
  98.      * @ORM\GeneratedValue
  99.      * @ORM\Column(type="integer")
  100.      * @Groups({"team:read", "team:read:id", "team_user:read", "chat:read", "chat_message:read"})
  101.      */
  102.     private $id;
  103.     /**
  104.      * @ORM\Column(type="string", length=255, nullable=true)
  105.      * @Groups({"team:read", "team:write", "team:patch", "team:manager", "team:read:name", "team_user:read"})
  106.      */
  107.     private $name;
  108.     /**
  109.      * @ORM\Column(type="integer", nullable=true)
  110.      * @Groups({"team:read", "team:write", "team:manager", "team:read:maxCapacity", "team_user:read"})
  111.      */
  112.     private $maxCapacity;
  113.     /**
  114.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  115.      * @Groups({"team:read", "team:write", "team:patch", "team:manager", "team:read:picture", "team_user:read"})
  116.      */
  117.     private $picture;
  118.     /**
  119.      * @ORM\Column(type="string", length=255, nullable=true)
  120.      * @Assert\Choice(choices=self::TYPES)
  121.      * @Groups({"team:read", "team:write", "team:read:type"})
  122.      */
  123.     private $type = self::TYPE_TEAMPLAY;
  124.     /**
  125.      * @ORM\Column(type="boolean", nullable=true, options={"default": 1})
  126.      * @Groups({"team:read", "team:write", "team:read:active", "team_user:read"})
  127.      */
  128.     private $active = true;
  129.     /**
  130.      * @ORM\Column(type="datetime", nullable=true)
  131.      * @Gedmo\Timestampable(on="create")
  132.      * @Groups({"team:read", "team:read:createdAt"})
  133.      */
  134.     private $createdAt;
  135.     /**
  136.      * @ORM\Column(type="datetime", nullable=true)
  137.      * @Gedmo\Timestampable(on="update")
  138.      * @Groups({"team:read", "team:read:read"})
  139.      */
  140.     private $updatedAt;
  141.     /**
  142.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="teams")
  143.      */
  144.     private $tvCompany;
  145.     /**
  146.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="teams")
  147.      * @Groups({"team:read", "team:write", "team:read:company"})
  148.      */
  149.     private $company;
  150.     /**
  151.      * Dernier membre qui a créé ou renommé la team, ou changé sa photo : l'auteur visé par un signalement.
  152.      * Renseigné par App\EventSubscriber\TeamContentSubscriber, non exposé.
  153.      *
  154.      * @ORM\ManyToOne(targetEntity=User::class)
  155.      * @ORM\JoinColumn(onDelete="SET NULL")
  156.      */
  157.     private ?User $lastEditedBy = null;
  158.     /**
  159.      * @ORM\OneToMany(targetEntity=TeamUser::class, mappedBy="team", orphanRemoval=true)
  160.      * @Groups({"team:read", "team:read:teamUsers"})
  161.      * @ApiSubresource(maxDepth=1)
  162.      */
  163.     private $teamUsers;
  164.     /**
  165.      * @ORM\ManyToOne(targetEntity=Teamplay::class, inversedBy="teams", cascade={"refresh"})
  166.      * @Groups({"team:read:teamplay", "team:read", "team:write", "team:manager"})
  167.      */
  168.     private $teamplay;
  169.     /**
  170.      * @ORM\Column(type="integer", nullable=true)
  171.      * @Groups({"team:read:points", "team:read"})
  172.      */
  173.     private $points;
  174.     /**
  175.      * @Groups({"team:read", "team:read:users"})
  176.      */
  177.     private $users;
  178.     /**
  179.      * @Groups({"team:read", "team:read:activeUsers"})
  180.      */
  181.     private $activeUsers = null;
  182.     /**
  183.      * @ORM\Column(name="`rank`", type="integer", nullable=true)
  184.      * @Groups({"team:read:rank", "team:read"})
  185.      */
  186.     private $rank = null;
  187.     /**
  188.      * Custom field
  189.      * @var ?int
  190.      * @Groups({"team:read:maxRank", "team:read"})
  191.      */
  192.     public $maxRank = null;
  193.     /**
  194.      * @var ?int
  195.      * @Groups({"team:read", "team:read:activeUsersCount"})
  196.      */
  197.     public $activeUsersCount = null;
  198.     /**
  199.      * @var ?int
  200.      * @Groups({"team:read", "team:read:usersCount"})
  201.      */
  202.     public $usersCount = null;
  203.     /**
  204.      * Cutsom field
  205.      * @var ?int
  206.      * @Groups({"team:read:", "team:read:remainingCapacity"})
  207.      */
  208.     private $remainingCapacity;
  209.     /**
  210.      * @Groups({"team:read", "team:read:chat"})
  211.      */
  212.     private $chat;
  213.     public function __construct()
  214.     {
  215.         $this->teamUsers = new ArrayCollection();
  216.         $this->users = new ArrayCollection();
  217.         $this->activeUsers = new ArrayCollection();
  218.     }
  219.     public function getId(): ?int
  220.     {
  221.         return $this->id;
  222.     }
  223.     public function getName(): ?string
  224.     {
  225.         return $this->name;
  226.     }
  227.     public function setName(?string $name): self
  228.     {
  229.         $this->name = $name;
  230.         return $this;
  231.     }
  232.     public function getMaxCapacity(): ?int
  233.     {
  234.         return $this->maxCapacity;
  235.     }
  236.     public function setMaxCapacity(?int $maxCapacity): self
  237.     {
  238.         $this->maxCapacity = $maxCapacity;
  239.         return $this;
  240.     }
  241.     public function getPicture(): ?MediaObject
  242.     {
  243.         return $this->picture;
  244.     }
  245.     public function setPicture(?MediaObject $picture): self
  246.     {
  247.         $this->picture = $picture;
  248.         return $this;
  249.     }
  250.     public function getLastEditedBy(): ?User
  251.     {
  252.         return $this->lastEditedBy;
  253.     }
  254.     public function setLastEditedBy(?User $lastEditedBy): self
  255.     {
  256.         $this->lastEditedBy = $lastEditedBy;
  257.         return $this;
  258.     }
  259.     /**
  260.      * @Groups({"team:write", "team:patch"})
  261.      */
  262.     public function setPictureFile($file = null): self
  263.     {
  264.         if ($file instanceof UploadedFile) {
  265.             $picture = empty($this->picture) ? new MediaObject : $this->picture;
  266.             $picture->setFile($file);
  267.             $this->setPicture($picture);
  268.         }
  269.         return $this;
  270.     }
  271.     public function getType(): ?string
  272.     {
  273.         return $this->type;
  274.     }
  275.     public function setType(?string $type): self
  276.     {
  277.         $this->type = $type;
  278.         return $this;
  279.     }
  280.     public function getActive(): ?bool
  281.     {
  282.         return $this->active;
  283.     }
  284.     public function isActive(): ?bool
  285.     {
  286.         return $this->active;
  287.     }
  288.     public function setActive(?bool $active): self
  289.     {
  290.         $this->active = $active;
  291.         return $this;
  292.     }
  293.     public function getCreatedAt(): ?\DateTimeInterface
  294.     {
  295.         return $this->createdAt;
  296.     }
  297.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  298.     {
  299.         $this->createdAt = $createdAt;
  300.         return $this;
  301.     }
  302.     public function getUpdatedAt(): ?\DateTimeInterface
  303.     {
  304.         return $this->updatedAt;
  305.     }
  306.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  307.     {
  308.         $this->updatedAt = $updatedAt;
  309.         return $this;
  310.     }
  311.     public function getTvCompany(): ?TvCompany
  312.     {
  313.         return $this->tvCompany;
  314.     }
  315.     public function setTvCompany(?TvCompany $tvCompany): self
  316.     {
  317.         $this->tvCompany = $tvCompany;
  318.         return $this;
  319.     }
  320.     public function getCompany(): ?Company
  321.     {
  322.         return $this->company;
  323.     }
  324.     public function setCompany(?Company $company): self
  325.     {
  326.         $this->company = $company;
  327.         return $this;
  328.     }
  329.     /**
  330.      * Permet de savoir si l'équipe est de type "Teamplay"
  331.      * @return bool
  332.      */
  333.     public function isTeamplay(): bool
  334.     {
  335.         if ($this->type === self::TYPE_TEAMPLAY) return true;
  336.         return false;
  337.     }
  338.     /**
  339.      * @return Collection|TeamUser[]
  340.      */
  341.     public function getTeamUsers(): Collection
  342.     {
  343.         return $this->teamUsers;
  344.     }
  345.     public function addTeamUser(TeamUser $teamUser): self
  346.     {
  347.         if (!empty($teamUser->getId()) && !$this->teamUsers->contains($teamUser)) {
  348.             $this->teamUsers[] = $teamUser;
  349.             $teamUser->setTeam($this);
  350.         } else {
  351.             $isContains = false;
  352.             // Vérifie si l'utilisateur est deja dans l'équipe
  353.             foreach ($this->teamUsers->toArray() as $value) {
  354.                 if ($value->getTvUser()->getId() === $teamUser->getTvUser()->getId()) {
  355.                     $isContains = true;
  356.                 }
  357.             }
  358.             if (!$isContains) {
  359.                 $this->teamUsers[] = $teamUser;
  360.                 $teamUser->setTeam($this);
  361.             }
  362.         }
  363.         return $this;
  364.     }
  365.     public function removeTeamUser(TeamUser $teamUser): self
  366.     {
  367.         if ($this->teamUsers->removeElement($teamUser)) {
  368.             // set the owning side to null (unless already changed)
  369.             if ($teamUser->getTeam() === $this) {
  370.                 $teamUser->setTeam(null);
  371.             }
  372.         }
  373.         return $this;
  374.     }
  375.     /**
  376.      * @return ArrayCollection|User[]
  377.      */
  378.     public function getUsers(): ?ArrayCollection
  379.     {
  380.         $this->users = new ArrayCollection;
  381.         if (!empty($this->teamUsers->toArray())) {
  382.             foreach ($this->teamUsers->toArray() as $teamUser) {
  383.                 $this->users->add($teamUser->getUser());
  384.             }
  385.         }
  386.         return $this->users;
  387.     }
  388.     public function getTeamplay(): ?Teamplay
  389.     {
  390.         return $this->teamplay;
  391.     }
  392.     public function setTeamplay(?Teamplay $teamplay): self
  393.     {
  394.         $this->teamplay = $teamplay;
  395.         return $this;
  396.     }
  397.     public function getRank(): ?int
  398.     {
  399.         return $this->rank;
  400.     }
  401.     public function setRank(?int $rank): self
  402.     {
  403.         $this->rank = $rank;
  404.         return $this;
  405.     }
  406.     public function getPoints(): ?int
  407.     {
  408.         return $this->points;
  409.     }
  410.     public function setPoints(?int $points): self
  411.     {
  412.         $this->points = $points;
  413.         return $this;
  414.     }
  415.     /**
  416.      * @return Collection|TeamUser[]
  417.      * /**
  418.      * Renvoie le nombre d'User
  419.      * @Groups({"team:read:members"})
  420.      */
  421.     public function getMembers(): Collection
  422.     {
  423.         return $this->getTeamUsers();
  424.     }
  425.     /**
  426.      * Renvoie le nombre d'User
  427.      * @Groups({"team:read:membersCount"})
  428.      */
  429.     public function getMembersCount(): ?int
  430.     {
  431.         return $this->teamUsers->count();
  432.     }
  433.     public function getActiveUsers(): Collection
  434.     {
  435.         $this->activeUsers = new ArrayCollection();
  436.         if ($this->getUsers()->count() > 0) {
  437.             foreach ($this->getTeamUsers() as $teamUser) {
  438.                 if ($teamUser->getUser()->isActive()) {
  439.                     $this->activeUsers->add($teamUser->getUser());
  440.                 }
  441.             }
  442.         }
  443.         return $this->activeUsers;
  444.     }
  445.     public function getActiveUsersCount(): ?int
  446.     {
  447.         if ($this->getUsers()->count() > 0) {
  448.             foreach ($this->users as $user) {
  449.                 if ($user->isActive()) {
  450.                     $this->activeUsersCount++;
  451.                 }
  452.             }
  453.         }
  454.         return $this->activeUsersCount;
  455.     }
  456.     public function setActiveUsersCount(?int $activeUsersCount): self
  457.     {
  458.         $this->activeUsersCount = $activeUsersCount;
  459.         return $this;
  460.     }
  461.     public function getUsersCount(): ?int
  462.     {
  463.         $this->usersCount = $this->users->count();
  464.         return $this->usersCount;
  465.     }
  466.     public function setUsersCount(?int $usersCount): self
  467.     {
  468.         $this->usersCount = $usersCount;
  469.         return $this;
  470.     }
  471.     public function getRemainingCapacity(): ?int
  472.     {
  473.         $this->remainingCapacity = $this->getMaxCapacity() - $this->getActiveUsersCount();
  474.         return $this->remainingCapacity;
  475.     }
  476.     public function setRemainingCapacity(?int $remainingCapacity): self
  477.     {
  478.         $this->remainingCapacity = $remainingCapacity;
  479.         return $this;
  480.     }
  481.     public function getChat(): ?Chat
  482.     {
  483.         return $this->chat;
  484.     }
  485.     public function setChat(?Chat $chat): self
  486.     {
  487.         $this->chat = $chat;
  488.         return $this;
  489.     }
  490. }