Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderBundle] properly implement AddMultipleToCart #1154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/CoreShop/Bundle/OrderBundle/Controller/CartEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CoreShop\Bundle\OrderBundle\Controller;

use CoreShop\Bundle\OrderBundle\DTO\AddMultipleToCartInterface;
use CoreShop\Bundle\OrderBundle\DTO\AddToCartInterface;
use CoreShop\Bundle\OrderBundle\Form\Type\AddMultipleToCartType;
use CoreShop\Bundle\OrderBundle\Form\Type\EditCartType;
Expand Down Expand Up @@ -96,16 +97,18 @@ public function addItemsAction(Request $request)
$commands[] = $this->createAddToCart($cart, $cartItem);
}

$form = $this->get('form.factory')->createNamed('', AddMultipleToCartType::class, ['items' => $commands]);
$addMultipleAddToCarts = $this->createMultipleAddToCart($commands);

$form = $this->get('form.factory')->createNamed('', AddMultipleToCartType::class, $addMultipleAddToCarts);

if ($request->isMethod('POST')) {
if ($form->handleRequest($request)->isValid()) {
$addsToCart = $form->getData();

/**
* @var AddToCartInterface $addToCart
* @var AddMultipleToCartInterface $addsToCart
*/
foreach ($addsToCart['items'] as $addToCart) {
$addsToCart = $form->getData();

foreach ($addsToCart->getItems() as $addToCart) {
$this->getCartModifier()->addToList(
$addToCart->getCart(),
$addToCart->getCartItem()
Expand Down Expand Up @@ -173,6 +176,15 @@ public function removeItemAction(Request $request)
return $this->viewHandler->handle(['success' => true]);
}

/**
* @param array $addToCarts
* @return AddMultipleToCartInterface
*/
protected function createMultipleAddToCart(array $addToCarts)
{
return $this->get('coreshop.factory.add_multiple_to_cart')->createWithMultipleAddToCarts($addToCarts);
}

/**
* @param CartInterface $cart
* @param CartItemInterface $item
Expand Down
53 changes: 53 additions & 0 deletions src/CoreShop/Bundle/OrderBundle/DTO/AddMultipleToCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\OrderBundle\DTO;

class AddMultipleToCart implements AddMultipleToCartInterface
{
/**
* @var AddToCartInterface[]
*/
private $items;

/**
* @param AddToCartInterface[] $items
*/
public function __construct(array $items)
{
$this->items = $items;
}

/**
* @return AddToCartInterface[]
*/
public function getItems()
{
return $this->items;
}

/**
* @param AddToCartInterface[] $items
*/
public function setItems(array $items)
{
$this->items = $items;
}

/**
* @param AddToCartInterface $addToCart
*/
public function addItem(AddToCartInterface $addToCart)
{
$this->items[] = $addToCart;
}
}
24 changes: 24 additions & 0 deletions src/CoreShop/Bundle/OrderBundle/DTO/AddMultipleToCartInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\OrderBundle\DTO;

use CoreShop\Component\Order\Model\CartInterface;
use CoreShop\Component\Order\Model\CartItemInterface;

interface AddMultipleToCartInterface
{
/**
* @return AddToCartInterface[]
*/
public function getItems();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\OrderBundle\Factory;

use CoreShop\Bundle\OrderBundle\DTO\AddMultipleToCartInterface;

class AddMultipleToCartFactory implements AddMultipleToCartFactoryInterface
{
/**
* @var string
*/
protected $addMultipleToCartClass;

/**
* @param string $addMultipleToCartClass
*/
public function __construct($addMultipleToCartClass)
{
$this->addMultipleToCartClass = $addMultipleToCartClass;
}

/**
* {@inheritdoc}
*/
public function createWithMultipleAddToCarts(array $addToCarts)
{
$class = new $this->addMultipleToCartClass($addToCarts);

if (!in_array(AddMultipleToCartInterface::class, class_implements($class), true)) {
throw new \InvalidArgumentException(
sprintf('%s needs to implement "%s".', get_class($class), AddMultipleToCartInterface::class)
);
}

return $class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\OrderBundle\Factory;

use CoreShop\Bundle\OrderBundle\DTO\AddMultipleToCartInterface;
use CoreShop\Bundle\OrderBundle\DTO\AddToCartInterface;
use CoreShop\Component\Order\Model\CartInterface;
use CoreShop\Component\Order\Model\CartItemInterface;

interface AddMultipleToCartFactoryInterface
{
/**
* @param AddToCartInterface[] $addToCarts
*
* @return AddMultipleToCartInterface
*/
public function createWithMultipleAddToCarts(array $addToCarts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

namespace CoreShop\Bundle\OrderBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use CoreShop\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Valid;

final class AddMultipleToCartType extends AbstractType
final class AddMultipleToCartType extends AbstractResourceType
{
/**
* {@inheritdoc}
Expand All @@ -27,6 +28,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add('items', CollectionType::class, [
'entry_type' => AddToCartType::class,
'allow_add' => true,
'constraints' => [
new Valid()
]
]);
}

Expand Down
5 changes: 4 additions & 1 deletion src/CoreShop/Bundle/OrderBundle/Form/Type/AddToCartType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CoreShop\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Valid;

final class AddToCartType extends AbstractResourceType
{
Expand All @@ -23,7 +24,9 @@ final class AddToCartType extends AbstractResourceType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('cartItem', CartItemType::class);
$builder->add('cartItem', CartItemType::class, [
'constraints' => new Valid()
]);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/CoreShop/Bundle/OrderBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
coreshop.dto.add_to_cart.class: 'CoreShop\Bundle\OrderBundle\DTO\AddToCart'
coreshop.dto.add_multiple_to_cart.class: 'CoreShop\Bundle\OrderBundle\DTO\AddMultipleToCart'

imports:
- { resource: "services/order.yml" }
Expand Down Expand Up @@ -104,3 +105,8 @@ services:
CoreShop\Bundle\OrderBundle\Factory\AddToCartFactory:
arguments:
- '%coreshop.dto.add_to_cart.class%'

coreshop.factory.add_multiple_to_cart: '@CoreShop\Bundle\OrderBundle\Factory\AddMultipleToCartFactory'
CoreShop\Bundle\OrderBundle\Factory\AddMultipleToCartFactory:
arguments:
- '%coreshop.dto.add_multiple_to_cart.class%'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
coreshop.form.type.add_to_cart.validation_groups: [coreshop]
coreshop.form.type.edit_cart.validation_groups: [coreshop]
coreshop.form.type.add_multiple_to_cart.validation_groups: [coreshop]

services:
CoreShop\Bundle\OrderBundle\Form\Type\AddToCartType:
Expand All @@ -10,6 +11,13 @@ services:
tags:
- { name: form.type }

CoreShop\Bundle\OrderBundle\Form\Type\AddMultipleToCartType:
arguments:
- '%coreshop.dto.add_multiple_to_cart.class%'
- '%coreshop.form.type.add_multiple_to_cart.validation_groups%'
tags:
- { name: form.type }

coreshop.form.type.edit_cart:
class: CoreShop\Bundle\OrderBundle\Form\Type\EditCartType
arguments:
Expand Down