CoachRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 41
ccs 0
cts 26
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A upgradePassword() 0 9 2
A loadUserByUsername() 0 8 1
A findOneForPasswordReset() 0 10 1
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