|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the BenGorUser package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace BenGorUser\User\Application\Command\ChangePassword; |
|
14
|
|
|
|
|
15
|
|
|
use BenGorUser\User\Domain\Model\Exception\UserTokenExpiredException; |
|
16
|
|
|
use BenGorUser\User\Domain\Model\Exception\UserTokenNotFoundException; |
|
17
|
|
|
use BenGorUser\User\Domain\Model\UserPassword; |
|
18
|
|
|
use BenGorUser\User\Domain\Model\UserPasswordEncoder; |
|
19
|
|
|
use BenGorUser\User\Domain\Model\UserRepository; |
|
20
|
|
|
use BenGorUser\User\Domain\Model\UserToken; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* By request remember password change user password command handler class. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Beñat Espiña <[email protected]> |
|
26
|
|
|
* @author Gorka Laucirica <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
class ByRequestRememberPasswordChangeUserPasswordHandler |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The user password encoder. |
|
32
|
|
|
* |
|
33
|
|
|
* @var UserPasswordEncoder |
|
34
|
|
|
*/ |
|
35
|
|
|
private $encoder; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The user repository. |
|
39
|
|
|
* |
|
40
|
|
|
* @var UserRepository |
|
41
|
|
|
*/ |
|
42
|
|
|
private $repository; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param UserRepository $aRepository The user repository |
|
48
|
|
|
* @param UserPasswordEncoder $anEncoder The password encoder |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(UserRepository $aRepository, UserPasswordEncoder $anEncoder) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->repository = $aRepository; |
|
53
|
|
|
$this->encoder = $anEncoder; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Handles the given command. |
|
58
|
|
|
* |
|
59
|
|
|
* @param ByRequestRememberPasswordChangeUserPasswordCommand $aCommand The command |
|
60
|
|
|
* |
|
61
|
|
|
* @throws UserTokenNotFoundException when the user does not exist |
|
62
|
|
|
* @throws UserTokenExpiredException when the token is expired |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __invoke(ByRequestRememberPasswordChangeUserPasswordCommand $aCommand) |
|
65
|
|
|
{ |
|
66
|
|
|
$user = $this->repository->userOfRememberPasswordToken(new UserToken($aCommand->rememberPasswordToken())); |
|
67
|
|
|
if (null === $user) { |
|
68
|
|
|
throw new UserTokenNotFoundException(); |
|
69
|
|
|
} |
|
70
|
|
|
if ($user->isRememberPasswordTokenExpired()) { |
|
71
|
|
|
throw new UserTokenExpiredException(); |
|
72
|
|
|
} |
|
73
|
|
|
$user->changePassword(UserPassword::fromPlain($aCommand->newPlainPassword(), $this->encoder)); |
|
74
|
|
|
|
|
75
|
|
|
$this->repository->persist($user); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.