UserRepository   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 83
ccs 0
cts 55
cp 0
rs 10
c 1
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByUsername() 0 3 1
A findOneWithPasswordResetToken() 0 13 1
A loadUserByIdentifier() 0 8 1
A findOneByEmail() 0 7 1
A findOneByUsernameAndNewEmailAddress() 0 15 1
A __construct() 0 8 2
A findExistingUserByNewEmail() 0 12 2
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
    public function __construct(ManagerRegistry $registry, string $entityClass, int $passwordRequestTimeout, int $newEmailConfirmTimeout)
35
    {
36
        if (!is_subclass_of($entityClass, AbstractUser::class)) {
37
            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
        parent::__construct($registry, $entityClass);
40
        $this->passwordRequestTimeout = $passwordRequestTimeout;
41
        $this->newEmailConfirmTimeout = $newEmailConfirmTimeout;
42
    }
43
44
    public function findOneByEmail(string $value): ?AbstractUser
45
    {
46
        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
            ->andWhere('LOWER(u.emailAddress) = :val')
48
            ->setParameter('val', strtolower($value))
49
            ->getQuery()
50
            ->getOneOrNullResult();
51
    }
52
53
    public function findOneWithPasswordResetToken(string $username): ?AbstractUser
54
    {
55
        $minimumRequestDateTime = new \DateTime();
56
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->passwordRequestTimeout));
57
58
        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
            ->andWhere('LOWER(u.username) = :username')
60
            ->andWhere('u.newPasswordConfirmationToken IS NOT NULL')
61
            ->andWhere('u.passwordRequestedAt > :minimumDateTime')
62
            ->setParameter('username', strtolower($username))
63
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
64
            ->getQuery()
65
            ->getOneOrNullResult();
66
    }
67
68
    public function findOneByUsernameAndNewEmailAddress(string $username, string $email): ?AbstractUser
69
    {
70
        $minimumRequestDateTime = new \DateTime();
71
        $minimumRequestDateTime->modify(sprintf('-%d seconds', $this->newEmailConfirmTimeout));
72
73
        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
            ->andWhere('LOWER(u.username) = :username')
75
            ->andWhere('LOWER(u.newEmailAddress) = :email')
76
            ->andWhere('u.newEmailConfirmationToken IS NOT NULL')
77
            ->andWhere('u.newEmailAddressChangeRequestedAt > :minimumDateTime')
78
            ->setParameter('username', strtolower($username))
79
            ->setParameter('email', strtolower($email))
80
            ->setParameter('minimumDateTime', $minimumRequestDateTime)
81
            ->getQuery()
82
            ->getOneOrNullResult();
83
    }
84
85
    public function loadUserByUsername(string $identifier): ?AbstractUser
86
    {
87
        return $this->loadUserByIdentifier($identifier);
88
    }
89
90
    public function loadUserByIdentifier(string $identifier): ?AbstractUser
91
    {
92
        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
            ->setParameter('username', strtolower($identifier))
96
            ->getQuery()
97
            ->getOneOrNullResult();
98
    }
99
100
    public function findExistingUserByNewEmail(AbstractUser $user): ?AbstractUser
101
    {
102
        $queryBuilder = $this->createQueryBuilder('u');
103
        $expr = $queryBuilder->expr();
104
        $newEmail = $user->getNewEmailAddress();
105
        $queryBuilder
106
            ->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