Completed
Pull Request — master (#12)
by
unknown
13:28
created

PasswordResetHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Command;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordConfirmationException;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
7
8
class PasswordResetHandler
9
{
10
    /**
11
     * @var UserRepositoryCollection
12
     */
13
    private $userRepositoryCollection;
14
15
    public function __construct(UserRepositoryCollection $userRepositoryCollection)
16
    {
17
        $this->userRepositoryCollection = $userRepositoryCollection;
18
    }
19
20
    /**
21
     * @param PasswordReset $command
22
     * 
23
     * @throws InvalidPasswordConfirmationException
24
     */
25
    public function handle(PasswordReset $command)
26
    {
27
        if ($command->passwordConfirmationIsValid()) {
28
            $user = $command->getUser();
29
            $updatedUser = clone $user;
30
            $updatedUser->setPassword($command->getPassword());
31
            $updatedUser->clearPasswordResetToken();
32
            $repository = $this->userRepositoryCollection->findRepositoryByClassName(get_class($user));
33
            $repository->update($user, $updatedUser);
34
35
            return;
36
        }
37
38
        throw new InvalidPasswordConfirmationException('The password confirmation isn\'t valid');
39
    }
40
}
41