UserProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 43
ccs 6
cts 18
cp 0.3333
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 refreshUser() 0 10 2
A supportsClass() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\Security\User;
12
13
use App\Entity\Access\User;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Security\Core\User\UserProviderInterface;
18
19
class UserProvider implements UserProviderInterface
20
{
21
    /** @var EntityManagerInterface */
22
    private $entityManager;
23
24 21
    public function __construct(EntityManagerInterface $entityManager)
25
    {
26 21
        $this->entityManager = $entityManager;
27 21
    }
28
29
    /**
30
     * @param string $username The username
31
     */
32
    public function loadUserByUsername($username): UserInterface
33
    {
34
        return $this->entityManager
35
            ->createQueryBuilder()
36
            ->select(User::class, 'u')
37
            ->where('u.email = :email')
38
            ->setParameter('email', $username)
39
            ->getQuery()
40
            ->getOneOrNullResult();
41
    }
42
43 17
    public function refreshUser(UserInterface $user): UserInterface
44
    {
45 17
        if (!$user instanceof User) {
46
            throw new UnsupportedUserException(
47
                sprintf('Instances of "%s" are not supported.', \get_class($user))
48
            );
49
        }
50
51 17
        return $user;
52
    }
53
54
    /**
55
     * @param string $class
56
     */
57
    public function supportsClass($class): bool
58
    {
59
        return User::class === $class;
60
    }
61
}
62