1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\User; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Exception\NoRepositoriesRegisteredException; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Exception\RepositoryNotRegisteredException; |
7
|
|
|
|
8
|
|
|
class UserRepositoryCollection |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $userRepositories = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* UserRepositoryCollection constructor. |
17
|
|
|
* |
18
|
|
|
* @param array $userRepositories |
19
|
|
|
*/ |
20
|
|
|
public function __construct(array $userRepositories) |
21
|
|
|
{ |
22
|
|
|
foreach ($userRepositories as $repository) { |
23
|
|
|
$this->addUserRepository($repository); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Registers the UserRepository to the UserRepositoryCollection. |
29
|
|
|
* |
30
|
|
|
* @param UserRepository $userRepository |
31
|
|
|
*/ |
32
|
|
|
public function addUserRepository(UserRepository $userRepository) |
33
|
|
|
{ |
34
|
|
|
$this->userRepositories[] = $userRepository; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the userRepositories. |
39
|
|
|
* |
40
|
|
|
* @throws NoRepositoriesRegisteredException |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function all() |
45
|
|
|
{ |
46
|
|
|
if (count($this->userRepositories) === 0) { |
47
|
|
|
throw new NoRepositoriesRegisteredException('No user repositories registered'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->userRepositories; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Find the UserRepository for a given User Class. |
55
|
|
|
* |
56
|
|
|
* @param $className |
57
|
|
|
* |
58
|
|
|
* @throws RepositoryNotRegisteredException |
59
|
|
|
* |
60
|
|
|
* @return UserRepository |
61
|
|
|
*/ |
62
|
|
|
public function findRepositoryByClassName($className) |
63
|
|
|
{ |
64
|
|
|
foreach ($this->userRepositories as $repository) { |
65
|
|
|
if ($repository->supportsClass($className)) { |
66
|
|
|
return $repository; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw RepositoryNotRegisteredException::withClassName($className); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $token |
75
|
|
|
* |
76
|
|
|
* @return UserInterface|null |
77
|
|
|
*/ |
78
|
|
|
public function findUserByToken($token) |
79
|
|
|
{ |
80
|
|
|
foreach ($this->userRepositories as $repository) { |
81
|
|
|
$user = $repository->findByPasswordResetToken($token); |
82
|
|
|
|
83
|
|
|
if ($user) { |
84
|
|
|
return $user; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $username |
93
|
|
|
*/ |
94
|
|
|
public function findUserByUserName($username) |
95
|
|
|
{ |
96
|
|
|
foreach ($this->userRepositories as $repository) { |
97
|
|
|
$user = $repository->findByUsername($username); |
98
|
|
|
|
99
|
|
|
if ($user) { |
100
|
|
|
return $user; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|