src/Entity/Token.php line 29
<?phpnamespace App\Entity;use App\Repository\TokenRepository;use App\Security\Model\BelongUser;use App\Validator\Constraints as AssertApp;use Doctrine\ORM\Mapping as ORM;use Gedmo\Blameable\Traits\BlameableEntity;use Gedmo\Timestampable\Traits\TimestampableEntity;use Symfony\Component\Serializer\Annotation\Groups;use Symfony\Component\Validator\Constraints as Assert;use ApiPlatform\Metadata\ApiResource;use ApiPlatform\Metadata\Post;use App\Controller\Api\Auth\TokenController;use DateTime;#[ORM\Entity(TokenRepository::class)]#[ORM\Table(name: '`Token`')]#[ApiResource(operations: [new Post(uriTemplate: '/login',routeName: 'app_api_token_login',controller: TokenController::class,normalizationContext: ['groups' => ['token_get', 'token_get_login']])])]class Token implements BelongUser{use BlameableEntity;use TimestampableEntity;#[ORM\Id()]#[Groups(['token_get','user_get_me'])]#[ORM\Column(type: 'string', length: 128)]private ?string $id;#[ORM\OneToOne(inversedBy: 'token', targetEntity: RefreshToken::class, cascade: [ 'persist' ], fetch: 'EAGER')]#[ORM\JoinColumn(nullable: true)]#[Groups(['token_get','user_get_me',])]private ?RefreshToken $refreshToken;#[ORM\Column(type: "integer")]#[Groups(['token_get','user_get_me',])]private ?int $createTime;#[ORM\Column(type: "integer", nullable: true)]#[Groups(['token_get','user_get_me',])]#[AssertApp\IfNotEmpty([new Assert\Range(min: 60,groups: [ 'token_post' ],minMessage: 'Un token doit ĂȘtre valide au minimum 60 secondes')], groups: [ "token_post" ])]private ?int $lifetime;#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'tokens')]#[ORM\JoinColumn(nullable: true)]#[Groups(['token_get_login',])]private ?User $user;#[ORM\Column(type: 'string', nullable: true)]#[Groups(['token_get','user_get_me',])]#[Assert\NotNull(groups: [ 'token_post' ])]#[Assert\NotBlank(groups: [ 'token_post' ])]private ?string $name;public function __construct(?User $user = null,?int $lifetime = null,?int $refreshLifetime = null,string $name = null){$this->id = 'TOKEN_'.bin2hex(random_bytes(20));$this->createdAt = new DateTime();$this->updatedAt = new DateTime();$this->refreshToken = $refreshLifetime !== null ? new RefreshToken($user, $refreshLifetime) : null;$this->user = $user;$this->createTime = time();$this->lifetime = $lifetime;$this->name = $name;if ($user) {$user->addToken($this);}}public function getId(): string {return $this->id;}public function getRefreshToken(): ?RefreshToken {return $this->refreshToken;}public function getCreateTime(): int {return $this->createTime;}public function getLifetime(): ?int {return $this->lifetime;}public function getName(): ?string {return $this->name;}public function getUser(): User {return $this->user;}public function isValid(): bool {return $this->getLifetime() === null || ($this->getCreateTime() + $this->getLifetime() - (new \DateTime())->getTimestamp()) > 0;}}