|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Users\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use App\Users\Entity\User; |
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
9
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
|
10
|
|
|
use Doctrine\ORM\NoResultException; |
|
11
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
12
|
|
|
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @method User|null find($id, $lockMode = null, $lockVersion = null) |
|
16
|
|
|
* @method User|null findOneBy(array $criteria, array $orderBy = null) |
|
17
|
|
|
* @method User[] findAll() |
|
18
|
|
|
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
19
|
|
|
*/ |
|
20
|
|
|
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface |
|
21
|
|
|
{ |
|
22
|
74 |
|
public function __construct(RegistryInterface $registry) |
|
23
|
|
|
{ |
|
24
|
74 |
|
parent::__construct($registry, User::class); |
|
25
|
74 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $username |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
|
31
|
|
|
* |
|
32
|
|
|
* @return User|null |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function loadUserByUsername($username): ?User |
|
35
|
|
|
{ |
|
36
|
8 |
|
$username = mb_strtolower($username); |
|
37
|
|
|
|
|
38
|
8 |
|
return $this->createQueryBuilder('u') |
|
39
|
8 |
|
->where('LOWER(u.username) = :username OR LOWER(u.email) = :email') |
|
40
|
8 |
|
->setParameter('username', $username) |
|
41
|
8 |
|
->setParameter('email', $username) |
|
42
|
8 |
|
->getQuery() |
|
43
|
8 |
|
->getOneOrNullResult(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $email |
|
48
|
|
|
* @return User|null |
|
49
|
|
|
* @throws NonUniqueResultException |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function loadUserByEmail(string $email): ?User |
|
52
|
|
|
{ |
|
53
|
3 |
|
return $this->createQueryBuilder('u') |
|
54
|
3 |
|
->where('u.email = :email') |
|
55
|
3 |
|
->setParameter('email', $email) |
|
56
|
3 |
|
->getQuery() |
|
57
|
3 |
|
->getOneOrNullResult(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $criteria |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function getUsersByCriteria(array $criteria) |
|
66
|
|
|
{ |
|
67
|
3 |
|
$field = key($criteria); |
|
68
|
3 |
|
$value = mb_strtolower(reset($criteria)); |
|
69
|
|
|
|
|
70
|
3 |
|
return $this->createQueryBuilder('u') |
|
71
|
3 |
|
->where("LOWER(u.{$field}) = :value") |
|
72
|
3 |
|
->setParameter('value', $value) |
|
73
|
3 |
|
->getQuery() |
|
74
|
3 |
|
->getResult(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* I do not understand why getSingleScalarResult throws NoResultException but it does. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $criteria |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function isUserExists(array $criteria): bool |
|
85
|
|
|
{ |
|
86
|
4 |
|
$field = key($criteria); |
|
87
|
4 |
|
$value = mb_strtolower($criteria[$field]); |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
4 |
|
$user = $this->createQueryBuilder('u') |
|
91
|
4 |
|
->where("LOWER(u.{$field}) = :value") |
|
92
|
4 |
|
->setParameter('value', $value) |
|
93
|
4 |
|
->getQuery() |
|
94
|
4 |
|
->getSingleScalarResult(); |
|
95
|
4 |
|
} catch (NonUniqueResultException $nonUniqueResultException) { |
|
96
|
2 |
|
return true; |
|
97
|
2 |
|
} catch (NoResultException $noResultException) { |
|
98
|
2 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $user !== null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|