<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\TeamRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* normalizationContext={
* "groups"={"team:read"}
* },
* denormalizationContext={
* "groups"={"team:write"}
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_USER')"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_USER')"
* },
* "post_update"={
* "security"="is_granted('ROLE_USER')",
* "path"="/teams/{id}",
* "description"="Update a Team with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\TeamController::update"
* },
* "get_team_by_user_teamplay"={
* "method"="GET",
* "security"="is_granted('ROLE_USER')",
* "path"="/users/{id}/teamplays/{teamplay}/team",
* "controller"="App\Controller\Api\TeamController::getTeamplayTeamByUser",
* "read"=false,
* "pagination_enabled"=false,
* "fetchEager"=true,
* "defaults"={"_api_receive"=false}
* }
* }
* )
* @ORM\Entity(repositoryClass=TeamRepository::class)
* @ApiFilter(SearchFilter::class, properties={
* "teamplay.id", "type", "active", "company.id",
* "name": "partial"
* })
* @ApiFilter(OrderFilter::class)
* @ApiFilter(GroupFilter::class,
* arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* }
* )
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Team
{
const TYPE_TEAMPLAY = "teamplay";
const TYPES = [
self::TYPE_TEAMPLAY
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"team:read", "team:read:id", "team_user:read", "chat:read", "chat_message:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"team:read", "team:write", "team:read:name", "team_user:read"})
*/
private $name;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"team:read", "team:write", "team:read:maxCapacity", "team_user:read"})
*/
private $maxCapacity;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"team:read", "team:write", "team:read:picture", "team_user:read"})
*/
private $picture;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Choice(choices=self::TYPES)
* @Groups({"team:read", "team:write", "team:read:type"})
*/
private $type = self::TYPE_TEAMPLAY;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": 1})
* @Groups({"team:read", "team:write", "team:read:active", "team_user:read"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="create")
* @Groups({"team:read", "team:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"team:read", "team:read:read"})
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="teams")
*/
private $tvCompany;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="teams")
* @Groups({"team:read", "team:write", "team:read:company"})
*/
private $company;
/**
* @ORM\OneToMany(targetEntity=TeamUser::class, mappedBy="team", orphanRemoval=true)
* @Groups({"team:read", "team:write", "team:read:teamUsers"})
* @ApiSubresource(maxDepth=1)
*/
private $teamUsers;
/**
* @ORM\ManyToOne(targetEntity=Teamplay::class, inversedBy="teams", cascade={"refresh"})
* @Groups({"team:read:teamplay", "team:read", "team:write"})
*/
private $teamplay;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"team:read:points", "team:read"})
*/
private $points;
/**
* @Groups({"team:read", "team:read:users"})
*/
private $users;
/**
* @Groups({"team:read", "team:read:activeUsers"})
*/
private $activeUsers = null;
/**
* @ORM\Column(name="`rank`", type="integer", nullable=true)
* @Groups({"team:read:rank", "team:read"})
*/
private $rank = null;
/**
* Custom field
* @var ?int
* @Groups({"team:read:maxRank", "team:read"})
*/
public $maxRank = null;
/**
* @var ?int
* @Groups({"team:read", "team:read:activeUsersCount"})
*/
public $activeUsersCount = null;
/**
* @var ?int
* @Groups({"team:read", "team:read:usersCount"})
*/
public $usersCount = null;
/**
* Cutsom field
* @var ?int
* @Groups({"team:read:", "team:read:remainingCapacity"})
*/
private $remainingCapacity;
/**
* @Groups({"team:read", "team:read:chat"})
*/
private $chat;
public function __construct()
{
$this->teamUsers = new ArrayCollection();
$this->users = new ArrayCollection();
$this->activeUsers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getMaxCapacity(): ?int
{
return $this->maxCapacity;
}
public function setMaxCapacity(?int $maxCapacity): self
{
$this->maxCapacity = $maxCapacity;
return $this;
}
public function getPicture(): ?MediaObject
{
return $this->picture;
}
public function setPicture(?MediaObject $picture): self
{
$this->picture = $picture;
return $this;
}
/**
* @Groups({"team:write"})
*/
public function setPictureFile($file = null): self
{
if ($file instanceof UploadedFile) {
$picture = empty($this->picture) ? new MediaObject : $this->picture;
$picture->setFile($file);
$this->setPicture($picture);
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getTvCompany(): ?TvCompany
{
return $this->tvCompany;
}
public function setTvCompany(?TvCompany $tvCompany): self
{
$this->tvCompany = $tvCompany;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* Permet de savoir si l'équipe est de type "Teamplay"
* @return bool
*/
public function isTeamplay(): bool
{
if ($this->type === self::TYPE_TEAMPLAY) return true;
return false;
}
/**
* @return Collection|TeamUser[]
*/
public function getTeamUsers(): Collection
{
return $this->teamUsers;
}
public function addTeamUser(TeamUser $teamUser): self
{
if (!empty($teamUser->getId()) && !$this->teamUsers->contains($teamUser)) {
$this->teamUsers[] = $teamUser;
$teamUser->setTeam($this);
} else {
$isContains = false;
// Vérifie si l'utilisateur est deja dans l'équipe
foreach ($this->teamUsers->toArray() as $value) {
if ($value->getTvUser()->getId() === $teamUser->getTvUser()->getId()) {
$isContains = true;
}
}
if (!$isContains) {
$this->teamUsers[] = $teamUser;
$teamUser->setTeam($this);
}
}
return $this;
}
public function removeTeamUser(TeamUser $teamUser): self
{
if ($this->teamUsers->removeElement($teamUser)) {
// set the owning side to null (unless already changed)
if ($teamUser->getTeam() === $this) {
$teamUser->setTeam(null);
}
}
return $this;
}
/**
* @return ArrayCollection|User[]
*/
public function getUsers(): ?ArrayCollection
{
$this->users = new ArrayCollection;
if (!empty($this->teamUsers->toArray())) {
foreach ($this->teamUsers->toArray() as $teamUser) {
$this->users->add($teamUser->getUser());
}
}
return $this->users;
}
public function getTeamplay(): ?Teamplay
{
return $this->teamplay;
}
public function setTeamplay(?Teamplay $teamplay): self
{
$this->teamplay = $teamplay;
return $this;
}
public function getRank(): ?int
{
return $this->rank;
}
public function setRank(?int $rank): self
{
$this->rank = $rank;
return $this;
}
public function getPoints(): ?int
{
return $this->points;
}
public function setPoints(?int $points): self
{
$this->points = $points;
return $this;
}
/**
* @return Collection|TeamUser[]
* /**
* Renvoie le nombre d'User
* @Groups({"team:read:members"})
*/
public function getMembers(): Collection
{
return $this->getTeamUsers();
}
/**
* Renvoie le nombre d'User
* @Groups({"team:read:membersCount"})
*/
public function getMembersCount(): ?int
{
return $this->teamUsers->count();
}
public function getActiveUsers(): Collection
{
$this->activeUsers = new ArrayCollection();
if ($this->getUsers()->count() > 0) {
foreach ($this->getTeamUsers() as $teamUser) {
if ($teamUser->getUser()->isActive()) {
$this->activeUsers->add($teamUser->getUser());
}
}
}
return $this->activeUsers;
}
public function getActiveUsersCount(): ?int
{
if ($this->getUsers()->count() > 0) {
foreach ($this->users as $user) {
if ($user->isActive()) {
$this->activeUsersCount++;
}
}
}
return $this->activeUsersCount;
}
public function setActiveUsersCount(?int $activeUsersCount): self
{
$this->activeUsersCount = $activeUsersCount;
return $this;
}
public function getUsersCount(): ?int
{
$this->usersCount = $this->users->count();
return $this->usersCount;
}
public function setUsersCount(?int $usersCount): self
{
$this->usersCount = $usersCount;
return $this;
}
public function getRemainingCapacity(): ?int
{
$this->remainingCapacity = $this->getMaxCapacity() - $this->getActiveUsersCount();
return $this->remainingCapacity;
}
public function setRemainingCapacity(?int $remainingCapacity): self
{
$this->remainingCapacity = $remainingCapacity;
return $this;
}
public function getChat(): ?Chat
{
return $this->chat;
}
public function setChat(?Chat $chat): self
{
$this->chat = $chat;
return $this;
}
}