|
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 Symfony\Bridge\Doctrine\RegistryInterface; |
|
9
|
|
|
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @method User|null find($id, $lockMode = null, $lockVersion = null) |
|
14
|
|
|
* @method User|null findOneBy(array $criteria, array $orderBy = null) |
|
15
|
|
|
* @method User[] findAll() |
|
16
|
|
|
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface |
|
19
|
|
|
{ |
|
20
|
33 |
|
public function __construct(RegistryInterface $registry) |
|
21
|
|
|
{ |
|
22
|
33 |
|
parent::__construct($registry, User::class); |
|
23
|
33 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $username |
|
27
|
|
|
* @return User|null |
|
28
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
|
29
|
|
|
*/ |
|
30
|
7 |
|
public function loadUserByUsername($username): ?User |
|
31
|
|
|
{ |
|
32
|
7 |
|
$username = mb_strtolower($username); |
|
33
|
7 |
|
return $this->createQueryBuilder('u') |
|
34
|
7 |
|
->where('LOWER(u.username) = :username OR LOWER(u.email) = :email') |
|
35
|
7 |
|
->setParameter('username', $username) |
|
36
|
7 |
|
->setParameter('email', $username) |
|
37
|
7 |
|
->getQuery() |
|
38
|
7 |
|
->getOneOrNullResult(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $criteria |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function isUserExists(array $criteria) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$field = key($criteria); |
|
48
|
2 |
|
$value = mb_strtolower(reset($criteria)); |
|
49
|
|
|
|
|
50
|
2 |
|
return $this->createQueryBuilder('u') |
|
51
|
2 |
|
->where("LOWER(u.{$field}) = :value") |
|
52
|
2 |
|
->setParameter('value', $value) |
|
53
|
2 |
|
->getQuery() |
|
54
|
2 |
|
->getResult(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|