Completed
Pull Request — master (#138)
by Łukasz
03:38
created

GenerateVerificationTokenHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 11 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\GenerateVerificationToken;
11
use Webmozart\Assert\Assert;
12
13
final class GenerateVerificationTokenHandler
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(GenerateVerificationToken $generateVerificationToken): void
32
    {
33
        $email = $generateVerificationToken->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->setEmailVerificationToken($this->tokenGenerator->generate());
41
    }
42
}
43