Completed
Push — master ( a0c302...199c16 )
by Valentyn
03:15
created

UserRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 65
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadUserByUsername() 0 10 1
A getUsersByCriteria() 0 11 1
A isUserExists() 0 19 3
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