Skip to content

Commit

Permalink
[API] Configure cart and related items serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Feb 2, 2017
1 parent a2fdee1 commit 000d676
Show file tree
Hide file tree
Showing 51 changed files with 777 additions and 403 deletions.
21 changes: 16 additions & 5 deletions src/Sylius/Bundle/ApiBundle/EventListener/AddToCartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Sylius\Bundle\ApiBundle\EventListener;

use Sylius\Component\Core\Model\OrderItem;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
Expand All @@ -26,22 +27,32 @@ final class AddToCartListener
*/
private $orderProcessor;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @param OrderProcessorInterface $orderProcessor
* @param ObjectManager $objectManager
*/
public function __construct(OrderProcessorInterface $orderProcessor)
public function __construct(OrderProcessorInterface $orderProcessor, ObjectManager $objectManager)
{
$this->orderProcessor = $orderProcessor;
$this->objectManager = $objectManager;
}

/**
* @param GenericEvent $event
*/
public function cartItemResolver(GenericEvent $event)
public function recalculateOrder(GenericEvent $event)
{
$item = $event->getSubject();
Assert::isInstanceOf($item, OrderItem::class);
Assert::isInstanceOf($item, OrderItemInterface::class);
$order = $item->getOrder();

$this->orderProcessor->process($order);

$this->orderProcessor->process($item->getOrder());
$this->objectManager->persist($order);
}
}
7 changes: 3 additions & 4 deletions src/Sylius/Bundle/ApiBundle/Form/Type/OrderItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\NotBlank;
use Webmozart\Assert\Assert;

/**
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.org>
Expand Down Expand Up @@ -57,9 +58,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$orderItem = $event->getData();

if (null === $orderItem) {
throw new UnexpectedTypeException($orderItem, OrderItemInterface::class);
}
Assert::notNull($orderItem);

if (null !== $orderItem->getId()) {
$form = $event->getForm();
Expand All @@ -82,6 +81,6 @@ public function getParent()
*/
public function getBlockPrefix()
{
return 'sylius_api_cart_item';
return 'sylius_api_order_item';
}
}
6 changes: 5 additions & 1 deletion src/Sylius/Bundle/ApiBundle/Form/Type/OrderType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public function __construct($dataClass, array $validationGroups = [], Repository
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customer', CustomerChoiceType::class)
->add('customer', CustomerChoiceType::class, [
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
],
])
->add('localeCode', LocaleChoiceType::class, [
'constraints' => [
new NotBlank(['groups' => ['sylius']]),
Expand Down
62 changes: 11 additions & 51 deletions src/Sylius/Bundle/ApiBundle/Resources/config/grids/cart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,47 @@ sylius_grid:
number: desc
fields:
channel:
type: twig
label: sylius.ui.channel
type: string
path: channel.code
sortable: channel.code
options:
template: "@SyliusAdmin/Order/Grid/Field/channel.html.twig"
number:
type: twig
label: sylius.ui.number
type: string
path: .
sortable: ~
options:
template: "@SyliusAdmin/Order/Grid/Field/number.html.twig"
date:
type: datetime
label: sylius.ui.date
path: checkoutCompletedAt
sortable: checkoutCompletedAt
options:
format: d-m-Y H:i:s
customer:
type: twig
label: sylius.ui.customer
sortable: customer.lastName
type: string
sortable: customer.email
path: customer.email
options:
template: "@SyliusAdmin/Order/Grid/Field/customer.html.twig"
state:
type: twig
label: sylius.ui.state
sortable: ~
options:
template: "@SyliusUi/Grid/Field/state.html.twig"
vars:
labels: "@SyliusAdmin/Order/Label/State"
paymentState:
type: twig
label: sylius.ui.payment_state
sortable: ~
options:
template: "@SyliusUi/Grid/Field/state.html.twig"
vars:
labels: "@SyliusAdmin/Order/Label/PaymentState"
shippingState:
type: twig
label: sylius.ui.shipping_state
sortable: ~
options:
template: "@SyliusUi/Grid/Field/state.html.twig"
vars:
labels: "@SyliusAdmin/Order/Label/ShippingState"
total:
type: twig
label: sylius.ui.total
path: .
type: integer
sortable: total
options:
template: "@SyliusAdmin/Order/Grid/Field/total.html.twig"
currencyCode:
type: string
label: sylius.ui.currency
sortable: ~
filters:
number:
type: string
label: sylius.ui.number
customer:
type: string
label: sylius.ui.customer
options:
fields: [customer.email, customer.firstName, customer.lastName]
fields: [customer.email]
date:
type: date
label: sylius.ui.date
options:
field: checkoutCompletedAt
channel:
type: entity
label: sylius.ui.channel
form_options:
class: "%sylius.model.channel.class%"
type: string
options:
fields: [channel.code]
total:
type: money
label: sylius.ui.total
options:
currency_field: currencyCode
3 changes: 3 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/routing/cart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sylius_api_cart_index:
paginate: $limit
sortable: true
grid: sylius_api_cart
serialization_groups: [Default]

sylius_api_cart_create:
path: /
Expand All @@ -20,6 +21,7 @@ sylius_api_cart_create:
_sylius:
serialization_version: $version
form: Sylius\Bundle\ApiBundle\Form\Type\OrderType
serialization_groups: [DetailedCart]

sylius_api_cart_delete:
path: /{id}
Expand All @@ -44,3 +46,4 @@ sylius_api_cart_show:
repository:
method: findCartById
arguments: [$id]
serialization_groups: [DetailedCart]
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sylius_api_order_item_create:
method: createForCart
arguments:
- "expr:service('sylius.repository.order').findCartById($cartId)"
serialization_groups: [DetailedCart]

sylius_api_order_item_update:
path: /{id}
Expand All @@ -22,6 +23,9 @@ sylius_api_order_item_update:
_sylius:
serialization_version: $version
form: Sylius\Bundle\ApiBundle\Form\Type\OrderItemType
repository:
method: findOneByIdAndCartId
arguments: [$id, $cartId]

sylius_api_order_item_delete:
path: /{id}
Expand All @@ -31,3 +35,6 @@ sylius_api_order_item_delete:
_sylius:
serialization_version: $version
csrf_protection: false
repository:
method: findOneByIdAndCartId
arguments: [$id, $cartId]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sylius_api_order:
resource: |
alias: sylius.order
section: api
only: [show, delete]
only: [show]
grid: sylius_admin_order
serialization_version: $version
type: sylius.resource_api
Expand Down
4 changes: 3 additions & 1 deletion src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

<service id="sylius.listener.api.add_to_cart" class="Sylius\Bundle\ApiBundle\EventListener\AddToCartListener">
<argument type="service" id="sylius.order_processing.order_processor.composite" />
<tag name="kernel.event_listener" event="sylius.order_item.post_create" method="cartItemResolver" />
<argument type="service" id="sylius.manager.order" />
<tag name="kernel.event_listener" event="sylius.order_item.pre_create" method="recalculateOrder" />
<tag name="kernel.event_listener" event="sylius.order_item.pre_update" method="recalculateOrder" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<tag name="form.type" />
</service>

<service id="sylius.form.extension.type.api_order_item" class="Sylius\Bundle\ApiBundle\Form\Type\OrderItemType">
<service id="sylius.form.type.api_order_item" class="Sylius\Bundle\ApiBundle\Form\Type\OrderItemType">
<argument type="service" id="sylius.repository.product_variant" />
<tag name="form.type" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\ApiBundle\EventListener;

use Doctrine\Common\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\EventListener\AddToCartListener;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

final class AddToCartListenerSpec extends ObjectBehavior
{
function let(OrderProcessorInterface $orderProcessor, ObjectManager $manager)
{
$this->beConstructedWith($orderProcessor, $manager);
}

function it_is_initializable()
{
$this->shouldHaveType(AddToCartListener::class);
}

function it_recalculates_cart(OrderProcessorInterface $orderProcessor, ObjectManager $manager, GenericEvent $event, OrderItemInterface $orderItem, OrderInterface $order)
{
$event->getSubject()->willReturn($orderItem);
$orderItem->getOrder()->willReturn($order);

$orderProcessor->process($order)->shouldBeCalled();
$manager->persist($order)->shouldBeCalled();

$this->recalculateOrder($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,45 @@ Sylius\Component\Channel\Model\Channel:
expose: true
type: integer
xml_attribute: true
groups: [Detailed]
code:
expose: true
type: string
groups: [Default, Detailed, DetailedCart]
name:
expose: true
type: string
groups: [Detailed]
hostname:
expose: true
type: string
groups: [Detailed]
enabled:
expose: true
type: boolean
groups: [Detailed]
description:
expose: true
type: string
groups: [Detailed]
color:
expose: true
type: string
groups: [Detailed]
createdAt:
expose: true
type: DateTime
groups: [Detailed]
updatedAt:
expose: true
type: DateTime
groups: [Detailed]
relations:
- rel: self
href:
route: sylius_api_channel_show
parameters:
id: expr(object.getId())
version: 1
route: sylius_api_channel_show
parameters:
id: expr(object.getId())
version: 1
exclusion:
groups: [Default, Detailed, DetailedCart]
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Sylius\Component\Core\Model\Channel:
taxCalculationStrategy:
expose: true
type: string
groups: [Detailed]
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Sylius\Component\Core\Model\Customer:
properties:
user:
expose: true
groups: [Default, Detailed]
groups: [Default, Detailed, DetailedCart]

0 comments on commit 000d676

Please sign in to comment.