ChangePasswordHandler::__invoke()   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 1
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\ChangePassword;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\LoggedInShopUserProviderInterface;
17
use Sylius\Component\User\Repository\UserRepositoryInterface;
18
use Sylius\Component\User\Security\UserPasswordEncoderInterface;
19
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
20
21
final class ChangePasswordHandler implements MessageHandlerInterface
22
{
23
    /** @var LoggedInShopUserProviderInterface */
24
    private $loggedInShopUserProvider;
25
26
    /** @var UserPasswordEncoderInterface */
27
    private $userPasswordEncoder;
28
29
    /** @var UserRepositoryInterface */
30
    private $shopUserRepository;
31
32
    public function __construct(
33
        LoggedInShopUserProviderInterface $loggedInShopUserProvider,
34
        UserPasswordEncoderInterface $userPasswordEncoder,
35
        UserRepositoryInterface $shopUserRepository
36
    ) {
37
        $this->loggedInShopUserProvider = $loggedInShopUserProvider;
38
        $this->userPasswordEncoder = $userPasswordEncoder;
39
        $this->shopUserRepository = $shopUserRepository;
40
    }
41
42
    public function __invoke(ChangePassword $changePassword): void
43
    {
44
        $shopUser = $this->loggedInShopUserProvider->provide();
45
        $shopUser->setPlainPassword($changePassword->newPassword());
46
47
        $encodedPassword = $this->userPasswordEncoder->encode($shopUser);
48
49
        $shopUser->setPassword($encodedPassword);
50
51
        $this->shopUserRepository->add($shopUser);
52
    }
53
}
54