__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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