Completed
Push — master ( 712d21...572f95 )
by Valentyn
05:22
created

UserRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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