[Api] Made sure api checkout has a customer

This commit is contained in:
Jan Goralski 2016-09-13 19:42:47 +02:00
parent 800f978975
commit 9eeff2ac79
7 changed files with 136 additions and 12 deletions

View file

@ -28,7 +28,7 @@ sylius_api_checkout_addressing:
_controller: sylius.controller.order:updateAction
_sylius:
form:
type: sylius_checkout_address
type: sylius_api_checkout_address
options:
customer: expr:service('sylius.repository.customer').find($customerId)
repository:

View file

@ -0,0 +1,62 @@
<?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\CoreBundle\Form\EventSubscriber\Api;
use Sylius\Component\Customer\Model\CustomerAwareInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Webmozart\Assert\Assert;
/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
class SetCustomerFormSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $field;
/**
* @param string $field
*/
public function __construct($field)
{
$this->field = $field;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'preSetData',
];
}
/**
* @param FormEvent $event
*/
public function preSetData(FormEvent $event)
{
$resource = $event->getData();
$customer = $event->getForm()->getConfig()->getOption($this->field);
Assert::isInstanceOf($resource, CustomerAwareInterface::class);
$resource->setCustomer($customer);
$event->setData($resource);
}
}

View file

@ -44,6 +44,9 @@ class SetCustomerFormSubscriber implements EventSubscriberInterface
];
}
/**
* @param FormEvent $event
*/
public function preSubmit(FormEvent $event)
{
$data = $event->getData();

View file

@ -0,0 +1,65 @@
<?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\CoreBundle\Form\Type\Api\Checkout;
use Sylius\Bundle\CoreBundle\Form\EventSubscriber\AddDefaultBillingAddressOnOrderFormSubscriber;
use Sylius\Bundle\CoreBundle\Form\EventSubscriber\Api\SetCustomerFormSubscriber;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Jan Góralski <jan.goralski@lakion.com>
*/
class AddressType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('shippingAddress', 'sylius_address', ['shippable' => true])
->add('billingAddress', 'sylius_address')
->add('differentBillingAddress', 'checkbox', [
'mapped' => false,
'required' => false,
'label' => 'sylius.form.checkout.addressing.different_billing_address',
])
->addEventSubscriber(new AddDefaultBillingAddressOnOrderFormSubscriber())
->addEventSubscriber(new SetCustomerFormSubscriber('customer'))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
$resolver
->setDefaults([
'customer' => null,
'cascade_validation' => true,
])
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_api_checkout_address';
}
}

View file

@ -12,7 +12,6 @@
namespace Sylius\Bundle\CoreBundle\Form\Type\Checkout;
use Sylius\Bundle\CoreBundle\Form\EventSubscriber\AddDefaultBillingAddressOnOrderFormSubscriber;
use Sylius\Bundle\CoreBundle\Form\EventSubscriber\AddEmailGuestFormSubscriber;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\CoreBundle\Form\EventSubscriber\AddCustomerGuestTypeFormSubscriber;
use Symfony\Component\Form\FormBuilderInterface;

View file

@ -27,5 +27,9 @@
<argument>%sylius.model.product.class%</argument>
<tag name="form.type" alias="sylius_api_product" />
</service>
<service id="sylius.form.type.api.checkout_address" class="Sylius\Bundle\CoreBundle\Form\Type\Api\Checkout\AddressType">
<argument>%sylius.model.cart.class%</argument>
<tag name="form.type" alias="sylius_api_checkout_address" />
</service>
</services>
</container>

View file

@ -75,16 +75,7 @@
}
}
},
"differentBillingAddress": {},
"customer": {
"children": {
"email": {
"errors": [
"Please enter your email."
]
}
}
}
"differentBillingAddress": {}
}
}
}