Test Failed
Pull Request — master (#72)
by Daniel
06:07
created

UserRepository::findOneByPasswordResetToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 15
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
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
34 7
    public function __construct(ManagerRegistry $registry, int $passwordRequestTimeout, string $entityClass)
35
    {
36 7
        if (!is_subclass_of($entityClass, AbstractUser::class)) {
37 1
            throw new InvalidArgumentException(sprintf('The entity class `%s` used for the repository `%s` must be a subclass of `%s`', $entityClass, __CLASS__, AbstractUser::class));
38
        }
39 7
        parent::__construct($registry, $entityClass);
40 7
        $this->passwordRequestTimeout = $passwordRequestTimeout;
41 7
    }
42
43 1
    public function findOneByEmail($value): ?AbstractUser
44
    {
45 1
        return $this->createQueryBuilder('u')
46 1
            ->andWhere('u.emailAddress = :val')
47 1
            ->setParameter('val', $value)
48 1
            ->getQuery()
49 1
            ->getOneOrNullResult();
50
    }
51
52 2
    public function findOneWithPasswordResetToken(string $username): ?AbstractUser
53
    {
54 2
        $minimumRequestDateTime = new \DateTime();
55 2
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout));
56
57 2
        return $this->createQueryBuilder('u')
58 2
            ->andWhere('u.username = :username')
59 2
            ->andWhere('u.newPasswordConfirmationToken IS NOT NULL')
60 2
            ->andWhere('u.passwordRequestedAt > :passwordRequestedAt')
61 2
            ->setParameter('username', $username)
62 2
            ->setParameter('passwordRequestedAt', $minimumRequestDateTime)
63 2
            ->getQuery()
64 2
            ->getOneOrNullResult();
65 2
    }
66 2
67
    public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser
68
    {
69 1
        return $this->createQueryBuilder('u')
70
            ->andWhere('u.username = :username')
71 1
            ->andWhere('u.newEmailAddress = :email')
72 1
            ->andWhere('u.newEmailConfirmationToken IS NOT NULL')
73 1
            ->setParameter('username', $username)
74 1
            ->setParameter('email', $email)
75 1
            ->getQuery()
76 1
            ->getOneOrNullResult();
77 1
    }
78 1
79 1
    public function loadUserByUsername($usernameOrEmail): ?AbstractUser
80 1
    {
81
        return $this->createQueryBuilder('u')
82
            ->andWhere('u.username = :username')
83 1
            ->orWhere('u.emailAddress = :username')
84
            ->setParameter('username', $usernameOrEmail)
85 1
            ->getQuery()
86 1
            ->getOneOrNullResult();
87 1
    }
88 1
89 1
    public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser
90 1
    {
91
        $queryBuilder = $this->createQueryBuilder('u');
92
        $expr = $queryBuilder->expr();
93 1
        $queryBuilder
94
            ->andWhere($expr->eq('u.emailAddress', ':email_address'))
95 1
            ->andWhere($expr->neq('u', ':user'))
96 1
            ->setParameter('email_address', $user->getNewEmailAddress())
97
            ->setParameter('user', $user, $this->getClassMetadata()->getTypeOfField('id'));
98 1
99 1
        return $queryBuilder->getQuery()->getOneOrNullResult();
100 1
    }
101
}
102