Passed
Push — v2 ( 5505d3...8372d3 )
by Daniel
04:27 queued 22s
created

UserRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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 Silverback\ApiComponentBundle\Entity\User\User;
18
use Symfony\Bridge\Doctrine\RegistryInterface;
19
20
/**
21
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
22
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
23
 * @method User[]    findAll()
24
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
25
 */
26
class UserRepository extends ServiceEntityRepository
27
{
28
    private $passwordRequestTimeout;
29
30
    public function __construct(RegistryInterface $registry, int $passwordRequestTimeout, string $entityClass)
31
    {
32
        parent::__construct($registry, $entityClass);
33
        $this->passwordRequestTimeout = $passwordRequestTimeout;
34
    }
35
36
    public function findOneByEmail($value): ?User
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