Completed
Push — master ( 577a52...db05bc )
by Valery
09:02
created

ResettingRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 35
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPassword() 0 7 1
A setToken() 0 6 1
A save() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\User;
8
use Doctrine\Persistence\ManagerRegistry;
9
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
10
11
final class ResettingRepository extends UserRepository
12
{
13
    /**
14
     * @var UserPasswordEncoderInterface
15
     */
16
    private $passwordEncoder;
17
18
    public function __construct(ManagerRegistry $registry, UserPasswordEncoderInterface $passwordEncoder)
19
    {
20
        parent::__construct($registry);
21
        $this->passwordEncoder = $passwordEncoder;
22
    }
23
24
    public function setPassword(User $user, string $plainPassword): void
25
    {
26
        $user->setPassword($this->passwordEncoder->encodePassword($user, $plainPassword));
27
        $user->setConfirmationToken(null);
28
        $user->setPasswordRequestedAt(null);
29
        $this->save($user);
30
    }
31
32
    public function setToken(User $user, string $token): void
33
    {
34
        $user->setConfirmationToken($token);
35
        $user->setPasswordRequestedAt(new \DateTime());
36
        $this->save($user);
37
    }
38
39
    private function save(User $user): void
40
    {
41
        $em = $this->getEntityManager();
42
        $em->persist($user);
43
        $em->flush();
44
    }
45
}
46