1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Users\Repository; |
5
|
|
|
|
6
|
|
|
use App\Users\Entity\User; |
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
8
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
9
|
|
|
use Doctrine\ORM\NoResultException; |
10
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
11
|
|
|
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; |
12
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
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
|
45 |
|
public function __construct(RegistryInterface $registry) |
23
|
|
|
{ |
24
|
45 |
|
parent::__construct($registry, User::class); |
25
|
45 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $username |
29
|
|
|
* @return User|null |
30
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
31
|
|
|
*/ |
32
|
7 |
|
public function loadUserByUsername($username): ?User |
33
|
|
|
{ |
34
|
7 |
|
$username = mb_strtolower($username); |
35
|
7 |
|
return $this->createQueryBuilder('u') |
36
|
7 |
|
->where('LOWER(u.username) = :username OR LOWER(u.email) = :email') |
37
|
7 |
|
->setParameter('username', $username) |
38
|
7 |
|
->setParameter('email', $username) |
39
|
7 |
|
->getQuery() |
40
|
7 |
|
->getOneOrNullResult(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $criteria |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
3 |
|
public function getUsersByCriteria(array $criteria) |
48
|
|
|
{ |
49
|
3 |
|
$field = key($criteria); |
50
|
3 |
|
$value = mb_strtolower(reset($criteria)); |
51
|
|
|
|
52
|
3 |
|
return $this->createQueryBuilder('u') |
53
|
3 |
|
->where("LOWER(u.{$field}) = :value") |
54
|
3 |
|
->setParameter('value', $value) |
55
|
3 |
|
->getQuery() |
56
|
3 |
|
->getResult(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* I do not understand why getSingleScalarResult throws NoResultException but it does |
61
|
|
|
* |
62
|
|
|
* @param array $criteria |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
4 |
|
public function isUserExists(array $criteria): bool |
66
|
|
|
{ |
67
|
4 |
|
$field = key($criteria); |
68
|
4 |
|
$value = mb_strtolower($criteria[$field]); |
69
|
|
|
|
70
|
|
|
try { |
71
|
4 |
|
$user = $this->createQueryBuilder('u') |
72
|
4 |
|
->where("LOWER(u.{$field}) = :value") |
73
|
4 |
|
->setParameter('value', $value) |
74
|
4 |
|
->getQuery() |
75
|
4 |
|
->getSingleScalarResult(); |
76
|
4 |
|
} catch (NonUniqueResultException $nonUniqueResultException) { |
77
|
2 |
|
return true; |
78
|
2 |
|
} catch (NoResultException $noResultException) { |
79
|
2 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $user !== null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|