Passed
Pull Request — master (#72)
by Daniel
06:15
created

UserRepository::findOneWithPasswordResetToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Repository\User;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
19
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
20
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 *
25
 * @method AbstractUser|null find($id, $lockMode = null, $lockVersion = null)
26
 * @method AbstractUser|null findOneBy(array $criteria, array $orderBy = null)
27
 * @method AbstractUser[]    findAll()
28
 * @method AbstractUser[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
29
 */
30
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
31
{
32
    private int $passwordRequestTimeout;
33
    private int $newEmailConfirmTimeout;
34
35 8
    public function __construct(ManagerRegistry $registry, string $entityClass, int $passwordRequestTimeout, int $newEmailConfirmTimeout)
36
    {
37 8
        if (!is_subclass_of($entityClass, AbstractUser::class)) {
38 1
            throw new InvalidArgumentException(sprintf('The entity class `%s` used for the repository `%s` must be a subclass of `%s`', $entityClass, __CLASS__, AbstractUser::class));
39
        }
40 8
        parent::__construct($registry, $entityClass);
41 8
        $this->passwordRequestTimeout = $passwordRequestTimeout;
42 8
        $this->newEmailConfirmTimeout = $newEmailConfirmTimeout;
43 8
    }
44
45 1
    public function findOneByEmail($value): ?AbstractUser
46
    {
47 1
        return $this->createQueryBuilder('u')
48 1
            ->andWhere('u.emailAddress = :val')
49 1
            ->setParameter('val', $value)
50 1
            ->getQuery()
51 1
            ->getOneOrNullResult();
52
    }
53
54 2
    public function findOneWithPasswordResetToken(string $username): ?AbstractUser
55
    {
56 2
        $minimumRequestDateTime = new \DateTime();
57 2
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout));
58
59 2
        return $this->createQueryBuilder('u')
60 2
            ->andWhere('u.username = :username')
61 2
            ->andWhere('u.newPasswordConfirmationToken IS NOT NULL')
62 2
            ->andWhere('u.passwordRequestedAt > :minimumDateTime')
63 2
            ->setParameter('username', $username)
64 2
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
65 2
            ->getQuery()
66 2
            ->getOneOrNullResult();
67
    }
68
69 2
    public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser
70
    {
71 2
        $minimumRequestDateTime = new \DateTime();
72 2
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->newEmailConfirmTimeout));
73
74 2
        return $this->createQueryBuilder('u')
75 2
            ->andWhere('u.username = :username')
76 2
            ->andWhere('u.newEmailAddress = :email')
77 2
            ->andWhere('u.newEmailConfirmationToken IS NOT NULL')
78 2
            ->andWhere('u.newEmailAddressChangeRequestedAt > :minimumDateTime')
79 2
            ->setParameter('username', $username)
80 2
            ->setParameter('email', $email)
81 2
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
82 2
            ->getQuery()
83 2
            ->getOneOrNullResult();
84
    }
85
86 1
    public function loadUserByUsername($usernameOrEmail): ?AbstractUser
87
    {
88 1
        return $this->createQueryBuilder('u')
89 1
            ->andWhere('u.username = :username')
90 1
            ->orWhere('u.emailAddress = :username')
91 1
            ->setParameter('username', $usernameOrEmail)
92 1
            ->getQuery()
93 1
            ->getOneOrNullResult();
94
    }
95
96 1
    public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser
97
    {
98 1
        $queryBuilder = $this->createQueryBuilder('u');
99 1
        $expr = $queryBuilder->expr();
100
        $queryBuilder
101 1
            ->andWhere($expr->eq('u.emailAddress', ':email_address'))
102 1
            ->andWhere($expr->neq('u', ':user'))
103 1
            ->setParameter('email_address', $user->getNewEmailAddress())
104 1
            ->setParameter('user', $user, $this->getClassMetadata()->getTypeOfField('id'));
105
106 1
        return $queryBuilder->getQuery()->getOneOrNullResult();
107
    }
108
}
109