Completed
Push — master ( ecedb8...999d6a )
by Jeff
04:44 queued 02:17
created

UserRepository::loadUserByUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Repository;
12
13
use App\Entity\User;
14
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
15
use Doctrine\Persistence\ManagerRegistry;
16
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
17
18
/**
19
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
20
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
21
 * @method User[]    findAll()
22
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
23
 */
24
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
25
{
26
    public function __construct(ManagerRegistry $registry)
27
    {
28
        parent::__construct($registry, User::class);
29
    }
30
31
    public function findOneByEmailAddress(string $emailAddress): ?User
32
    {
33
        $parts = explode('@', $emailAddress, 2);
34
35
        if (2 !== \count($parts)) {
36
            return null;
37
        }
38
39
        $qb = $this->createQueryBuilder('user');
40
        $qb
41
            ->join('user.domain', 'domain')
42
            ->andWhere($qb->expr()->eq('user.name', ':localPart'))
43
            ->andWhere($qb->expr()->eq('domain.name', ':domainPart'))
44
            ->setParameter('localPart', $parts[0])
45
            ->setParameter('domainPart', $parts[1]);
46
47
        return $qb->getQuery()->getOneOrNullResult();
48
    }
49
50
    public function loadUserByUsername($username)
51
    {
52
        return $this->findOneByEmailAddress((string) $username);
53
    }
54
}
55