refactor cartAdd EventListener, removing Request references

This commit is contained in:
Manuel Raynaud
2013-08-14 15:10:21 +02:00
parent 61342e06f4
commit ab81b58e94
8 changed files with 126 additions and 59 deletions

View File

@@ -52,29 +52,22 @@ class Cart extends BaseAction implements EventSubscriberInterface
*/ */
public function addArticle(CartEvent $event) public function addArticle(CartEvent $event)
{ {
$request = $event->getRequest();
$message = null;
try {
$cartAdd = $this->getAddCartForm($request);
$form = $this->validateForm($cartAdd); $cart = $event->cart;
$newness = $event->newness;
$append = $event->append;
$quantity = $event->quantity;
$cart = $event->getCart(); $productSaleElementsId = $event->productSaleElementsId;
$newness = $form->get("newness")->getData(); $productId = $event->product;
$append = $form->get("append")->getData();
$quantity = $form->get("quantity")->getData();
$productSaleElementsId = $form->get("product_sale_elements_id")->getData();
$productId = $form->get("product")->getData();
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId); $cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
if ($cartItem === null || $newness) { if ($cartItem === null || $newness) {
$productPrice = ProductPriceQuery::create() $productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId) ->filterByProductSaleElementsId($productSaleElementsId)
->findOne() ->findOne();
;
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice); $this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
} }
@@ -82,19 +75,6 @@ class Cart extends BaseAction implements EventSubscriberInterface
if ($append && $cartItem !== null) { if ($append && $cartItem !== null) {
$this->updateQuantity($cartItem, $quantity); $this->updateQuantity($cartItem, $quantity);
} }
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
$message = "Failed to add this article to your cart, please try again";
} catch (FormValidationException $e) {
$message = $e->getMessage();
}
if ($message) {
// The form has errors, propagate it.
$this->propagateFormError($cartAdd, $message, $event);
}
} }
/** /**

View File

@@ -19,11 +19,11 @@
<default key="_view">connexion</default> <default key="_view">connexion</default>
</route> </route>
<route id="cart.add.process" path="/cart/add" methods="post"> <route id="cart.add.process" path="/cart/add">
<default key="_controller">Thelia\Controller\Front\CartController::changeArticle</default> <default key="_controller">Thelia\Controller\Front\CartController::addArticle</default>
</route> </route>
<route id="cart.change.process" path="/cart/delete" methods="post"> <route id="cart.change.process" path="/cart/delete">
<default key="_controller">Thelia\Controller\Front\CartController::deleteArticle</default> <default key="_controller">Thelia\Controller\Front\CartController::changeArticle</default>
</route> </route>
</routes> </routes>

View File

@@ -32,6 +32,8 @@ use Thelia\Core\Template\ParserContext;
use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\ActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Thelia\Core\Factory\ActionEventFactory; use Thelia\Core\Factory\ActionEventFactory;
use Thelia\Form\BaseForm;
use Thelia\Action\Exception\FormValidationException;
/** /**
* *
@@ -89,7 +91,7 @@ class BaseController extends ContainerAware
/** /**
* Return the event dispatcher, * Return the event dispatcher,
* *
* @return EventDispatcherInterface * @return \Symfony\Component\EventDispatcher\EventDispatcher
*/ */
public function getDispatcher() public function getDispatcher()
{ {
@@ -140,6 +142,32 @@ class BaseController extends ContainerAware
return $request->getSession(); return $request->getSession();
} }
/**
* Validate a BaseForm
*
* @param BaseForm $aBaseForm the form
* @param string $expectedMethod the expected method, POST or GET, or null for any of them
* @throws FormValidationException is the form contains error, or the method is not the right one
* @return \Symfony\Component\Form\Form Form the symfony form object
*/
protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
{
$form = $aBaseForm->getForm();
if ($expectedMethod == null || $aBaseForm->getRequest()->isMethod($expectedMethod)) {
$form->bind($aBaseForm->getRequest());
if ($form->isValid()) {
return $form;
} else {
throw new FormValidationException("Missing or invalid data");
}
} else {
throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod));
}
}
/** /**
* *
* redirect request to specify url * redirect request to specify url

View File

@@ -22,8 +22,12 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Controller\Front; namespace Thelia\Controller\Front;
use Propel\Runtime\Exception\PropelException;
use Thelia\Action\Exception\FormValidationException;
use Thelia\Core\Event\CartEvent; use Thelia\Core\Event\CartEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Form\CartAdd;
class CartController extends BaseFrontController class CartController extends BaseFrontController
{ {
@@ -31,14 +35,39 @@ class CartController extends BaseFrontController
public function addArticle() public function addArticle()
{ {
$cartEvent = $this->getCartEvent(); $request = $this->getRequest();
$this->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); $cartAdd = $this->getAddCartForm($request);
$message = null;
try {
$form = $this->validateForm($cartAdd);
$cartEvent = $this->getCartEvent();
$cartEvent->newness = $form->get("newness")->getData();
$cartEvent->append = $form->get("append")->getData();
$cartEvent->quantity = $form->get("quantity")->getData();
$cartEvent->productSaleElementsId = $form->get("product_sale_elements_id")->getData();
$cartEvent->product = $form->get("product")->getData();
$this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent);
$this->redirectSuccess(); $this->redirectSuccess();
} catch(PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
$message = "Failed to add this article to your cart, please try again";
} catch(FormValidationException $e) {
$message = $e->getMessage();
} }
public function modifyArticle() if ($message) {
$cartAdd->setErrorMessage($e->getMessage());
$this->getParserContext()->setErrorForm($cartAdd);
}
}
public function changeArticle()
{ {
$cartEvent = $this->getCartEvent(); $cartEvent = $this->getCartEvent();
@@ -56,12 +85,40 @@ class CartController extends BaseFrontController
$this->redirectSuccess(); $this->redirectSuccess();
} }
/**
* use Thelia\Cart\CartTrait for searching current cart or create a new one
*
* @return CartEvent
*/
protected function getCartEvent() protected function getCartEvent()
{ {
$request = $this->getRequest(); $cart = $this->getCart($this->getRequest());
$cart = $this->getCart($request);
return new CartEvent($request, $cart); return new CartEvent($cart);
}
/**
* Find the good way to construct the cart form
*
* @param Request $request
* @return CartAdd
*/
private function getAddCartForm(Request $request)
{
if ($request->isMethod("post")) {
$cartAdd = new CartAdd($request);
} else {
$cartAdd = new CartAdd(
$request,
"form",
array(),
array(
'csrf_protection' => false,
)
);
}
return $cartAdd;
} }
} }

View File

@@ -23,21 +23,22 @@
namespace Thelia\Core\Event; namespace Thelia\Core\Event;
use Symfony\Component\EventDispatcher\Event;
use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Request;
use Thelia\Model\Cart; use Thelia\Model\Cart;
class CartEvent extends ActionEvent class CartEvent extends Event
{ {
protected $cart; public $cart;
public $quantity;
public $append;
public $newness;
public $productSaleElementsId;
public $product;
public $cartItem;
public function __construct(Request $request, Cart $cart) public function __construct(Cart $cart)
{ {
parent::__construct($request);
$this->cart = $cart; $this->cart = $cart;
} }
public function getCart()
{
return $this->cart;
}
} }

View File

@@ -116,7 +116,7 @@ final class TheliaEvents
/** /**
* sent when a new item is added to current cart * sent when a new item is added to current cart
*/ */
const AFTER_CARTADDITEM = "cart.addItem"; const AFTER_CARTADDITEM = "cart.after.addItem";
/** /**
* sent when a cart item is modify * sent when a cart item is modify

View File

@@ -171,6 +171,7 @@ abstract class BaseForm
*/ */
public function setErrorMessage($message) public function setErrorMessage($message)
{ {
$this->setError(true);
$this->error_message = $message; $this->error_message = $message;
} }

View File

@@ -23,7 +23,7 @@ class CartItem extends BaseCartItem
if ($this->dispatcher) { if ($this->dispatcher) {
$cartEvent = new CartEvent($this->getCart()); $cartEvent = new CartEvent($this->getCart());
$this->dispatcher->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTADDITEM, $cartEvent);
} }
} }
@@ -32,7 +32,7 @@ class CartItem extends BaseCartItem
if ($this->dispatcher) { if ($this->dispatcher) {
$cartEvent = new CartEvent($this->getCart()); $cartEvent = new CartEvent($this->getCart());
$this->dispatcher->dispatch(TheliaEvents::CART_MODIFYITEM, $cartEvent); $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTCHANGEITEM, $cartEvent);
} }
} }