src/Entity/TeamInvitation.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  6. use App\Repository\TeamInvitationRepository;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. use Symfony\Component\Uid\Uuid;
  10. /**
  11.  * @ORM\Entity(repositoryClass=TeamInvitationRepository::class)
  12.  * @ORM\HasLifecycleCallbacks()
  13.  * @ApiResource(
  14.  *     normalizationContext={"groups"={"team_invitation:read"}},
  15.  *     collectionOperations={
  16.  *         "post"={
  17.  *             "security"="is_granted('ROLE_USER')",
  18.  *             "controller"="App\Controller\Api\TeamInvitationController::create",
  19.  *             "deserialize"=false
  20.  *         }
  21.  *     },
  22.  *     itemOperations={
  23.  *         "get"={
  24.  *             "security"="is_granted('ROLE_USER')",
  25.  *             "normalization_context"={"groups"={"team_invitation:read:public"}},
  26.  *             "controller"="App\Controller\Api\TeamInvitationController::getByToken"
  27.  *         }
  28.  *     }
  29.  * )
  30.  * @ApiFilter(PropertyFilter::class, arguments={"parameterName"="fields", "overrideDefaultProperties"=true})
  31.  */
  32. class TeamInvitation
  33. {
  34.     const EXPIRATION_HOURS = 24;
  35.     /**
  36.      * @ORM\Id
  37.      * @ORM\GeneratedValue
  38.      * @ORM\Column(type="integer")
  39.      * @Groups({"team_invitation:read"})
  40.      */
  41.     private $id;
  42.     /**
  43.      * @ORM\Column(type="string", length=64, unique=true)
  44.      * @Groups({"team_invitation:read", "team_invitation:read:public"})
  45.      */
  46.     private $token;
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity=Team::class)
  49.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  50.      * @Groups({"team_invitation:read", "team_invitation:read:public"})
  51.      */
  52.     private $team;
  53.     /**
  54.      * @ORM\ManyToOne(targetEntity=User::class)
  55.      * @ORM\JoinColumn(nullable=false)
  56.      * @Groups({"team_invitation:read"})
  57.      */
  58.     private $createdBy;
  59.     /**
  60.      * @ORM\Column(type="datetime")
  61.      * @Groups({"team_invitation:read", "team_invitation:read:public"})
  62.      */
  63.     private $expiresAt;
  64.     /**
  65.      * @ORM\Column(type="datetime")
  66.      * @Groups({"team_invitation:read"})
  67.      */
  68.     private $createdAt;
  69.     /**
  70.      * @ORM\PrePersist
  71.      */
  72.     public function onPrePersist(): void
  73.     {
  74.         $this->token = Uuid::v4()->toBase58();
  75.         $this->createdAt = new \DateTime();
  76.         $this->expiresAt = (new \DateTime())->modify('+'.self::EXPIRATION_HOURS.' hours');
  77.     }
  78.     public function isValid(): bool
  79.     {
  80.         if (new \DateTime() > $this->expiresAt) {
  81.             return false;
  82.         }
  83.         if (!$this->team || !$this->team->isActive()) {
  84.             return false;
  85.         }
  86.         $teamplay = $this->team->getTeamplay();
  87.         if (!$teamplay || !$teamplay->isActive() || !$teamplay->isInProgress()) {
  88.             return false;
  89.         }
  90.         return true;
  91.     }
  92.     // Getters / Setters
  93.     public function getId(): ?int { return $this->id; }
  94.     public function getToken(): ?string { return $this->token; }
  95.     public function getTeam(): ?Team { return $this->team; }
  96.     public function setTeam(?Team $team): self { $this->team = $team; return $this; }
  97.     public function getCreatedBy(): ?User { return $this->createdBy; }
  98.     public function setCreatedBy(?User $user): self { $this->createdBy = $user; return $this; }
  99.     public function getExpiresAt(): ?\DateTimeInterface { return $this->expiresAt; }
  100.     public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; }
  101. }