ProfileFormHandler.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace Sonata\UserBundle\Form\Handler;
  12. use Symfony\Component\Form\Form;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use FOS\UserBundle\Model\UserInterface;
  15. use FOS\UserBundle\Model\UserManagerInterface;
  16. /**
  17. *
  18. * This file is an adapted version of FOS User Bundle ProfileFormHandler class
  19. *
  20. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  21. */
  22. class ProfileFormHandler
  23. {
  24. /**
  25. * @var \Symfony\Component\HttpFoundation\Request
  26. */
  27. protected $request;
  28. /**
  29. * @var \FOS\UserBundle\Model\UserManagerInterface
  30. */
  31. protected $userManager;
  32. /**
  33. * @var \Symfony\Component\Form\Form
  34. */
  35. protected $form;
  36. /**
  37. * @param Form $form
  38. * @param \Symfony\Component\HttpFoundation\Request $request
  39. * @param UserManagerInterface $userManager
  40. */
  41. public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
  42. {
  43. $this->form = $form;
  44. $this->request = $request;
  45. $this->userManager = $userManager;
  46. }
  47. /**
  48. * @param UserInterface $user
  49. *
  50. * @return bool
  51. */
  52. public function process(UserInterface $user)
  53. {
  54. $this->form->setData($user);
  55. if ('POST' == $this->request->getMethod()) {
  56. $this->form->bindRequest($this->request);
  57. if ($this->form->isValid()) {
  58. $this->onSuccess($user);
  59. return true;
  60. }
  61. // Reloads the user to reset its username. This is needed when the
  62. // username or password have been changed to avoid issues with the
  63. // security layer.
  64. $this->userManager->reloadUser($user);
  65. }
  66. return false;
  67. }
  68. /**
  69. * @param UserInterface $user
  70. */
  71. protected function onSuccess(UserInterface $user)
  72. {
  73. $this->userManager->updateUser($user);
  74. }
  75. }