UserRepository::getUsersByCriteria()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 96
    public function __construct(RegistryInterface $registry)
23
    {
24 96
        parent::__construct($registry, User::class);
25 96
    }
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
     *
49
     * @throws NonUniqueResultException
50
     *
51
     * @return User|null
52
     */
53 3
    public function loadUserByEmail(string $email): ?User
54
    {
55 3
        return $this->createQueryBuilder('u')
56 3
            ->where('u.email = :email')
57 3
            ->setParameter('email', $email)
58 3
            ->getQuery()
59 3
            ->getOneOrNullResult();
60
    }
61
62
    /**
63
     * @param array $criteria
64
     *
65
     * @return mixed
66
     */
67 3
    public function getUsersByCriteria(array $criteria)
68
    {
69 3
        $field = key($criteria);
70 3
        $value = mb_strtolower(reset($criteria));
71
72 3
        return $this->createQueryBuilder('u')
73 3
            ->where("LOWER(u.{$field}) = :value")
74 3
            ->setParameter('value', $value)
75 3
            ->getQuery()
76 3
            ->getResult();
77
    }
78
79
    /**
80
     * I do not understand why getSingleScalarResult throws NoResultException but it does.
81
     *
82
     * @param array $criteria
83
     *
84
     * @return bool
85
     */
86 4
    public function isUserExists(array $criteria): bool
87
    {
88 4
        $field = key($criteria);
89 4
        $value = mb_strtolower($criteria[$field]);
90
91
        try {
92 4
            $user = $this->createQueryBuilder('u')
93 4
                ->where("LOWER(u.{$field}) = :value")
94 4
                ->setParameter('value', $value)
95 4
                ->getQuery()
96 4
                ->getSingleScalarResult();
97 4
        } catch (NonUniqueResultException $nonUniqueResultException) {
98 2
            return true;
99 2
        } catch (NoResultException $noResultException) {
100 2
            return false;
101
        }
102
103
        return $user !== null;
104
    }
105
}
106