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 |
|
|
|
|
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
|
|
|
|
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.