UserProvider::refreshUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\TestRoleHierarchyBundle\Util;
13
14
use SpomkyLabs\TestRoleHierarchyBundle\Entity\User;
15
use SpomkyLabs\TestRoleHierarchyBundle\Entity\UserManager;
16
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
17
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
use Symfony\Component\Security\Core\User\UserProviderInterface;
20
21
class UserProvider implements UserProviderInterface
22
{
23
    private $user_manager;
24
25
    public function __construct(UserManager $user_manager)
26
    {
27
        $this->user_manager = $user_manager;
28
    }
29
30
    public function loadUserByUsername($username)
31
    {
32
        $user = $this->user_manager->getUser($username);
33
34
        if ($user) {
35
            return $user;
36
        }
37
38
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
39
    }
40
41
    public function refreshUser(UserInterface $user)
42
    {
43
        if (!$user instanceof User) {
44
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
45
        }
46
47
        return $this->loadUserByUsername($user->getUsername());
48
    }
49
50
    public function supportsClass($class)
51
    {
52
        return $class === 'SpomkyLabs\TestRoleHierarchyBundle\Entity\User';
53
    }
54
}
55