19
19
20
20
class FlashListener implements EventSubscriberInterface
21
21
{
22
+ private static $ successMessages = array (
23
+ FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'change_password.flash.updated ' ,
24
+ FOSUserEvents::PROFILE_EDIT_SUCCESS => 'profile.flash.updated ' ,
25
+ FOSUserEvents::REGISTRATION_SUCCESS => 'registration.flash.user_created ' ,
26
+ FOSUserEvents::RESETTING_RESET_SUCCESS => 'resetting.flash.success '
27
+ );
28
+
22
29
/**
23
30
* @var \Symfony\Component\HttpFoundation\Session\Session
24
31
*/
@@ -34,31 +41,20 @@ public function __construct(SessionInterface $session, TranslatorInterface $tran
34
41
public static function getSubscribedEvents ()
35
42
{
36
43
return array (
37
- FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'onChangePasswordSuccess ' ,
38
- FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess ' ,
39
- FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess ' ,
40
- FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess '
44
+ FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'addSuccessFlash ' ,
45
+ FOSUserEvents::PROFILE_EDIT_SUCCESS => 'addSuccessFlash ' ,
46
+ FOSUserEvents::REGISTRATION_SUCCESS => 'addSuccessFlash ' ,
47
+ FOSUserEvents::RESETTING_RESET_SUCCESS => 'addSuccessFlash '
41
48
);
42
49
}
43
50
44
- public function onProfileEditSuccess (FormEvent $ event )
51
+ public function addSuccessFlash (FormEvent $ event )
45
52
{
46
- $ this ->session ->getFlashBag ()->add ('success ' , $ this ->trans ('profile.flash.updated ' ));
47
- }
48
-
49
- public function onChangePasswordSuccess (FormEvent $ event )
50
- {
51
- $ this ->session ->getFlashBag ()->add ('success ' , $ this ->trans ('change_password.flash.updated ' ));
52
- }
53
+ if (!isset (self ::$ successMessages [$ event ->getName ()])) {
54
+ throw new \InvalidArgumentException ('This event does not correspond to a known flash message ' );
55
+ }
53
56
54
- public function onRegistrationSuccess (FormEvent $ event )
55
- {
56
- $ this ->session ->getFlashBag ()->add ('success ' , $ this ->trans ('registration.flash.user_created ' ));
57
- }
58
-
59
- public function onResettingResetSuccess (FormEvent $ event )
60
- {
61
- $ this ->session ->getFlashBag ()->add ('success ' , $ this ->trans ('resetting.flash.success ' ));
57
+ $ this ->session ->getFlashBag ()->add ('success ' , $ this ->trans (self ::$ successMessages [$ event ->getName ()]));
62
58
}
63
59
64
60
private function trans ($ message , array $ params = array ())
0 commit comments