Completed
Push — develop ( 456f1c...054456 )
by Marek
03:20
created

FormUserProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B loadUserByUsername() 0 22 4
A refreshUser() 0 8 2
A supportsClass() 0 4 1
1
<?php
2
namespace AppBundle\Security;
3
4
use AppBundle\Entity\User;
5
use AppBundle\Service\UserService;
6
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
7
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Symfony\Component\Security\Core\User\UserProviderInterface;
10
11
class FormUserProvider implements UserProviderInterface
12
{
13
    /** @var UserService */
14
    private $userService;
15
16
    public function __construct(UserService $userService)
17
    {
18
        $this->userService = $userService;
19
    }
20
21
    /**
22
     * @inheritdoc
23
     *
24
     * @param string $username Compound username [type:username]
25
     *
26
     * @return null|User
27
     */
28
    public function loadUserByUsername($username)
29
    {
30
        list($type, $username) = explode(':', $username, 2);
31
32
        $user = null;
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
        switch ($type) {
34
            case UserCredentials::TYPE_ID:
35
                $user = $this->userService->getUserById($username);
36
                break;
37
            case UserCredentials::TYPE_NAME:
38
                $user = $this->userService->getUserByLogin($username);
39
                break;
40
            default:
41
                throw new \InvalidArgumentException('Invalid user credentials type');
42
        }
43
44
        if ($user === null) {
45
            throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
46
        }
47
48
        return $user;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     *
54
     * @param UserInterface $user
55
     *
56
     * @throws UnsupportedUserException if the account is not supported
57
     *
58
     * @return UserInterface
59
     */
60
    public function refreshUser(UserInterface $user)
61
    {
62
        if (!$this->supportsClass(get_class($user))) {
63
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
64
        }
65
66
        return $user;
67
    }
68
69
    /**
70
     * Whether this provider supports the given user class.
71
     *
72
     * @param string $class
73
     *
74
     * @return bool
75
     */
76
    public function supportsClass($class)
77
    {
78
        return $class === User::class;
79
    }
80
}
81