Passed
Push — v2 ( 7ee84c...a408c1 )
by Daniel
04:52
created

UserRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 26
c 1
b 0
f 0
dl 0
loc 43
ccs 0
cts 29
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findOneByPasswordResetToken() 0 14 1
A findOneByEmail() 0 7 1
A loadUserByUsername() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Repository\User;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
19
20
/**
21
 * @method AbstractUser|null find($id, $lockMode = null, $lockVersion = null)
22
 * @method AbstractUser|null findOneBy(array $criteria, array $orderBy = null)
23
 * @method AbstractUser[]    findAll()
24
 * @method AbstractUser[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
25
 */
26
class UserRepository extends ServiceEntityRepository
27
{
28
    private int $passwordRequestTimeout;
29
30
    public function __construct(ManagerRegistry $registry, int $passwordRequestTimeout, string $entityClass)
31
    {
32
        parent::__construct($registry, $entityClass);
33
        $this->passwordRequestTimeout = $passwordRequestTimeout;
34
    }
35
36
    public function findOneByEmail($value): ?AbstractUser
37
    {
38
        return $this->createQueryBuilder('u')
39
            ->andWhere('u.email = :val')
40
            ->setParameter('val', $value)
41
            ->getQuery()
42
            ->getOneOrNullResult();
43
    }
44
45
    public function findOneByPasswordResetToken(string $username, string $token)
46
    {
47
        $minimumRequestDateTime = new \DateTime();
48
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout));
49
50
        return $this->createQueryBuilder('u')
51
            ->andWhere('u.username = :username')
52
            ->andWhere('u.passwordResetConfirmationToken = :token')
53
            ->andWhere('u.passwordRequestedAt > :passwordRequestedAt')
54
            ->setParameter('username', $username)
55
            ->setParameter('token', $token)
56
            ->setParameter('passwordRequestedAt', $minimumRequestDateTime)
57
            ->getQuery()
58
            ->getOneOrNullResult();
59
    }
60
61
    public function loadUserByUsername($usernameOrEmail)
62
    {
63
        return $this->createQueryBuilder('u')
64
            ->andWhere('u.username = :username')
65
            ->orWhere('u.emailAddress = :username')
66
            ->setParameter('username', $usernameOrEmail)
67
            ->getQuery()
68
            ->getOneOrNullResult();
69
    }
70
}
71