refactor cartAdd EventListener, removing Request references
This commit is contained in:
@@ -52,29 +52,22 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
*/
|
||||
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();
|
||||
$newness = $form->get("newness")->getData();
|
||||
$append = $form->get("append")->getData();
|
||||
$quantity = $form->get("quantity")->getData();
|
||||
|
||||
$productSaleElementsId = $form->get("product_sale_elements_id")->getData();
|
||||
$productId = $form->get("product")->getData();
|
||||
$productSaleElementsId = $event->productSaleElementsId;
|
||||
$productId = $event->product;
|
||||
|
||||
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
|
||||
|
||||
if ($cartItem === null || $newness) {
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->findOne()
|
||||
;
|
||||
->findOne();
|
||||
|
||||
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
|
||||
}
|
||||
@@ -82,19 +75,6 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
if ($append && $cartItem !== null) {
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,11 +19,11 @@
|
||||
<default key="_view">connexion</default>
|
||||
</route>
|
||||
|
||||
<route id="cart.add.process" path="/cart/add" methods="post">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::changeArticle</default>
|
||||
<route id="cart.add.process" path="/cart/add">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::addArticle</default>
|
||||
</route>
|
||||
|
||||
<route id="cart.change.process" path="/cart/delete" methods="post">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::deleteArticle</default>
|
||||
<route id="cart.change.process" path="/cart/delete">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::changeArticle</default>
|
||||
</route>
|
||||
</routes>
|
||||
|
||||
@@ -32,6 +32,8 @@ use Thelia\Core\Template\ParserContext;
|
||||
use Thelia\Core\Event\ActionEvent;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
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 EventDispatcherInterface
|
||||
* @return \Symfony\Component\EventDispatcher\EventDispatcher
|
||||
*/
|
||||
public function getDispatcher()
|
||||
{
|
||||
@@ -140,6 +142,32 @@ class BaseController extends ContainerAware
|
||||
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
|
||||
|
||||
@@ -22,8 +22,12 @@
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Controller\Front;
|
||||
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
use Thelia\Action\Exception\FormValidationException;
|
||||
use Thelia\Core\Event\CartEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Thelia\Form\CartAdd;
|
||||
|
||||
class CartController extends BaseFrontController
|
||||
{
|
||||
@@ -31,14 +35,39 @@ class CartController extends BaseFrontController
|
||||
|
||||
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();
|
||||
|
||||
} 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();
|
||||
|
||||
@@ -56,12 +85,40 @@ class CartController extends BaseFrontController
|
||||
$this->redirectSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* use Thelia\Cart\CartTrait for searching current cart or create a new one
|
||||
*
|
||||
* @return CartEvent
|
||||
*/
|
||||
protected function getCartEvent()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$cart = $this->getCart($request);
|
||||
$cart = $this->getCart($this->getRequest());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,21 +23,22 @@
|
||||
|
||||
namespace Thelia\Core\Event;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
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;
|
||||
}
|
||||
|
||||
public function getCart()
|
||||
{
|
||||
return $this->cart;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ final class TheliaEvents
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -171,6 +171,7 @@ abstract class BaseForm
|
||||
*/
|
||||
public function setErrorMessage($message)
|
||||
{
|
||||
$this->setError(true);
|
||||
$this->error_message = $message;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class CartItem extends BaseCartItem
|
||||
if ($this->dispatcher) {
|
||||
$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) {
|
||||
$cartEvent = new CartEvent($this->getCart());
|
||||
|
||||
$this->dispatcher->dispatch(TheliaEvents::CART_MODIFYITEM, $cartEvent);
|
||||
$this->dispatcher->dispatch(TheliaEvents::AFTER_CARTCHANGEITEM, $cartEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user