Skip to content

Commit daa2fcb

Browse files
committed
PostUserAccountResetPasswordResourceHandler optimizations
Signed-off-by: alexmerlin <[email protected]>
1 parent d97e264 commit daa2fcb

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

src/Core/src/User/src/Entity/User.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,6 @@ public function addResetPassword(UserResetPassword $resetPassword): void
117117
$this->resetPasswords->add($resetPassword);
118118
}
119119

120-
public function createResetPassword(): self
121-
{
122-
$this->resetPasswords->add(
123-
(new UserResetPassword())
124-
->setHash(self::generateHash())
125-
->setUser($this)
126-
);
127-
128-
return $this;
129-
}
130-
131120
public function getResetPasswords(): Collection
132121
{
133122
return $this->resetPasswords;

src/User/src/Handler/Account/ResetPassword/PostUserAccountResetPasswordResourceHandler.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace Api\User\Handler\Account\ResetPassword;
66

77
use Api\App\Exception\BadRequestException;
8-
use Api\App\Exception\ConflictException;
98
use Api\App\Exception\NotFoundException;
109
use Api\App\Handler\AbstractHandler;
1110
use Api\App\Template\RendererInterface;
1211
use Api\User\InputFilter\ResetPasswordInputFilter;
12+
use Api\User\Service\UserResetPasswordServiceInterface;
1313
use Api\User\Service\UserServiceInterface;
1414
use Core\App\Message;
1515
use Core\App\Service\MailService;
@@ -24,20 +24,21 @@ class PostUserAccountResetPasswordResourceHandler extends AbstractHandler
2424
#[Inject(
2525
MailService::class,
2626
UserServiceInterface::class,
27+
UserResetPasswordServiceInterface::class,
2728
ResetPasswordInputFilter::class,
2829
RendererInterface::class,
2930
)]
3031
public function __construct(
3132
protected MailService $mailService,
3233
protected UserServiceInterface $userService,
34+
protected UserResetPasswordServiceInterface $userResetPasswordService,
3335
protected ResetPasswordInputFilter $inputFilter,
3436
protected RendererInterface $renderer,
3537
) {
3638
}
3739

3840
/**
3941
* @throws BadRequestException
40-
* @throws ConflictException
4142
* @throws MailException
4243
* @throws NotFoundException
4344
*/
@@ -59,10 +60,13 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5960
throw NotFoundException::create(Message::USER_NOT_FOUND);
6061
}
6162

62-
$this->userService->saveUser([], $user->createResetPassword());
63+
$resetPassword = $this->userResetPasswordService->saveResetPassword($user);
6364
$this->mailService->sendResetPasswordRequestedMail(
6465
$user,
65-
$this->renderer->render('user::reset-password-requested', ['user' => $user])
66+
$this->renderer->render('user::reset-password-requested', [
67+
'user' => $user,
68+
'resetPassword' => $resetPassword,
69+
])
6670
);
6771

6872
return $this->infoResponse(Message::MAIL_SENT_RESET_PASSWORD, StatusCodeInterface::STATUS_CREATED);

src/User/src/Service/UserResetPasswordService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Api\App\Exception\NotFoundException;
88
use Core\App\Message;
9+
use Core\User\Entity\User;
910
use Core\User\Entity\UserResetPassword;
1011
use Core\User\Repository\UserResetPasswordRepository;
1112
use Dot\DependencyInjection\Attribute\Inject;
@@ -37,4 +38,16 @@ public function findOneBy(array $params): UserResetPassword
3738

3839
return $userResetPassword;
3940
}
41+
42+
public function saveResetPassword(User $user): UserResetPassword
43+
{
44+
$resetPassword = (new UserResetPassword())
45+
->setHash(User::generateHash())
46+
->setUser($user);
47+
$user->addResetPassword($resetPassword);
48+
49+
$this->userResetPasswordRepository->saveResource($resetPassword);
50+
51+
return $resetPassword;
52+
}
4053
}

src/User/src/Service/UserResetPasswordServiceInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Api\User\Service;
66

77
use Api\App\Exception\NotFoundException;
8+
use Core\User\Entity\User;
89
use Core\User\Entity\UserResetPassword;
910
use Core\User\Repository\UserResetPasswordRepository;
1011

@@ -16,4 +17,6 @@ public function getUserResetPasswordRepository(): UserResetPasswordRepository;
1617
* @throws NotFoundException
1718
*/
1819
public function findOneBy(array $params): UserResetPassword;
20+
21+
public function saveResetPassword(User $user): UserResetPassword;
1922
}

src/User/templates/user/reset-password-requested.phtml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ use Core\User\Entity\UserResetPassword;
99
/** @var ParserInterface $this */
1010
/** @var array $application */
1111
/** @var User $user */
12+
/** @var UserResetPassword $resetPassword */
1213

13-
$resetPasswords = $user->getResetPasswords()->toArray();
14-
usort($resetPasswords, fn(UserResetPassword $a, UserResetPassword $b) => $b->getCreated() <=> $a->getCreated());
15-
16-
$resetPassword = array_shift($resetPasswords);
1714
$resetUrl = $this->absoluteUrl(
1815
$this->url('user::update-account-reset-password', ['hash' => $resetPassword->getHash()])
1916
);

0 commit comments

Comments
 (0)