ProfileFormHandler.php 2.0 KB

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