ObjectUserProvider::refreshUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
6
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection;
7
use Symfony\Component\Security\Core\User\UserProviderInterface;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
12 View Code Duplication
class ObjectUserProvider implements UserProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /** @var BaseUserRepositoryCollection */
15
    private $userRepositoryCollection;
16
17
    public function __construct(BaseUserRepositoryCollection $userRepositoryCollection)
18
    {
19
        $this->userRepositoryCollection = $userRepositoryCollection;
20
    }
21
22
    /**
23
     * @param string $username
24
     *
25
     * @return User
26
     *
27
     * @throws UsernameNotFoundException
28
     */
29
    public function loadUserByUsername($username): User
30
    {
31
        foreach ($this->userRepositoryCollection->all() as $repository) {
32
            $user = $repository->findByUsername($username);
33
34
            if ($user instanceof User) {
35
                return $user;
36
            }
37
        }
38
39
        throw new UsernameNotFoundException(
40
            sprintf('Username "%s" does not exist.', $username)
41
        );
42
    }
43
44
    /**
45
     * @param UserInterface $user
46
     *
47
     * @return User
48
     *
49
     * @throws UnsupportedUserException
50
     */
51
    public function refreshUser(UserInterface $user): User
52
    {
53
        if (!$this->supportsClass(get_class($user))) {
54
            throw new UnsupportedUserException(
55
                sprintf('Instances of "%s" are not supported.', get_class($user))
56
            );
57
        }
58
59
        return $this->loadUserByUsername($user->getUsername());
60
    }
61
62
    public function supportsClass($class): bool
63
    {
64
        return $this->userRepositoryCollection->supportsClass($class);
65
    }
66
}
67