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