Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
UserRepository
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 remove
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 upgradePassword
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Repository;
4
5use App\Entity\User;
6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7use Doctrine\Persistence\ManagerRegistry;
8use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
9use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
10use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
11
12/**
13 * @extends ServiceEntityRepository<User>
14 *
15 * @method User|null find($id, $lockMode = null, $lockVersion = null)
16 * @method User|null findOneBy(array $criteria, array $orderBy = null)
17 * @method User[]    findAll()
18 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
19 */
20class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
21{
22    public function __construct(ManagerRegistry $registry)
23    {
24        parent::__construct($registry, User::class);
25    }
26
27    public function save(User $entity, bool $flush = false): void
28    {
29        $this->getEntityManager()->persist($entity);
30
31        if ($flush) {
32            $this->getEntityManager()->flush();
33        }
34    }
35
36    public function remove(User $entity, bool $flush = false): void
37    {
38        $this->getEntityManager()->remove($entity);
39
40        if ($flush) {
41            $this->getEntityManager()->flush();
42        }
43    }
44
45    /**
46     * Used to upgrade (rehash) the user's password automatically over time.
47     */
48    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
49    {
50        if (!$user instanceof User) {
51            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
52        }
53
54        $user->setPassword($newHashedPassword);
55
56        $this->save($user, true);
57    }
58}