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
|
|
|
/** |
18
|
|
|
* @param UserRepository[] $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 BaseUserRepositoryCollection. |
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 UserRepository[] |
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 BaseUser Class. |
55
|
|
|
* |
56
|
|
|
* @param string $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
|
|
|
* Check if the BaseUserRepositoryCollection supports a BaseUser class. |
75
|
|
|
* |
76
|
|
|
* @param string $className |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function supportsClass($className) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->userRepositories as $repository) { |
83
|
|
|
if ($repository->supportsClass($className)) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param PasswordResetToken $token |
93
|
|
|
* |
94
|
|
|
* @throws UserNotFound |
95
|
|
|
* |
96
|
|
|
* @return User |
97
|
|
|
*/ |
98
|
|
|
public function findUserByToken(PasswordResetToken $token) |
99
|
|
|
{ |
100
|
|
|
foreach ($this->userRepositories as $repository) { |
101
|
|
|
$user = $repository->findByPasswordResetToken($token); |
|
|
|
|
102
|
|
|
|
103
|
|
|
if ($user) { |
104
|
|
|
return $user; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw UserNotFound::withToken($token); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $username |
113
|
|
|
* |
114
|
|
|
* @throws UserNotFound |
115
|
|
|
* |
116
|
|
|
* @return User |
117
|
|
|
*/ |
118
|
|
|
public function findUserByUserName($username) |
119
|
|
|
{ |
120
|
|
|
foreach ($this->userRepositories as $repository) { |
121
|
|
|
$user = $repository->findByUsername($username); |
|
|
|
|
122
|
|
|
|
123
|
|
|
if ($user) { |
124
|
|
|
return $user; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
throw UserNotFound::withUsername($username); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: