Skip to content

Commit 09363a2

Browse files
committed
Refactored the FlashListener to keep it more DRY
1 parent 5668ef4 commit 09363a2

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

EventListener/FlashListener.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
class FlashListener implements EventSubscriberInterface
2121
{
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+
2229
/**
2330
* @var \Symfony\Component\HttpFoundation\Session\Session
2431
*/
@@ -34,31 +41,20 @@ public function __construct(SessionInterface $session, TranslatorInterface $tran
3441
public static function getSubscribedEvents()
3542
{
3643
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'
4148
);
4249
}
4350

44-
public function onProfileEditSuccess(FormEvent $event)
51+
public function addSuccessFlash(FormEvent $event)
4552
{
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+
}
5356

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()]));
6258
}
6359

6460
private function trans($message, array $params = array())

0 commit comments

Comments
 (0)