From cf5ccdc7ae01751f66616efb62bd79002d252a59 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 13 Aug 2013 10:09:12 +0200 Subject: [PATCH] refactor modify Article action --- core/lib/Thelia/Action/Cart.php | 172 +++++++++--------- .../Controller/Front/CartController.php | 19 +- core/lib/Thelia/Core/Event/ActionEvent.php | 17 +- core/lib/Thelia/Core/Event/CartEvent.php | 4 +- core/lib/Thelia/Core/Event/TheliaEvents.php | 14 +- 5 files changed, 118 insertions(+), 108 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 3b4b32a78..39f9c3cda 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -99,6 +99,93 @@ class Cart extends BaseAction implements EventSubscriberInterface } + + /** + * + * Delete specify article present into cart + * + * @param \Thelia\Core\Event\CartEvent $event + */ + public function deleteArticle(CartEvent $event) + { + $request = $event->getRequest(); + + if (null !== $cartItemId = $request->get('cartItem')) { + $cart = $event->getCart(); + try { + $cartItem = CartItemQuery::create() + ->filterByCartId($cart->getId()) + ->filterById($cartItemId) + ->delete(); + } catch (PropelException $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); + } + + } + } + + /** + * + * Modify article's quantity + * + * don't use Form here just test the Request. + * + * @param \Thelia\Core\Event\CartEvent $event + */ + public function modifyArticle(CartEvent $event) + { + $request = $event->getRequest(); + + if (null !== $cartItemId = $request->get("cartItem") && null !== $quantity = $request->get("quantity")) { + + try { + $cart = $event->getCart($request); + + $cartItem = CartItemQuery::create() + ->filterByCartId($cart->getId()) + ->filterById($cartItemId) + ->findOne(); + + if ($cartItem) { + $this->updateQuantity($cartItem, $quantity); + } + } catch (PropelException $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating cartItem with message : %s", $e->getMessage())); + } + } + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * The array keys are event names and the value can be: + * + * * The method name to call (priority defaults to 0) + * * An array composed of the method name to call and the priority + * * An array of arrays composed of the method names to call and respective + * priorities, or 0 if unset + * + * For instance: + * + * * array('eventName' => 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) + * + * @return array The event names to listen to + * + * @api + */ + public static function getSubscribedEvents() + { + return array( + "action.addArticle" => array("addArticle", 128), + "action.deleteArticle" => array("deleteArticle", 128), + "action.modifyArticle" => array("modifyArticle", 128), + ); + } + + + /** * increase the quantity for an existing cartItem * @@ -178,89 +265,4 @@ class Cart extends BaseAction implements EventSubscriberInterface return $cartAdd; } - /** - * - * Delete specify article present into cart - * - * @param \Thelia\Core\Event\ActionEvent $event - */ - public function deleteArticle(CartEvent $event) - { - $request = $event->getRequest(); - - if (null !== $cartItemId = $request->get('cartItem')) { - $cart = $event->getCart(); - try { - $cartItem = CartItemQuery::create() - ->filterByCartId($cart->getId()) - ->filterById($cartItemId) - ->delete(); - } catch (PropelException $e) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); - } - - } - } - - /** - * - * Modify article's quantity - * - * don't use Form here just test the Request. - * - * @param \Thelia\Core\Event\ActionEvent $event - */ - public function modifyArticle(CartEvent $event) - { - $request = $event->getRequest(); - - if (null !== $cartItemId = $request->get("cartItem") && null !== $quantity = $request->get("quantity")) { - - try { - $cart = $event->getCart($request); - - $cartItem = CartItemQuery::create() - ->filterByCartId($cart->getId()) - ->filterById($cartItemId) - ->findOne(); - - if ($cartItem) { - $this->updateQuantity($cartItem, $quantity); - } - } catch (PropelException $e) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating cartItem with message : %s", $e->getMessage())); - } - - } - } - - /** - * Returns an array of event names this subscriber wants to listen to. - * - * The array keys are event names and the value can be: - * - * * The method name to call (priority defaults to 0) - * * An array composed of the method name to call and the priority - * * An array of arrays composed of the method names to call and respective - * priorities, or 0 if unset - * - * For instance: - * - * * array('eventName' => 'methodName') - * * array('eventName' => array('methodName', $priority)) - * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) - * - * @return array The event names to listen to - * - * @api - */ - public static function getSubscribedEvents() - { - return array( - "action.addArticle" => array("addArticle", 128), - "action.deleteArticle" => array("deleteArticle", 128), - "action.modifyArticle" => array("modifyArticle", 128), - ); - } - } diff --git a/core/lib/Thelia/Controller/Front/CartController.php b/core/lib/Thelia/Controller/Front/CartController.php index 3a0a499b2..53a455aa7 100644 --- a/core/lib/Thelia/Controller/Front/CartController.php +++ b/core/lib/Thelia/Controller/Front/CartController.php @@ -24,18 +24,31 @@ namespace Thelia\Controller\Front; use Thelia\Core\Event\CartEvent; +use Thelia\Core\Event\TheliaEvents; class CartController extends BaseFrontController { use \Thelia\Cart\CartTrait; public function addArticle() + { + $cartEvent = $this->getCartEvent(); + + $this->dispatch(TheliaEvents::CART_ADD, $cartEvent); + } + + public function modifyArticle() + { + $cartEvent = $this->getCartEvent(); + + $this->dispatch(TheliaEvents::CART_MODIFYARTICLE, $cartEvent); + } + + protected function getCartEvent() { $request = $this->getRequest(); $cart = $this->getCart($request); - $cartEvent = new CartEvent($request, "action.addArticle", $cart); - - $this->dispatch("action.addArticle", $cartEvent); + return new CartEvent($request, $cart); } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/ActionEvent.php b/core/lib/Thelia/Core/Event/ActionEvent.php index 2cfde3d79..5285b9aaf 100755 --- a/core/lib/Thelia/Core/Event/ActionEvent.php +++ b/core/lib/Thelia/Core/Event/ActionEvent.php @@ -43,11 +43,6 @@ abstract class ActionEvent extends Event */ protected $request; - /** - * - * @var string - */ - protected $action; protected $errorForm = null; @@ -58,10 +53,9 @@ abstract class ActionEvent extends Event * @param \Symfony\Component\HttpFoundation\Request $request * @param string $action */ - public function __construct(Request $request, $action) + public function __construct(Request $request) { $this->request = $request; - $this->action = $action; } @@ -79,15 +73,6 @@ abstract class ActionEvent extends Event return null; } - /** - * - * @return string - */ - public function getAction() - { - return $this->action; - } - /** * * @return \Symfony\Component\HttpFoundation\Request diff --git a/core/lib/Thelia/Core/Event/CartEvent.php b/core/lib/Thelia/Core/Event/CartEvent.php index d8c091b5d..9dff52ed9 100644 --- a/core/lib/Thelia/Core/Event/CartEvent.php +++ b/core/lib/Thelia/Core/Event/CartEvent.php @@ -30,9 +30,9 @@ class CartEvent extends ActionEvent { protected $cart; - public function __construct(Request $request, $action, Cart $cart) + public function __construct(Request $request, Cart $cart) { - parent::__construct($request, $action); + parent::__construct($request); $this->cart = $cart; } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 00df25a08..4d0e7ba1d 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -121,10 +121,20 @@ final class TheliaEvents /** * sent when a new item is added to current cart */ - const CART_ADDITEM = "cart.addItem"; + const AFTER_CARTADDITEM = "cart.addItem"; /** * sent when a cart item is modify */ - const CART_MODIFYITEM = "cart.modifyItem"; + const AFTER_CARTMODIFYITEM = "cart.modifyItem"; + + /** + * sent for addArticle action + */ + const CART_ADDITEM = "action.addArticle"; + + /** + * sent on modify article action + */ + const CART_MODIFYARTICLE = "action.modifyArticle"; } \ No newline at end of file