src/Entity/Team.php line 85

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