|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the FOSUserBundle package. |
|
5 * |
|
6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace FOS\UserBundle\Form\Handler; |
|
13 |
|
14 use Symfony\Component\Form\Form; |
|
15 use Symfony\Component\HttpFoundation\Request; |
|
16 use FOS\UserBundle\Model\UserManagerInterface; |
|
17 use FOS\UserBundle\Model\UserInterface; |
|
18 use FOS\UserBundle\Mailer\MailerInterface; |
|
19 |
|
20 class RegistrationFormHandler |
|
21 { |
|
22 protected $request; |
|
23 protected $userManager; |
|
24 protected $form; |
|
25 protected $mailer; |
|
26 |
|
27 public function __construct(Form $form, Request $request, UserManagerInterface $userManager, MailerInterface $mailer) |
|
28 { |
|
29 $this->form = $form; |
|
30 $this->request = $request; |
|
31 $this->userManager = $userManager; |
|
32 $this->mailer = $mailer; |
|
33 } |
|
34 |
|
35 public function process($confirmation = false) |
|
36 { |
|
37 $user = $this->userManager->createUser(); |
|
38 $this->form->setData($user); |
|
39 |
|
40 if ('POST' == $this->request->getMethod()) { |
|
41 $this->form->bindRequest($this->request); |
|
42 |
|
43 if ($this->form->isValid()) { |
|
44 $this->onSuccess($user, $confirmation); |
|
45 |
|
46 return true; |
|
47 } |
|
48 } |
|
49 |
|
50 return false; |
|
51 } |
|
52 |
|
53 protected function onSuccess(UserInterface $user, $confirmation) |
|
54 { |
|
55 if ($confirmation) { |
|
56 $user->setEnabled(false); |
|
57 $this->mailer->sendConfirmationEmailMessage($user); |
|
58 } else { |
|
59 $user->setConfirmationToken(null); |
|
60 $user->setEnabled(true); |
|
61 } |
|
62 |
|
63 $this->userManager->updateUser($user); |
|
64 } |
|
65 } |