Completed
Push — master ( 82aaff...06b207 )
by Kamil
14s
created

GenerateResetPasswordTokenHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sylius\ShopApiPlugin\Handler;
6
7
use Sylius\Component\Core\Model\ShopUserInterface;
8
use Sylius\Component\User\Repository\UserRepositoryInterface;
9
use Sylius\Component\User\Security\Generator\GeneratorInterface;
10
use Sylius\ShopApiPlugin\Command\GenerateResetPasswordToken;
11
use Webmozart\Assert\Assert;
12
13 View Code Duplication
final class GenerateResetPasswordTokenHandler
14
{
15
    /**
16
     * @var UserRepositoryInterface
17
     */
18
    private $userRepository;
19
20
    /**
21
     * @var GeneratorInterface
22
     */
23
    private $tokenGenerator;
24
25
    public function __construct(UserRepositoryInterface $userRepository, GeneratorInterface $tokenGenerator)
26
    {
27
        $this->userRepository = $userRepository;
28
        $this->tokenGenerator = $tokenGenerator;
29
    }
30
31
    public function handle(GenerateResetPasswordToken $generateResetPasswordToken)
32
    {
33
        $email = $generateResetPasswordToken->email();
34
35
        /** @var ShopUserInterface $user */
36
        $user = $this->userRepository->findOneByEmail($email);
37
38
        Assert::notNull($user, sprintf('User with %s email has not been found.', $email));
39
40
        $user->setPasswordResetToken($this->tokenGenerator->generate());
41
        $user->setPasswordRequestedAt(new \DateTime());
42
    }
43
}
44