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\UserDoesNotExistException; |
16
|
|
|
use BenGorUser\User\Domain\Model\UserEmail; |
17
|
|
|
use BenGorUser\User\Domain\Model\UserPassword; |
18
|
|
|
use BenGorUser\User\Domain\Model\UserPasswordEncoder; |
19
|
|
|
use BenGorUser\User\Domain\Model\UserRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Without old password change user password command handler class. |
23
|
|
|
* |
24
|
|
|
* @author Beñat Espiña <[email protected]> |
25
|
|
|
* @author Gorka Laucirica <[email protected]> |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
class WithoutOldPasswordChangeUserPasswordHandler |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The user password encoder. |
31
|
|
|
* |
32
|
|
|
* @var UserPasswordEncoder |
33
|
|
|
*/ |
34
|
|
|
private $encoder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The user repository. |
38
|
|
|
* |
39
|
|
|
* @var UserRepository |
40
|
|
|
*/ |
41
|
|
|
private $repository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param UserRepository $aRepository The user repository |
47
|
|
|
* @param UserPasswordEncoder $anEncoder The password encoder |
48
|
|
|
*/ |
49
|
|
|
public function __construct(UserRepository $aRepository, UserPasswordEncoder $anEncoder) |
50
|
|
|
{ |
51
|
|
|
$this->repository = $aRepository; |
52
|
|
|
$this->encoder = $anEncoder; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Handles the given command. |
57
|
|
|
* |
58
|
|
|
* @param WithoutOldPasswordChangeUserPasswordCommand $aCommand The command |
59
|
|
|
* |
60
|
|
|
* @throws UserDoesNotExistException when the user does not exist |
61
|
|
|
*/ |
62
|
|
|
public function __invoke(WithoutOldPasswordChangeUserPasswordCommand $aCommand) |
63
|
|
|
{ |
64
|
|
|
$user = $this->repository->userOfEmail(new UserEmail($aCommand->email())); |
65
|
|
|
if (null === $user) { |
66
|
|
|
throw new UserDoesNotExistException(); |
67
|
|
|
} |
68
|
|
|
$user->changePassword(UserPassword::fromPlain($aCommand->newPlainPassword(), $this->encoder)); |
69
|
|
|
|
70
|
|
|
$this->repository->persist($user); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.