27
27
*/
28
28
class EntityUserProvider implements UserProviderInterface
29
29
{
30
+ private $ registry ;
31
+ private $ managerName ;
32
+ private $ classOrAlias ;
30
33
private $ class ;
31
- private $ repository ;
32
34
private $ property ;
33
- private $ metadata ;
34
35
35
- public function __construct (ManagerRegistry $ registry , $ class , $ property = null , $ managerName = null )
36
+ public function __construct (ManagerRegistry $ registry , $ classOrAlias , $ property = null , $ managerName = null )
36
37
{
37
- $ em = $ registry ->getManager ($ managerName );
38
- $ this ->class = $ class ;
39
- $ this ->metadata = $ em ->getClassMetadata ($ class );
40
-
41
- if (false !== strpos ($ this ->class , ': ' )) {
42
- $ this ->class = $ this ->metadata ->getName ();
43
- }
44
-
45
- $ this ->repository = $ em ->getRepository ($ class );
38
+ $ this ->registry = $ registry ;
39
+ $ this ->managerName = $ managerName ;
40
+ $ this ->classOrAlias = $ classOrAlias ;
46
41
$ this ->property = $ property ;
47
42
}
48
43
@@ -51,14 +46,15 @@ public function __construct(ManagerRegistry $registry, $class, $property = null,
51
46
*/
52
47
public function loadUserByUsername ($ username )
53
48
{
49
+ $ repository = $ this ->getRepository ();
54
50
if (null !== $ this ->property ) {
55
- $ user = $ this -> repository ->findOneBy (array ($ this ->property => $ username ));
51
+ $ user = $ repository ->findOneBy (array ($ this ->property => $ username ));
56
52
} else {
57
- if (!$ this -> repository instanceof UserProviderInterface) {
58
- throw new \InvalidArgumentException (sprintf ('The Doctrine repository "%s" must implement UserProviderInterface. ' , get_class ($ this -> repository )));
53
+ if (!$ repository instanceof UserProviderInterface) {
54
+ throw new \InvalidArgumentException (sprintf ('The Doctrine repository "%s" must implement UserProviderInterface. ' , get_class ($ repository )));
59
55
}
60
56
61
- $ user = $ this -> repository ->loadUserByUsername ($ username );
57
+ $ user = $ repository ->loadUserByUsername ($ username );
62
58
}
63
59
64
60
if (null === $ user ) {
@@ -73,26 +69,28 @@ public function loadUserByUsername($username)
73
69
*/
74
70
public function refreshUser (UserInterface $ user )
75
71
{
76
- if (!$ user instanceof $ this ->class ) {
72
+ $ class = $ this ->getClass ();
73
+ if (!$ user instanceof $ class ) {
77
74
throw new UnsupportedUserException (sprintf ('Instances of "%s" are not supported. ' , get_class ($ user )));
78
75
}
79
76
80
- if ($ this ->repository instanceof UserProviderInterface) {
81
- $ refreshedUser = $ this ->repository ->refreshUser ($ user );
77
+ $ repository = $ this ->getRepository ();
78
+ if ($ repository instanceof UserProviderInterface) {
79
+ $ refreshedUser = $ repository ->refreshUser ($ user );
82
80
} else {
83
81
// The user must be reloaded via the primary key as all other data
84
82
// might have changed without proper persistence in the database.
85
83
// That's the case when the user has been changed by a form with
86
84
// validation errors.
87
- if (!$ id = $ this ->metadata ->getIdentifierValues ($ user )) {
85
+ if (!$ id = $ this ->getClassMetadata () ->getIdentifierValues ($ user )) {
88
86
throw new \InvalidArgumentException ('You cannot refresh a user ' .
89
87
'from the EntityUserProvider that does not contain an identifier. ' .
90
88
'The user object has to be serialized with its own identifier ' .
91
89
'mapped by Doctrine. '
92
90
);
93
91
}
94
92
95
- $ refreshedUser = $ this -> repository ->find ($ id );
93
+ $ refreshedUser = $ repository ->find ($ id );
96
94
if (null === $ refreshedUser ) {
97
95
throw new UsernameNotFoundException (sprintf ('User with id %s not found ' , json_encode ($ id )));
98
96
}
@@ -106,6 +104,36 @@ public function refreshUser(UserInterface $user)
106
104
*/
107
105
public function supportsClass ($ class )
108
106
{
109
- return $ class === $ this ->class || is_subclass_of ($ class , $ this ->class );
107
+ return $ class === $ this ->getClass () || is_subclass_of ($ class , $ this ->getClass ());
108
+ }
109
+
110
+ private function getEntityManager ()
111
+ {
112
+ return $ this ->registry ->getManager ($ this ->managerName );
113
+ }
114
+
115
+ private function getRepository ()
116
+ {
117
+ return $ this ->getEntityManager ()->getRepository ($ this ->getClass ());
118
+ }
119
+
120
+ private function getClass ()
121
+ {
122
+ if (null === $ this ->class ) {
123
+ $ class = $ this ->classOrAlias ;
124
+
125
+ if (false !== strpos ($ class , ': ' )) {
126
+ $ class = $ this ->getClassMetadata ()->getName ();
127
+ }
128
+
129
+ $ this ->class = $ class ;
130
+ }
131
+
132
+ return $ this ->class ;
133
+ }
134
+
135
+ private function getClassMetadata ()
136
+ {
137
+ return $ this ->getEntityManager ()->getClassMetadata ($ this ->getClass ());
110
138
}
111
139
}
0 commit comments