CoachRepository::loadUserByUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Repository;
4
5
use Obblm\Core\Entity\Coach;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\Persistence\ManagerRegistry;
8
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
use function get_class;
13
14
/**
15
 * @method Coach|null find($id, $lockMode = null, $lockVersion = null)
16
 * @method Coach|null findOneBy(array $criteria, array $orderBy = null)
17
 * @method Coach[]    findAll()
18
 * @method Coach[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
19
 */
20
class CoachRepository extends ServiceEntityRepository implements UserLoaderInterface, PasswordUpgraderInterface
21
{
22
    public function __construct(ManagerRegistry $registry)
23
    {
24
        parent::__construct($registry, Coach::class);
25
    }
26
27
    /**
28
     * Used to upgrade (rehash) the user's password automatically over time.
29
     */
30
    public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
31
    {
32
        if (!$user instanceof Coach) {
33
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
34
        }
35
36
        $user->setPassword($newEncodedPassword);
37
        $this->_em->persist($user);
38
        $this->_em->flush();
39
    }
40
41
    public function loadUserByUsername($usernameOrEmail)
42
    {
43
        return $this->createQueryBuilder('c')
44
            ->where('c.username = :username_or_email')
45
            ->orWhere('c.email = :username_or_email')
46
            ->getQuery()
47
            ->setParameter(':username_or_email', $usernameOrEmail)
48
            ->getOneOrNullResult();
49
    }
50
51
    public function findOneForPasswordReset($hash)
52
    {
53
        $limitDateTime = new \DateTime('- 1 day');
54
        return $this->createQueryBuilder('c')
55
            ->where('c.resetPasswordHash = :hash')
56
            ->andWhere('c.resetPasswordAt > :limit_datetime')
57
            ->getQuery()
58
            ->setParameter(':hash', $hash)
59
            ->setParameter(':limit_datetime', $limitDateTime)
60
            ->getOneOrNullResult();
61
    }
62
}
63