Completed
Push — master ( 2798f7...d9c10c )
by
unknown
06:53 queued 04:03
created

ObjectUserEmailProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
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 ObjectUserEmailProvider 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 UserRepositoryCollection */
15
    private $userRepositoryCollection;
16
17
    /**
18
     * @param UserRepositoryCollection $userRepositoryCollection
19
     */
20
    public function __construct(UserRepositoryCollection $userRepositoryCollection)
21
    {
22
        $this->userRepositoryCollection = $userRepositoryCollection;
23
    }
24
25
    public function loadUserByUsername($username)
26
    {
27
        foreach ($this->userRepositoryCollection->all() as $repository) {
28
            $user = $repository->findByEmailAddress($username);
29
30
            if ($user instanceof User) {
31
                return $user;
32
            }
33
        }
34
35
        throw new UsernameNotFoundException(
36
            sprintf('Username "%s" does not exist.', $username)
37
        );
38
    }
39
40
    public function refreshUser(UserInterface $user)
41
    {
42
        if (!$this->supportsClass(get_class($user))) {
43
            throw new UnsupportedUserException(
44
                sprintf('Instances of "%s" are not supported.', get_class($user))
45
            );
46
        }
47
48
        return $this->loadUserByUsername($user->getUsername());
49
    }
50
51
    public function supportsClass($class)
52
    {
53
        return $this->userRepositoryCollection->supportsClass($class);
54
    }
55
}
56