Skip to content

Commit a7f4723

Browse files
Use Clock instead of DateTime (#354)
1 parent 8c9b6ac commit a7f4723

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"require": {
99
"php": ">=8.1.10",
1010
"symfony/config": "^5.4 | ^6.0 | ^7.0 | ^8.0",
11+
"symfony/clock": "^6.3 | ^7.0 | ^8.0",
1112
"symfony/dependency-injection": "^5.4 | ^6.0 | ^7.0 | ^8.0",
1213
"symfony/deprecation-contracts": "^2.2 | ^3.0",
1314
"symfony/http-kernel": "^5.4 | ^6.0 | ^7.0 | ^8.0"

src/Exception/TooManyPasswordRequestsException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Exception;
1111

12+
use Symfony\Component\Clock\Clock;
13+
1214
/**
1315
* @author Ryan Weaver <ryan@symfonycasts.com>
1416
*/
@@ -30,7 +32,7 @@ public function getAvailableAt(): \DateTimeInterface
3032

3133
public function getRetryAfter(): int
3234
{
33-
return $this->getAvailableAt()->getTimestamp() - (new \DateTime('now'))->getTimestamp();
35+
return $this->getAvailableAt()->getTimestamp() - Clock::get()->now()->getTimestamp();
3436
}
3537

3638
public function getReason(): string

src/Model/ResetPasswordRequestTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Doctrine\DBAL\Types\Types;
1313
use Doctrine\ORM\Mapping as ORM;
14+
use Symfony\Component\Clock\Clock;
1415

1516
/**
1617
* @author Jesse Rushlow <jr@rushlow.dev>
@@ -53,7 +54,7 @@ trait ResetPasswordRequestTrait
5354
/** @return void */
5455
protected function initialize(\DateTimeInterface $expiresAt, string $selector, string $hashedToken)
5556
{
56-
$this->requestedAt = new \DateTimeImmutable('now');
57+
$this->requestedAt = Clock::get()->now();
5758
$this->expiresAt = $expiresAt;
5859
$this->selector = $selector;
5960
$this->hashedToken = $hashedToken;

src/Persistence/Repository/ResetPasswordRequestRepositoryTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Persistence\Repository;
1111

12+
use Symfony\Component\Clock\Clock;
1213
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
1314

1415
/**
@@ -72,7 +73,7 @@ public function removeResetPasswordRequest(ResetPasswordRequestInterface $resetP
7273

7374
public function removeExpiredResetPasswordRequests(): int
7475
{
75-
$time = new \DateTimeImmutable('-1 week');
76+
$time = Clock::get()->now()->modify('-1 week');
7677
$query = $this->createQueryBuilder('t')
7778
->delete()
7879
->where('t.expiresAt <= :time')

src/ResetPasswordHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword;
1111

12+
use Symfony\Component\Clock\Clock;
1213
use SymfonyCasts\Bundle\ResetPassword\Exception\ExpiredResetPasswordTokenException;
1314
use SymfonyCasts\Bundle\ResetPassword\Exception\InvalidResetPasswordTokenException;
1415
use SymfonyCasts\Bundle\ResetPassword\Exception\TooManyPasswordRequestsException;
@@ -72,7 +73,7 @@ public function generateResetToken(object $user, ?int $resetRequestLifetime = nu
7273

7374
$resetRequestLifetime = $resetRequestLifetime ?? $this->resetRequestLifetime;
7475

75-
$expiresAt = new \DateTimeImmutable(\sprintf('+%d seconds', $resetRequestLifetime));
76+
$expiresAt = Clock::get()->now()->modify(\sprintf('+%d seconds', $resetRequestLifetime));
7677

7778
$generatedAt = ($expiresAt->getTimestamp() - $resetRequestLifetime);
7879

@@ -164,7 +165,7 @@ public function getTokenLifetime(): int
164165
public function generateFakeResetToken(?int $resetRequestLifetime = null): ResetPasswordToken
165166
{
166167
$resetRequestLifetime = $resetRequestLifetime ?? $this->resetRequestLifetime;
167-
$expiresAt = new \DateTimeImmutable(\sprintf('+%d seconds', $resetRequestLifetime));
168+
$expiresAt = Clock::get()->now()->modify(\sprintf('+%d seconds', $resetRequestLifetime));
168169

169170
$generatedAt = ($expiresAt->getTimestamp() - $resetRequestLifetime);
170171

@@ -191,7 +192,7 @@ private function hasUserHitThrottling(object $user): ?\DateTimeInterface
191192

192193
$availableAt = (clone $lastRequestDate)->add(new \DateInterval("PT{$this->requestThrottleTime}S"));
193194

194-
if ($availableAt > new \DateTime('now')) {
195+
if ($availableAt > Clock::get()->now()) {
195196
return $availableAt;
196197
}
197198

0 commit comments

Comments
 (0)