forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[User] Added proper listener to CustomerGuestType
- Loading branch information
Showing
6 changed files
with
245 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/Sylius/Bundle/UserBundle/Form/EventListener/GuestCustomerFormListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\UserBundle\Form\EventListener; | ||
|
||
use Sylius\Bundle\UserBundle\Context\CustomerContext; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Sylius\Component\User\Context\CustomerContextInterface; | ||
use Sylius\Component\User\Model\CustomerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
/** | ||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> | ||
*/ | ||
class GuestCustomerFormListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var RepositoryInterface | ||
*/ | ||
private $customerRepository; | ||
|
||
/** | ||
* @var CustomerContext | ||
*/ | ||
private $customerContext; | ||
|
||
/** | ||
* @param RepositoryInterface $customerRepository | ||
* @param CustomerContextInterface $customerContext | ||
*/ | ||
public function __construct(RepositoryInterface $customerRepository, CustomerContextInterface $customerContext) | ||
{ | ||
$this->customerRepository = $customerRepository; | ||
$this->customerContext = $customerContext; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
FormEvents::PRE_SUBMIT => 'preSubmit', | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function preSubmit(FormEvent $event) | ||
{ | ||
$rawData = $event->getData(); | ||
$form = $event->getForm(); | ||
|
||
$customer = $this->getCustomerFromProperSource($rawData, $form); | ||
|
||
$form->setData($customer); | ||
} | ||
|
||
/** | ||
* @param array $rawData | ||
* @param FormInterface $form | ||
* | ||
* @return null|CustomerInterface | ||
*/ | ||
protected function getCustomerFromProperSource($rawData, FormInterface $form) | ||
{ | ||
if (null !== $customer = $this->customerContext->getCustomer()) { | ||
$form->remove('email'); | ||
|
||
return $customer; | ||
} | ||
|
||
if (!isset($rawData['email']) || empty($rawData['email'])) { | ||
return null; | ||
} | ||
|
||
return $this->getCustomer($rawData['email']); | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* | ||
* @return CustomerInterface|null | ||
*/ | ||
protected function getCustomer($email) | ||
{ | ||
$customer = $this->customerRepository->findOneBy(array('email' => $email)); | ||
|
||
if (null !== $customer && null !== $customer->getUser()) { | ||
return null; | ||
} | ||
|
||
if (null === $customer) { | ||
$customer = $this->customerRepository->createNew(); | ||
$customer->setEmail($email); | ||
} | ||
|
||
return $customer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/Sylius/Bundle/UserBundle/spec/Form/EventListener/GuestCustomerFormListenerSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace spec\Sylius\Bundle\UserBundle\Form\EventListener; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\UserInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Sylius\Component\User\Context\CustomerContextInterface; | ||
use Sylius\Component\User\Model\CustomerInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
/** | ||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> | ||
*/ | ||
class GuestCustomerFormListenerSpec extends ObjectBehavior | ||
{ | ||
function let(RepositoryInterface $customerRepository, CustomerContextInterface $customerContext) | ||
{ | ||
$this->beConstructedWith($customerRepository, $customerContext); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Sylius\Bundle\UserBundle\Form\EventListener\GuestCustomerFormListener'); | ||
} | ||
|
||
function it_implements_event_subscriber_interface() | ||
{ | ||
$this->shouldImplement('Symfony\Component\EventDispatcher\EventSubscriberInterface'); | ||
} | ||
|
||
function it_sets_currently_logged_user_as_form_data( | ||
$customerContext, | ||
CustomerInterface $customer, | ||
FormEvent $event, | ||
FormInterface $form | ||
) { | ||
$event->getData()->willReturn(array('email' => null))->shouldBeCalled(); | ||
$event->getForm()->willReturn($form)->shouldBeCalled(); | ||
|
||
$customerContext->getCustomer()->willReturn($customer)->shouldBeCalled(); | ||
|
||
$form->remove('email')->shouldBeCalled(); | ||
$form->setData($customer)->shouldBeCalled(); | ||
|
||
$this->preSubmit($event); | ||
} | ||
|
||
function it_sets_new_customer_with_passed_email_as_form_data_if_customer_with_such_email_does_not_exist( | ||
$customerContext, | ||
$customerRepository, | ||
CustomerInterface $customer, | ||
FormEvent $event, | ||
FormInterface $form | ||
) { | ||
$event->getData()->willReturn(array('email' => 'john.doe@example.com'))->shouldBeCalled(); | ||
$event->getForm()->willReturn($form)->shouldBeCalled(); | ||
|
||
$customerContext->getCustomer()->willReturn(null)->shouldBeCalled(); | ||
$customerRepository->findOneBy(array('email' => 'john.doe@example.com'))->willReturn(null)->shouldBeCalled(); | ||
|
||
$customerRepository->createNew()->willReturn($customer)->shouldBeCalled(); | ||
$customer->setEmail('john.doe@example.com')->shouldBeCalled(); | ||
|
||
$form->setData($customer)->shouldBeCalled(); | ||
|
||
$this->preSubmit($event); | ||
} | ||
|
||
function it_sets_null_as_form_data_if_no_customer_is_logged_in_and_email_was_not_passed( | ||
$customerContext, | ||
FormEvent $event, | ||
FormInterface $form | ||
) { | ||
$event->getData()->willReturn(array())->shouldBeCalled(); | ||
$event->getForm()->willReturn($form)->shouldBeCalled(); | ||
|
||
$customerContext->getCustomer()->willReturn(null)->shouldBeCalled(); | ||
|
||
$form->setData(null)->shouldBeCalled(); | ||
|
||
$this->preSubmit($event); | ||
} | ||
|
||
function it_sets_null_as_form_data_if_customer_with_passed_email_already_exist( | ||
$customerContext, | ||
$customerRepository, | ||
CustomerInterface $customer, | ||
FormEvent $event, | ||
FormInterface $form, | ||
UserInterface $user | ||
) { | ||
$event->getData()->willReturn(array('email' => 'john.doe@example.com'))->shouldBeCalled(); | ||
$event->getForm()->willReturn($form)->shouldBeCalled(); | ||
|
||
$customerContext->getCustomer()->willReturn(null)->shouldBeCalled(); | ||
$customerRepository->findOneBy(array('email' => 'john.doe@example.com'))->willReturn($customer)->shouldBeCalled(); | ||
$customer->getUser()->willReturn($user)->shouldBeCalled(); | ||
|
||
$form->setData(null)->shouldBeCalled(); | ||
|
||
$this->preSubmit($event); | ||
} | ||
} |