mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
Use latest resource bundle, introduce cart item resource controller
This commit is contained in:
parent
c4e32f998a
commit
38898f3d77
9 changed files with 187 additions and 119 deletions
|
|
@ -16,7 +16,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cart frontend controller.
|
* Cart controller.
|
||||||
*
|
*
|
||||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||||
*/
|
*/
|
||||||
|
|
@ -32,7 +32,7 @@ class CartController extends ResourceController
|
||||||
public function showAction(Request $request)
|
public function showAction(Request $request)
|
||||||
{
|
{
|
||||||
$cart = $this->getCurrentCart();
|
$cart = $this->getCurrentCart();
|
||||||
$form = $this->createForm($this->getResourceFormType(), $cart);
|
$form = $this->createForm('sylius_cart', $cart);
|
||||||
|
|
||||||
return $this->renderResponse('show.html', array(
|
return $this->renderResponse('show.html', array(
|
||||||
'cart' => $cart,
|
'cart' => $cart,
|
||||||
|
|
@ -40,74 +40,6 @@ class CartController extends ResourceController
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds item to cart.
|
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function addItemAction(Request $request)
|
|
||||||
{
|
|
||||||
$cart = $this->getCurrentCart();
|
|
||||||
$emptyItem = $this->getCartItemManager()->create();
|
|
||||||
|
|
||||||
$item = $this->getResolver()->resolve($emptyItem, $request);
|
|
||||||
|
|
||||||
if (!$item) {
|
|
||||||
$this->setFlash('error', 'sylius_cart.flashes.add.error');
|
|
||||||
|
|
||||||
return $this->redirectToCart();
|
|
||||||
}
|
|
||||||
|
|
||||||
$cartOperator = $this->getOperator();
|
|
||||||
|
|
||||||
$cartOperator
|
|
||||||
->addItem($cart, $item)
|
|
||||||
->refresh($cart)
|
|
||||||
;
|
|
||||||
|
|
||||||
$errors = $this->get('validator')->validate($cart);
|
|
||||||
|
|
||||||
if (0 === count($errors)) {
|
|
||||||
$this->setFlash('success', 'sylius_cart.flashes.add.success');
|
|
||||||
$cartOperator->save($cart);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->redirectToCart();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes item from cart.
|
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
* @param mixed $id
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function removeItemAction(Request $request, $id)
|
|
||||||
{
|
|
||||||
$cart = $this->getCurrentCart();
|
|
||||||
$item = $this->getCartItemRepository()->get(array('id' => $id));
|
|
||||||
|
|
||||||
if (!$item || false === $cart->hasItem($item)) {
|
|
||||||
$this->setFlash('error', 'sylius_cart.flashes.remove.error');
|
|
||||||
|
|
||||||
return $this->redirectToCart();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this
|
|
||||||
->getOperator()
|
|
||||||
->removeItem($cart, $item)
|
|
||||||
->refresh($cart)
|
|
||||||
->save($cart)
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->setFlash('success', 'sylius_cart.flashes.remove.success');
|
|
||||||
|
|
||||||
return $this->redirectToCart();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves cart.
|
* Saves cart.
|
||||||
*
|
*
|
||||||
|
|
@ -120,7 +52,7 @@ class CartController extends ResourceController
|
||||||
$cart = $this->getCurrentCart();
|
$cart = $this->getCurrentCart();
|
||||||
|
|
||||||
$form = $this
|
$form = $this
|
||||||
->createForm($this->getResourceFormType(), $cart)
|
->createForm('sylius_cart', $cart)
|
||||||
->bind($request)
|
->bind($request)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
@ -190,26 +122,6 @@ class CartController extends ResourceController
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get cart item manager.
|
|
||||||
*
|
|
||||||
* @return ResourceManagerInterface
|
|
||||||
*/
|
|
||||||
protected function getCartItemManager()
|
|
||||||
{
|
|
||||||
return $this->get('sylius_cart.manager.item');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get cart item repository.
|
|
||||||
*
|
|
||||||
* @return ResourceRepositoryInterface
|
|
||||||
*/
|
|
||||||
protected function getCartItemRepository()
|
|
||||||
{
|
|
||||||
return $this->get('sylius_cart.repository.item');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get cart provider.
|
* Get cart provider.
|
||||||
*
|
*
|
||||||
|
|
@ -229,23 +141,4 @@ class CartController extends ResourceController
|
||||||
{
|
{
|
||||||
return $this->get('sylius_cart.operator');
|
return $this->get('sylius_cart.operator');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get cart item resolver.
|
|
||||||
*
|
|
||||||
* @return CartResolverInterface
|
|
||||||
*/
|
|
||||||
protected function getResolver()
|
|
||||||
{
|
|
||||||
return $this->get('sylius_cart.resolver');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
protected function getResourceFormType()
|
|
||||||
{
|
|
||||||
return 'sylius_cart';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
165
src/Sylius/Bundle/CartBundle/Controller/CartItemController.php
Normal file
165
src/Sylius/Bundle/CartBundle/Controller/CartItemController.php
Normal file
|
|
@ -0,0 +1,165 @@
|
||||||
|
<?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\CartBundle\Controller;
|
||||||
|
|
||||||
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cart item controller.
|
||||||
|
*
|
||||||
|
* It manages the cart item resource, but also it has
|
||||||
|
* two handy methods for easy adding and removing items
|
||||||
|
* using the services, an operator and resolver.
|
||||||
|
*
|
||||||
|
* The operator performs basic cart operations,
|
||||||
|
* adding, removing items, saving and clearing the cart.
|
||||||
|
*
|
||||||
|
* The resolver is used to create a new cart item, based
|
||||||
|
* on the data from current request.
|
||||||
|
*
|
||||||
|
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||||
|
*/
|
||||||
|
class CartItemController extends ResourceController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Adds item to cart.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function addAction(Request $request)
|
||||||
|
{
|
||||||
|
$cart = $this->getCurrentCart();
|
||||||
|
$emptyItem = $this->create();
|
||||||
|
|
||||||
|
$item = $this->getResolver()->resolve($emptyItem, $request);
|
||||||
|
|
||||||
|
if (!$item) {
|
||||||
|
$this->setFlash('error', 'sylius_cart.flashes.add.error');
|
||||||
|
|
||||||
|
return $this->redirectToCart();
|
||||||
|
}
|
||||||
|
|
||||||
|
$cartOperator = $this->getOperator();
|
||||||
|
|
||||||
|
$cartOperator
|
||||||
|
->addItem($cart, $item)
|
||||||
|
->refresh($cart)
|
||||||
|
;
|
||||||
|
|
||||||
|
$errors = $this->get('validator')->validate($cart);
|
||||||
|
|
||||||
|
if (0 === count($errors)) {
|
||||||
|
$this->setFlash('success', 'sylius_cart.flashes.add.success');
|
||||||
|
$cartOperator->save($cart);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->redirectToCart();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes item from cart.
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @param mixed $id
|
||||||
|
*
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function removeAction(Request $request, $id)
|
||||||
|
{
|
||||||
|
$cart = $this->getCurrentCart();
|
||||||
|
$item = $this->getRepository()->find($id);
|
||||||
|
|
||||||
|
if (!$item || false === $cart->hasItem($item)) {
|
||||||
|
$this->setFlash('error', 'sylius_cart.flashes.remove.error');
|
||||||
|
|
||||||
|
return $this->redirectToCart();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this
|
||||||
|
->getOperator()
|
||||||
|
->removeItem($cart, $item)
|
||||||
|
->refresh($cart)
|
||||||
|
->save($cart)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->setFlash('success', 'sylius_cart.flashes.remove.success');
|
||||||
|
|
||||||
|
return $this->redirectToCart();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redirect to show cart action.
|
||||||
|
*
|
||||||
|
* @return RedirectResponse
|
||||||
|
*/
|
||||||
|
protected function redirectToCart()
|
||||||
|
{
|
||||||
|
return $this->redirect($this->generateUrl($this->getCartRoute()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cart show action route.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getCartRoute()
|
||||||
|
{
|
||||||
|
return 'sylius_cart_show';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current cart.
|
||||||
|
*
|
||||||
|
* @return CartInterface
|
||||||
|
*/
|
||||||
|
protected function getCurrentCart()
|
||||||
|
{
|
||||||
|
return $this
|
||||||
|
->getProvider()
|
||||||
|
->getCart()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get cart provider.
|
||||||
|
*
|
||||||
|
* @return CartProviderInterface
|
||||||
|
*/
|
||||||
|
protected function getProvider()
|
||||||
|
{
|
||||||
|
return $this->get('sylius_cart.provider');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get cart operator.
|
||||||
|
*
|
||||||
|
* @return CartOperatorInterface
|
||||||
|
*/
|
||||||
|
protected function getOperator()
|
||||||
|
{
|
||||||
|
return $this->get('sylius_cart.operator');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get cart item resolver.
|
||||||
|
*
|
||||||
|
* @return CartResolverInterface
|
||||||
|
*/
|
||||||
|
protected function getResolver()
|
||||||
|
{
|
||||||
|
return $this->get('sylius_cart.resolver');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -74,6 +74,7 @@ class Configuration implements ConfigurationInterface
|
||||||
->addDefaultsIfNotSet()
|
->addDefaultsIfNotSet()
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('cart')->defaultValue('Sylius\\Bundle\\CartBundle\\Controller\\CartController')->end()
|
->scalarNode('cart')->defaultValue('Sylius\\Bundle\\CartBundle\\Controller\\CartController')->end()
|
||||||
|
->scalarNode('item')->defaultValue('Sylius\\Bundle\\CartBundle\\Controller\\CartItemController')->end()
|
||||||
->end()
|
->end()
|
||||||
->end()
|
->end()
|
||||||
->arrayNode('form')
|
->arrayNode('form')
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ class SyliusCartExtension extends Extension
|
||||||
$container->setParameter('sylius_cart.model.item.class', $config['classes']['model']['item']);
|
$container->setParameter('sylius_cart.model.item.class', $config['classes']['model']['item']);
|
||||||
|
|
||||||
$container->setParameter('sylius_cart.controller.cart.class', $config['classes']['controller']['cart']);
|
$container->setParameter('sylius_cart.controller.cart.class', $config['classes']['controller']['cart']);
|
||||||
|
$container->setParameter('sylius_cart.controller.item.class', $config['classes']['controller']['item']);
|
||||||
|
|
||||||
$container->setParameter('sylius_cart.form.type.cart.class', $config['classes']['form']['type']['cart']);
|
$container->setParameter('sylius_cart.form.type.cart.class', $config['classes']['form']['type']['cart']);
|
||||||
$container->setParameter('sylius_cart.form.type.item.class', $config['classes']['form']['type']['item']);
|
$container->setParameter('sylius_cart.form.type.item.class', $config['classes']['form']['type']['item']);
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,6 @@ class CartProvider implements CartProviderInterface
|
||||||
*/
|
*/
|
||||||
protected function getCartByIdentifier($identifier)
|
protected function getCartByIdentifier($identifier)
|
||||||
{
|
{
|
||||||
return $this->repository->get(array('id' => $identifier));
|
return $this->repository->find($identifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,10 @@
|
||||||
http://symfony.com/schema/dic/services/services-1.0.xsd">
|
http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||||
|
|
||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="sylius_cart.manager.cart.class">Sylius\Bundle\ResourceBundle\Manager\Doctrine\DoctrineResourceManager</parameter>
|
<parameter key="sylius_cart.manager.cart.class">Sylius\Bundle\ResourceBundle\Doctrine\ResourceManager</parameter>
|
||||||
<parameter key="sylius_cart.repository.cart.class">Sylius\Bundle\ResourceBundle\Repository\Doctrine\ORM\ResourceRepository</parameter>
|
<parameter key="sylius_cart.repository.cart.class">Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepository</parameter>
|
||||||
<parameter key="sylius_cart.manager.item.class">Sylius\Bundle\ResourceBundle\Manager\Doctrine\DoctrineResourceManager</parameter>
|
<parameter key="sylius_cart.manager.item.class">Sylius\Bundle\ResourceBundle\Doctrine\ResourceManager</parameter>
|
||||||
<parameter key="sylius_cart.repository.item.class">Sylius\Bundle\ResourceBundle\Repository\Doctrine\ORM\ResourceRepository</parameter>
|
<parameter key="sylius_cart.repository.item.class">Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepository</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,14 @@
|
||||||
<argument type="service" id="service_container" />
|
<argument type="service" id="service_container" />
|
||||||
</call>
|
</call>
|
||||||
</service>
|
</service>
|
||||||
|
<service id="sylius_cart.controller.item" class="%sylius_cart.controller.item.class%">
|
||||||
|
<argument>sylius_cart</argument>
|
||||||
|
<argument>item</argument>
|
||||||
|
<argument>SyliusCartBundle:CartItem</argument>
|
||||||
|
<call method="setContainer">
|
||||||
|
<argument type="service" id="service_container" />
|
||||||
|
</call>
|
||||||
|
</service>
|
||||||
|
|
||||||
<service id="sylius_cart.form.type.cart" class="%sylius_cart.form.type.cart.class%">
|
<service id="sylius_cart.form.type.cart" class="%sylius_cart.form.type.cart.class%">
|
||||||
<argument>%sylius_cart.model.cart.class%</argument>
|
<argument>%sylius_cart.model.cart.class%</argument>
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ sylius_cart_clear:
|
||||||
|
|
||||||
sylius_cart_item_add:
|
sylius_cart_item_add:
|
||||||
pattern: /item/add
|
pattern: /item/add
|
||||||
defaults: { _controller: sylius_cart.controller.cart:addItemAction }
|
defaults: { _controller: sylius_cart.controller.item:addAction }
|
||||||
|
|
||||||
sylius_cart_item_remove:
|
sylius_cart_item_remove:
|
||||||
pattern: /item/{id}/remove
|
pattern: /item/{id}/remove
|
||||||
defaults: { _controller: sylius_cart.controller.cart:removeItemAction }
|
defaults: { _controller: sylius_cart.controller.item:removeAction }
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class CartProvider extends ObjectBehavior
|
||||||
function it_should_look_for_cart_by_identifier_if_any($storage, $repository, $cart)
|
function it_should_look_for_cart_by_identifier_if_any($storage, $repository, $cart)
|
||||||
{
|
{
|
||||||
$storage->getCurrentCartIdentifier()->willReturn(3);
|
$storage->getCurrentCartIdentifier()->willReturn(3);
|
||||||
$repository->get(array('id' => 3))->shouldBeCalled()->willReturn($cart);
|
$repository->find(3)->shouldBeCalled()->willReturn($cart);
|
||||||
|
|
||||||
$this->getCart()->shouldReturn($cart);
|
$this->getCart()->shouldReturn($cart);
|
||||||
}
|
}
|
||||||
|
|
@ -71,7 +71,7 @@ class CartProvider extends ObjectBehavior
|
||||||
function it_should_create_new_cart_if_identifier_is_wrong($storage, $manager, $repository, $cart)
|
function it_should_create_new_cart_if_identifier_is_wrong($storage, $manager, $repository, $cart)
|
||||||
{
|
{
|
||||||
$storage->getCurrentCartIdentifier()->willReturn(7);
|
$storage->getCurrentCartIdentifier()->willReturn(7);
|
||||||
$repository->get(array('id' => 7))->shouldBeCalled()->willReturn(null);
|
$repository->find(7)->shouldBeCalled()->willReturn(null);
|
||||||
$manager->create()->willReturn($cart);
|
$manager->create()->willReturn($cart);
|
||||||
|
|
||||||
$this->getCart()->shouldReturn($cart);
|
$this->getCart()->shouldReturn($cart);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue