@@ -56,39 +56,17 @@ public function newAction(Request $request)
56
56
$ form ->handleRequest ($ request );
57
57
58
58
if ($ form ->isSubmitted () && $ form ->isValid ()) {
59
- try {
60
- $ userManager = $ this ->container ->get ('fos_user.user_manager ' );
61
- $ user = $ userManager ->createUser ();
62
-
63
- $ user ->setFirstname ($ form ['firstname ' ]->getData ());
64
- $ user ->setLastname ($ form ['lastname ' ]->getData ());
65
- $ user ->setDob ($ form ['dob ' ]->getData ());
66
- $ user ->setEmail ($ form ['email ' ]->getData ());
67
- $ user ->setUsername ($ form ['username ' ]->getData ());
68
- $ user ->setPlainPassword ($ form ['password ' ]->getData ());
69
- $ user ->setRoles ($ form ['roles ' ]->getData ());
70
- $ user ->setConfirmationToken (null );
71
- $ user ->setEnabled (true );
72
- $ user ->setLastLogin (new \DateTime ());
73
-
74
- $ userManager ->updateUser ($ user );
75
- $ flashMsg = $ this ->get ('translator ' )->trans ('flash.user_created_successfully ' );
76
- $ this ->addFlash ('success ' , $ flashMsg );
77
-
78
- } catch (HttpException $ e ) {
79
- return $ this ->redirectToRoute ('admin_user_new ' );
80
-
81
- // Always catch exact exception for which flash message or logger is needed,
82
- // otherwise catch block will not get executed on higher or lower ranked exceptions.
83
- } catch (\Doctrine \DBAL \Exception \UniqueConstraintViolationException $ e ) {
84
- $ flashMsg = $ this ->get ('translator ' )->trans ('flash.user_already_exists ' );
85
- $ this ->logMessage (400 , 'danger ' , $ e ->getMessage ());
86
- $ this ->addFlash ('danger ' , $ flashMsg );
87
- return $ this ->redirectToRoute ('admin_user_new ' );
88
- }
59
+ $ userManager = $ this ->container ->get ('fos_user.user_manager ' );
60
+ $ user = $ userManager ->createUser ();
61
+
62
+ $ this ->setUserColumns ($ user , $ form );
63
+
64
+ $ userManager ->updateUser ($ user );
65
+
66
+ $ this ->logMessageAndFlash (200 , 'success ' , 'User successfully created: ' , $ this ->get ('translator ' )->trans ('flash.user_creatd_successfully ' ), $ request ->getLocale () );
89
67
90
68
return $ this ->redirectToRoute ('admin_user_index ' );
91
- } // if form is valid
69
+ }
92
70
93
71
return $ this ->render ('@ApiBundle/Resources/views/admin/user/new.html.twig ' , [
94
72
'form ' => $ form ->createView (),
@@ -119,41 +97,19 @@ public function showAction(User $user)
119
97
*/
120
98
public function editAction (User $ user , Request $ request )
121
99
{
122
- $ entityManager = $ this ->getDoctrine ()->getManager ();
123
-
124
100
$ editForm = $ this ->createForm (UserType::class, $ user );
125
101
$ deleteForm = $ this ->createDeleteForm ($ user );
126
102
$ locale = $ request ->getLocale ();
127
103
128
104
$ editForm ->handleRequest ($ request );
129
105
130
106
if ($ editForm ->isSubmitted () && $ editForm ->isValid ()) {
131
- try {
132
- $ user ->setFirstname ($ editForm ['firstname ' ]->getData ());
133
- $ user ->setLastname ($ editForm ['lastname ' ]->getData ());
134
- $ user ->setDob ($ editForm ['dob ' ]->getData ());
135
- $ user ->setEmail ($ editForm ['email ' ]->getData ());
136
- $ user ->setUsername ($ editForm ['username ' ]->getData ());
137
- $ user ->setPlainPassword ($ editForm ['password ' ]->getData ());
138
- $ user ->setRoles ($ editForm ['roles ' ]->getData ());
139
- $ user ->setConfirmationToken (null );
140
- $ user ->setEnabled (true );
141
- $ user ->setLastLogin (new \DateTime ());
142
-
143
- $ entityManager ->flush ();
144
- $ flashMsg = $ this ->get ('translator ' )->trans ('flash.user_updated_successfully ' );
145
- $ this ->addFlash ('success ' , $ flashMsg );
146
-
147
- // Always catch exact exception for which flash message or logger is needed,
148
- // otherwise catch block will not get executed on higher or lower ranked exceptions.
149
- } catch (HttpException $ e ) {
150
- return $ this ->redirectToRoute ('admin_user_edit ' , ['id ' => $ user ->getId ()]);
151
- } catch (\Doctrine \DBAL \Exception \UniqueConstraintViolationException $ e ) {
152
- $ flashMsg = $ this ->get ('translator ' )->trans ('flash.user_already_exists ' );
153
- $ this ->logMessage (400 , 'danger ' , $ e ->getMessage ());
154
- $ this ->addFlash ('danger ' , $ flashMsg );
155
- return $ this ->redirectToRoute ('admin_user_edit ' , ['id ' => $ user ->getId ()]);
156
- }
107
+ $ this ->setUserColumns ($ user , $ editForm );
108
+
109
+ $ entityManager = $ this ->getDoctrine ()->getManager ();
110
+ $ entityManager ->flush ();
111
+
112
+ $ this ->logMessageAndFlash (200 , 'success ' , 'User successfully updated: ' , $ this ->get ('translator ' )->trans ('flash.user_updated_successfully ' ), $ request ->getLocale () );
157
113
158
114
return $ this ->redirectToRoute ('admin_user_index ' );
159
115
}
@@ -179,9 +135,7 @@ public function deleteAction(Request $request, User $user)
179
135
180
136
$ entityManager ->flush ();
181
137
182
- $ flashMsg = $ this ->get ('translator ' )->trans ('flash.user_deleted_successfully ' );
183
- $ this ->logMessage (200 , 'success ' , 'User successfully deleted: ' );
184
- $ this ->addFlash ('success ' , $ flashMsg );
138
+ $ this ->logMessageAndFlash (200 , 'success ' , 'User successfully deleted: ' , $ this ->get ('translator ' )->trans ('flash.user_deleted_successfully ' ), $ request ->getLocale () );
185
139
186
140
return $ this ->redirectToRoute ('admin_user_index ' );
187
141
}
@@ -202,11 +156,24 @@ private function createDeleteForm(User $user)
202
156
;
203
157
}
204
158
159
+ private function setUserColumns (User $ user , \Symfony \Component \Form \Form $ form )
160
+ {
161
+ $ user ->setFirstname ($ form ['firstname ' ]->getData ());
162
+ $ user ->setLastname ($ form ['lastname ' ]->getData ());
163
+ $ user ->setDob ($ form ['dob ' ]->getData ());
164
+ $ user ->setEmail ($ form ['email ' ]->getData ());
165
+ $ user ->setUsername ($ form ['username ' ]->getData ());
166
+ $ user ->setPlainPassword ($ form ['password ' ]->getData ());
167
+ $ user ->setRoles ($ form ['roles ' ]->getData ());
168
+ $ user ->setConfirmationToken (null );
169
+ $ user ->setEnabled (true );
170
+ $ user ->setLastLogin (new \DateTime ());
171
+ }
172
+
205
173
private function logMessageAndFlash ($ code = 200 , $ type = 'success ' , $ logMsg = '' , $ flashMsg = '' , $ locale = 'en ' )
206
174
{
207
175
$ this ->logMessage ($ code , $ type , $ logMsg );
208
176
$ this ->addFlash ($ type , $ flashMsg );
209
- throw new HttpException ($ code , $ logMsg );
210
177
}
211
178
212
179
private function logMessage ($ code = 200 , $ type ='success ' , $ logMsg = '' ) {
0 commit comments