ResetPasswordHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\CommandHandler\User;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\User\ResetPassword;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Mailer\Emails;
17
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
18
use Sylius\Component\Core\Model\ShopUserInterface;
19
use Sylius\Component\Mailer\Sender\SenderInterface;
20
use Sylius\Component\User\Repository\UserRepositoryInterface;
21
use Sylius\Component\User\Security\Generator\GeneratorInterface;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Webmozart\Assert\Assert;
24
25
final class ResetPasswordHandler implements MessageHandlerInterface
26
{
27
    /** @var UserRepositoryInterface */
28
    private $userRepository;
29
30
    /** @var GeneratorInterface */
31
    private $tokenGenerator;
32
33
    /** @var SenderInterface */
34
    private $sender;
35
36
    /** @var ChannelProviderInterface */
37
    private $channelProvider;
38
39
    public function __construct(
40
        UserRepositoryInterface $userRepository,
41
        GeneratorInterface $tokenGenerator,
42
        SenderInterface $sender,
43
        ChannelProviderInterface $channelProvider
44
    ) {
45
        $this->userRepository = $userRepository;
46
        $this->tokenGenerator = $tokenGenerator;
47
        $this->sender = $sender;
48
        $this->channelProvider = $channelProvider;
49
    }
50
51
    public function __invoke(ResetPassword $resetPassword): void
52
    {
53
        $email = $resetPassword->email();
54
55
        /** @var ShopUserInterface $user */
56
        $user = $this->userRepository->findOneByEmail($email);
57
58
        Assert::notNull($user, sprintf('User with %s email has not been found.', $email));
59
60
        $user->setPasswordResetToken($this->tokenGenerator->generate());
61
        $user->setPasswordRequestedAt(new \DateTime());
62
63
        Assert::notNull($user->getPasswordResetToken(), sprintf('User with %s email has not verification token defined.', $email));
64
65
        $this->sender->send(
66
            Emails::RESET_PASSWORD_TOKEN,
67
            [$email],
68
            ['user' => $user, 'channel' => $this->channelProvider->provide()]
69
        );
70
    }
71
}
72