Test Failed
Push — master ( 61801d...f3c992 )
by Daniel
11:05
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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
21
/**
22
 * @author Daniel West <[email protected]>
23
 *
24
 * @method AbstractUser|null find($id, $lockMode = null, $lockVersion = null)
25
 * @method AbstractUser|null findOneBy(array $criteria, array $orderBy = null)
26
 * @method AbstractUser[]    findAll()
27
 * @method AbstractUser[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
28
 */
29
class UserRepository extends ServiceEntityRepository implements UserRepositoryInterface
30
{
31
    private int $passwordRequestTimeout;
32
    private int $newEmailConfirmTimeout;
33
34 15
    public function __construct(ManagerRegistry $registry, string $entityClass, int $passwordRequestTimeout, int $newEmailConfirmTimeout)
35
    {
36 15
        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 15
        parent::__construct($registry, $entityClass);
40 15
        $this->passwordRequestTimeout = $passwordRequestTimeout;
41 15
        $this->newEmailConfirmTimeout = $newEmailConfirmTimeout;
42 15
    }
43
44 1
    public function findOneByEmail(string $value): ?AbstractUser
45
    {
46 1
        return $this->createQueryBuilder('u')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Silverback\ApiComponents...\User\AbstractUser|null. Consider adding an additional type-check to rule them out.
Loading history...
47 1
            ->andWhere('LOWER(u.emailAddress) = :val')
48 1
            ->setParameter('val', strtolower($value))
49 1
            ->getQuery()
50 1
            ->getOneOrNullResult();
51
    }
52
53 2
    public function findOneWithPasswordResetToken(string $username): ?AbstractUser
54
    {
55 2
        $minimumRequestDateTime = new \DateTime();
56 2
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout));
57
58 2
        return $this->createQueryBuilder('u')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Silverback\ApiComponents...\User\AbstractUser|null. Consider adding an additional type-check to rule them out.
Loading history...
59 2
            ->andWhere('LOWER(u.username) = :username')
60 2
            ->andWhere('u.newPasswordConfirmationToken IS NOT NULL')
61 2
            ->andWhere('u.passwordRequestedAt > :minimumDateTime')
62 2
            ->setParameter('username', strtolower($username))
63 2
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
64 2
            ->getQuery()
65 2
            ->getOneOrNullResult();
66
    }
67
68 2
    public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser
69
    {
70 2
        $minimumRequestDateTime = new \DateTime();
71 2
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->newEmailConfirmTimeout));
72
73 2
        return $this->createQueryBuilder('u')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Silverback\ApiComponents...\User\AbstractUser|null. Consider adding an additional type-check to rule them out.
Loading history...
74 2
            ->andWhere('LOWER(u.username) = :username')
75 2
            ->andWhere('LOWER(u.newEmailAddress) = :email')
76 2
            ->andWhere('u.newEmailConfirmationToken IS NOT NULL')
77 2
            ->andWhere('u.newEmailAddressChangeRequestedAt > :minimumDateTime')
78 2
            ->setParameter('username', strtolower($username))
79 2
            ->setParameter('email', strtolower($email))
80 2
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
81 2
            ->getQuery()
82 2
            ->getOneOrNullResult();
83
    }
84
85 2
    public function loadUserByUsername(string $identifier): ?AbstractUser
86
    {
87 2
        return $this->loadUserByIdentifier($identifier);
88 2
    }
89 2
90 2
    public function loadUserByIdentifier(string $identifier): ?AbstractUser
91 2
    {
92 2
        return $this->createQueryBuilder('u')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createQuer...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Silverback\ApiComponents...\User\AbstractUser|null. Consider adding an additional type-check to rule them out.
Loading history...
93
            ->andWhere('LOWER(u.username) = :username')
94
            ->orWhere('LOWER(u.emailAddress) = :username')
95 1
            ->setParameter('username', strtolower($identifier))
96
            ->getQuery()
97 1
            ->getOneOrNullResult();
98 1
    }
99 1
100
    public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser
101 1
    {
102 1
        $queryBuilder = $this->createQueryBuilder('u');
103 1
        $expr = $queryBuilder->expr();
104 1
        $newEmail = $user->getNewEmailAddress();
105
        $queryBuilder
106 1
            ->andWhere($expr->eq('LOWER(u.emailAddress)', ':email_address'))
107
            ->andWhere($expr->neq('u', ':user'))
108
            ->setParameter('email_address', $newEmail ? strtolower($newEmail) : null)
109
            ->setParameter('user', $user, $this->getClassMetadata()->getTypeOfField('id'));
110
111
        return $queryBuilder->getQuery()->getOneOrNullResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryBuilder->ge...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Silverback\ApiComponents...\User\AbstractUser|null. Consider adding an additional type-check to rule them out.
Loading history...
112
    }
113
}
114