Skip to content

Commit b15e3a1

Browse files
authored
feature #309 [generator] throws ResetPasswordRuntimeException when unable to create json
1 parent daebae8 commit b15e3a1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

UPGRADING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ instantiate a token object with a `null` `$token` value. This is intentional._
9999
- public function createToken(\DateTimeInterface $expiresAt, $userId, ?string $verifier = null): ResetPasswordTokenComponents
100100
+ public function createToken(\DateTimeInterface $expiresAt, int|string $userId, ?string $verifier = null): ResetPasswordTokenComponents
101101
```
102+
103+
- `createToken()` now throws a `ResetPasswordRuntimeException` when unable to convert
104+
the `$verified`, `$userId`, and `$expiresAt` values to json.

src/Generator/ResetPasswordTokenGenerator.php

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

1010
namespace SymfonyCasts\Bundle\ResetPassword\Generator;
1111

12+
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordRuntimeException;
1213
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordTokenComponents;
1314

1415
/**
@@ -35,6 +36,8 @@ public function __construct(
3536
*
3637
* @param int|string $userId Unique user identifier
3738
* @param ?string $verifier Only required for token comparison
39+
*
40+
* @throws ResetPasswordRuntimeException
3841
*/
3942
public function createToken(\DateTimeInterface $expiresAt, int|string $userId, ?string $verifier = null): ResetPasswordTokenComponents
4043
{
@@ -44,7 +47,11 @@ public function createToken(\DateTimeInterface $expiresAt, int|string $userId, ?
4447

4548
$selector = $this->generator->getRandomAlphaNumStr();
4649

47-
$encodedData = json_encode([$verifier, $userId, $expiresAt->getTimestamp()]);
50+
try {
51+
$encodedData = json_encode(value: [$verifier, $userId, $expiresAt->getTimestamp()], flags: \JSON_THROW_ON_ERROR);
52+
} catch (\JsonException $exception) {
53+
throw new ResetPasswordRuntimeException(message: 'Unable to create token. Invalid JSON.', previous: $exception);
54+
}
4855

4956
return new ResetPasswordTokenComponents(
5057
$selector,

0 commit comments

Comments
 (0)