Use DatetimeImmutable instead of DateTime wherever possible

This commit is contained in:
Jan Böhmer 2024-06-22 17:36:54 +02:00
parent eebc373734
commit 235d572f8c
39 changed files with 222 additions and 112 deletions

View file

@ -75,10 +75,10 @@ class ApiToken implements TimeStampableInterface
#[Groups('token:read')]
private ?User $user = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups('token:read')]
#[Year2038BugWorkaround]
private ?\DateTime $valid_until;
private ?\DateTimeImmutable $valid_until;
#[ORM\Column(length: 68, unique: true)]
private string $token;
@ -87,9 +87,9 @@ class ApiToken implements TimeStampableInterface
#[Groups('token:read')]
private ApiTokenLevel $level = ApiTokenLevel::READ_ONLY;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups('token:read')]
private ?\DateTime $last_time_used = null;
private ?\DateTimeImmutable $last_time_used = null;
public function __construct(ApiTokenType $tokenType = ApiTokenType::PERSONAL_ACCESS_TOKEN)
{
@ -97,7 +97,7 @@ class ApiToken implements TimeStampableInterface
$this->token = $tokenType->getTokenPrefix() . bin2hex(random_bytes(32));
//By default, tokens are valid for 1 year.
$this->valid_until = new \DateTime('+1 year');
$this->valid_until = new \DateTimeImmutable('+1 year');
}
public function getTokenType(): ApiTokenType
@ -116,7 +116,7 @@ class ApiToken implements TimeStampableInterface
return $this;
}
public function getValidUntil(): ?\DateTimeInterface
public function getValidUntil(): ?\DateTimeImmutable
{
return $this->valid_until;
}
@ -127,10 +127,10 @@ class ApiToken implements TimeStampableInterface
*/
public function isValid(): bool
{
return $this->valid_until === null || $this->valid_until > new \DateTime();
return $this->valid_until === null || $this->valid_until > new \DateTimeImmutable();
}
public function setValidUntil(?\DateTime $valid_until): ApiToken
public function setValidUntil(?\DateTimeImmutable $valid_until): ApiToken
{
$this->valid_until = $valid_until;
return $this;
@ -159,19 +159,17 @@ class ApiToken implements TimeStampableInterface
/**
* Gets the last time the token was used to authenticate or null if it was never used.
* @return \DateTimeInterface|null
*/
public function getLastTimeUsed(): ?\DateTimeInterface
public function getLastTimeUsed(): ?\DateTimeImmutable
{
return $this->last_time_used;
}
/**
* Sets the last time the token was used to authenticate.
* @param \DateTime|null $last_time_used
* @return ApiToken
*/
public function setLastTimeUsed(?\DateTime $last_time_used): ApiToken
public function setLastTimeUsed(?\DateTimeImmutable $last_time_used): ApiToken
{
$this->last_time_used = $last_time_used;
return $this;