Skip to content

Commit f48cc28

Browse files
committed
Refactored the profile controller to use events
1 parent 27663a6 commit f48cc28

File tree

8 files changed

+60
-105
lines changed

8 files changed

+60
-105
lines changed

Controller/ProfileController.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\UserResponseEvent;
17+
use FOS\UserBundle\Model\UserInterface;
1418
use Symfony\Component\DependencyInjection\ContainerAware;
19+
use Symfony\Component\HttpFoundation\Request;
1520
use Symfony\Component\HttpFoundation\RedirectResponse;
1621
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17-
use FOS\UserBundle\Model\UserInterface;
1822

1923
/**
2024
* Controller managing the user profile
@@ -39,47 +43,47 @@ public function showAction()
3943
/**
4044
* Edit the user
4145
*/
42-
public function editAction()
46+
public function editAction(Request $request)
4347
{
4448
$user = $this->container->get('security.context')->getToken()->getUser();
4549
if (!is_object($user) || !$user instanceof UserInterface) {
4650
throw new AccessDeniedException('This user does not have access to this section.');
4751
}
4852

49-
$form = $this->container->get('fos_user.profile.form');
50-
$formHandler = $this->container->get('fos_user.profile.form.handler');
53+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
54+
$formFactory = $this->container->get('fos_user.profile.form.factory');
55+
56+
$form = $formFactory->createForm();
57+
$form->setData($user);
58+
59+
if ('POST' === $request->getMethod()) {
60+
$form->bind($request);
61+
62+
if ($form->isValid()) {
63+
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
64+
$userManager = $this->container->get('fos_user.user_manager');
65+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
66+
$dispatcher = $this->container->get('event_dispatcher');
5167

52-
$process = $formHandler->process($user);
53-
if ($process) {
54-
$this->setFlash('fos_user_success', 'profile.flash.updated');
68+
$event = new FormEvent($form);
69+
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
5570

56-
return new RedirectResponse($this->getRedirectionUrl($user));
71+
$userManager->updateUser($user);
72+
73+
if (null === $response = $event->getResponse()) {
74+
$url = $this->container->get('router')->generate('fos_user_profile_show');
75+
$response = new RedirectResponse($url);
76+
}
77+
78+
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new UserResponseEvent($user, $response));
79+
80+
return $response;
81+
}
5782
}
5883

5984
return $this->container->get('templating')->renderResponse(
6085
'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
6186
array('form' => $form->createView())
6287
);
6388
}
64-
65-
/**
66-
* Generate the redirection url when editing is completed.
67-
*
68-
* @param \FOS\UserBundle\Model\UserInterface $user
69-
*
70-
* @return string
71-
*/
72-
protected function getRedirectionUrl(UserInterface $user)
73-
{
74-
return $this->container->get('router')->generate('fos_user_profile_show');
75-
}
76-
77-
/**
78-
* @param string $action
79-
* @param string $value
80-
*/
81-
protected function setFlash($action, $value)
82-
{
83-
$this->container->get('session')->setFlash($action, $value);
84-
}
8589
}

DependencyInjection/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ private function addProfileSection(ArrayNodeDefinition $node)
9494
->addDefaultsIfNotSet()
9595
->children()
9696
->scalarNode('type')->defaultValue('fos_user_profile')->end()
97-
->scalarNode('handler')->defaultValue('fos_user.profile.form.handler.default')->end()
9897
->scalarNode('name')->defaultValue('fos_user_profile_form')->end()
9998
->arrayNode('validation_groups')
10099
->prototype('scalar')->end()

DependencyInjection/FOSUserExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ private function loadProfile(array $config, ContainerBuilder $container, XmlFile
102102
{
103103
$loader->load('profile.xml');
104104

105-
$container->setAlias('fos_user.profile.form.handler', $config['form']['handler']);
106-
unset($config['form']['handler']);
107-
108105
$this->remapParametersNamespaces($config, $container, array(
109106
'form' => 'fos_user.profile.form.%s',
110107
));

EventListener/FlashListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ public function __construct(SessionInterface $session, TranslatorInterface $tran
3434
public static function getSubscribedEvents()
3535
{
3636
return array(
37+
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEditSuccess',
3738
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
3839
);
3940
}
4041

42+
public function onProfileEditSuccess(FormEvent $event)
43+
{
44+
$this->session->getFlashBag()->add('success', $this->trans('profile.flash.updated'));
45+
}
46+
4147
public function onRegistrationSuccess(FormEvent $event)
4248
{
4349
$this->session->getFlashBag()->add('success', $this->trans('registration.flash.user_created'));

FOSUserEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
*/
1717
final class FOSUserEvents
1818
{
19+
/**
20+
* The PROFILE_EDIT_SUCCESS event occurs when the profile edit form is submitted successfully.
21+
*
22+
* This event allows you to set the response instead of using the default one.
23+
* The event listener method receives a FOS\UserBundle\Event\FormEvent instance.
24+
*/
25+
const PROFILE_EDIT_SUCCESS = 'fos_user.profile.edit.success';
26+
27+
/**
28+
* The PROFILE_EDIT_COMPLETED event occurs after saving the user in the profile edit process.
29+
*
30+
* This event allows you to access the response which will be sent.
31+
* The event listener method receives a FOS\UserBundle\Event\UserResponseEvent instance.
32+
*/
33+
const PROFILE_EDIT_COMPLETED = 'fos_user.profile.edit.completed';
34+
1935
/**
2036
* The REGISTRATION_INITIALIZE event occurs when the registration process is initialized.
2137
*

Form/Handler/ProfileFormHandler.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

Resources/config/profile.xml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,18 @@
66

77
<services>
88

9-
<service id="fos_user.profile.form" factory-method="createNamed" factory-service="form.factory" class="Symfony\Component\Form\Form">
9+
<service id="fos_user.profile.form.factory" class="FOS\UserBundle\Form\Factory\FormFactory">
10+
<argument type="service" id="form.factory" />
1011
<argument>%fos_user.profile.form.name%</argument>
1112
<argument>%fos_user.profile.form.type%</argument>
12-
<argument>null</argument>
13-
<argument type="collection">
14-
<argument key="validation_groups">%fos_user.profile.form.validation_groups%</argument>
15-
</argument>
13+
<argument>%fos_user.profile.form.validation_groups%</argument>
1614
</service>
1715

1816
<service id="fos_user.profile.form.type" class="FOS\UserBundle\Form\Type\ProfileFormType">
1917
<argument>%fos_user.model.user.class%</argument>
2018
<tag name="form.type" alias="fos_user_profile" />
2119
</service>
2220

23-
<service id="fos_user.profile.form.handler.default" class="FOS\UserBundle\Form\Handler\ProfileFormHandler" scope="request" public="false">
24-
<argument type="service" id="fos_user.profile.form" />
25-
<argument type="service" id="request" />
26-
<argument type="service" id="fos_user.user_manager" />
27-
</service>
28-
2921
</services>
3022

3123
</container>

Tests/DependencyInjection/FOSUserExtensionTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function testUserLoadFormServiceWithDefaults()
219219
{
220220
$this->createEmptyConfiguration();
221221

222-
$this->assertHasDefinition('fos_user.profile.form');
222+
$this->assertHasDefinition('fos_user.profile.form.factory');
223223
$this->assertHasDefinition('fos_user.registration.form.factory');
224224
$this->assertNotHasDefinition('fos_user.group.form');
225225
$this->assertHasDefinition('fos_user.change_password.form');
@@ -230,7 +230,7 @@ public function testUserLoadFormService()
230230
{
231231
$this->createFullConfiguration();
232232

233-
$this->assertHasDefinition('fos_user.profile.form');
233+
$this->assertHasDefinition('fos_user.profile.form.factory');
234234
$this->assertHasDefinition('fos_user.registration.form.factory');
235235
$this->assertHasDefinition('fos_user.group.form');
236236
$this->assertHasDefinition('fos_user.change_password.form');
@@ -342,7 +342,6 @@ protected function getFullConfig()
342342
profile:
343343
form:
344344
type: acme_my_profile
345-
handler: acme_my.form.handler.profile
346345
name: acme_profile_form
347346
validation_groups: [acme_profile]
348347
change_password:

0 commit comments

Comments
 (0)