ChangeUserPasswordHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 13 3
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\Exception\UserPasswordInvalidException;
17
use BenGorUser\User\Domain\Model\UserId;
18
use BenGorUser\User\Domain\Model\UserPassword;
19
use BenGorUser\User\Domain\Model\UserPasswordEncoder;
20
use BenGorUser\User\Domain\Model\UserRepository;
21
22
/**
23
 * Change user password command handler class.
24
 *
25
 * @author Beñat Espiña <[email protected]>
26
 * @author Gorka Laucirica <[email protected]>
27
 */
28
class ChangeUserPasswordHandler
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 ChangeUserPasswordCommand $aCommand The command
60
     *
61
     * @throws UserDoesNotExistException    when the user does not exist
62
     * @throws UserPasswordInvalidException when the user password is invalid
63
     */
64
    public function __invoke(ChangeUserPasswordCommand $aCommand)
65
    {
66
        $user = $this->repository->userOfId(new UserId($aCommand->id()));
67
        if (null === $user) {
68
            throw new UserDoesNotExistException();
69
        }
70
        if (false === $user->password()->equals($aCommand->oldPlainPassword(), $this->encoder)) {
71
            throw new UserPasswordInvalidException();
72
        }
73
        $user->changePassword(UserPassword::fromPlain($aCommand->newPlainPassword(), $this->encoder));
74
75
        $this->repository->persist($user);
76
    }
77
}
78