1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\UserBundle\Form\EventSubscriber; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
15
|
|
|
use Sylius\Component\User\Model\CustomerInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
18
|
|
|
use Symfony\Component\Form\FormEvent; |
19
|
|
|
use Symfony\Component\Form\FormEvents; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Michał Marcinkowski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CustomerRegistrationFormSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $customerRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param RepositoryInterface $customerRepository |
33
|
|
|
*/ |
34
|
|
|
public function __construct(RepositoryInterface $customerRepository) |
35
|
|
|
{ |
36
|
|
|
$this->customerRepository = $customerRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function preSubmit(FormEvent $event) |
53
|
|
|
{ |
54
|
|
|
$rawData = $event->getData(); |
55
|
|
|
$form = $event->getForm(); |
56
|
|
|
$data = $form->getData(); |
57
|
|
|
|
58
|
|
|
if (!$data instanceof CustomerInterface) { |
59
|
|
|
throw new UnexpectedTypeException($data, CustomerInterface::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// if email is not filled in, go on |
63
|
|
|
if (!isset($rawData['email']) || empty($rawData['email'])) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
$existingCustomer = $this->customerRepository->findOneBy(['email' => $rawData['email']]); |
67
|
|
|
if (null === $existingCustomer || null !== $existingCustomer->getUser()) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$existingCustomer->setUser($data->getUser()); |
72
|
|
|
$form->setData($existingCustomer); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|