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

GenerateResetPasswordTokenHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A handle() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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