Skip to content

Commit dd214c5

Browse files
authored
feature #308 [token] throws ResetPasswordRuntimeException
1 parent aa54daa commit dd214c5

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

UPGRADING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,12 @@ trait has been dropped - attribute mapping is required.
5757
- protected function initialize(....)
5858
+ protected function initialize(....): void
5959
```
60+
61+
## ResetPasswordToken
62+
63+
- Method `getToken()` now throws a `ResetPasswordRuntimeException` instead of a
64+
`\RuntimeException` if the `clearToken()` method has been previously called.
65+
66+
- Method's `getExpirationMessageKey`, `getExpirationMessageData`, & `getExpiresAtIntervalInstance`
67+
no longer potentially throw a `LogicException`. They now throw a `ResetPasswordRuntimeException`
68+
if an invalid `$generatedAt` timestamp is provided to the class constructor.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SymfonyCasts ResetPasswordBundle package.
5+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyCasts\Bundle\ResetPassword\Exception;
11+
12+
/**
13+
* @author Jesse Rushlow <jr@rushlow.dev>
14+
*/
15+
final class ResetPasswordRuntimeException extends \RuntimeException implements ResetPasswordExceptionInterface
16+
{
17+
public function getReason(): string
18+
{
19+
return $this->getMessage();
20+
}
21+
}

src/Model/ResetPasswordToken.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Model;
1111

12+
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordRuntimeException;
13+
1214
/**
1315
* @author Jesse Rushlow <jr@rushlow.dev>
1416
* @author Ryan Weaver <ryan@symfonycasts.com>
@@ -52,11 +54,13 @@ public function __construct(string $token, \DateTimeInterface $expiresAt, ?int $
5254
* Internally, this consists of two parts - the selector and
5355
* the hashed token - but that's an implementation detail
5456
* of how the token will later be parsed.
57+
*
58+
* @throws ResetPasswordRuntimeException
5559
*/
5660
public function getToken(): string
5761
{
5862
if (null === $this->token) {
59-
throw new \RuntimeException('The token property is not set. Calling getToken() after calling clearToken() is not allowed.');
63+
throw new ResetPasswordRuntimeException('The token property is not set. Calling getToken() after calling clearToken() is not allowed.');
6064
}
6165

6266
return $this->token;
@@ -85,7 +89,7 @@ public function getExpiresAt(): \DateTimeInterface
8589
*
8690
* symfony/translation is required to translate into a non-English locale.
8791
*
88-
* @throws \LogicException
92+
* @throws ResetPasswordRuntimeException
8993
*/
9094
public function getExpirationMessageKey(): string
9195
{
@@ -118,7 +122,7 @@ public function getExpirationMessageKey(): string
118122
/**
119123
* @return array<string, int>
120124
*
121-
* @throws \LogicException
125+
* @throws ResetPasswordRuntimeException
122126
*/
123127
public function getExpirationMessageData(): array
124128
{
@@ -130,9 +134,7 @@ public function getExpirationMessageData(): array
130134
/**
131135
* Get the interval that the token is valid for.
132136
*
133-
* @throws \LogicException
134-
*
135-
* @psalm-suppress PossiblyFalseArgument
137+
* @throws ResetPasswordRuntimeException
136138
*/
137139
public function getExpiresAtIntervalInstance(): \DateInterval
138140
{
@@ -142,6 +144,10 @@ public function getExpiresAtIntervalInstance(): \DateInterval
142144

143145
$createdAtTime = \DateTimeImmutable::createFromFormat('U', (string) $this->generatedAt);
144146

147+
if (false === $createdAtTime) {
148+
throw new ResetPasswordRuntimeException(sprintf('Unable to create DateTimeInterface instance from "generatedAt": %s', $this->generatedAt));
149+
}
150+
145151
return $this->expiresAt->diff($createdAtTime);
146152
}
147153

0 commit comments

Comments
 (0)