dispatch event when cart is duplicated

This commit is contained in:
Manuel Raynaud
2013-08-02 16:01:21 +02:00
parent fe850718cc
commit 9995daa158
5 changed files with 23 additions and 33 deletions

View File

@@ -24,9 +24,12 @@
namespace Thelia\Action;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\CartEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Form\CartAdd;
use Thelia\Model\CartQuery;
@@ -36,6 +39,13 @@ use Thelia\Model\Customer;
class Cart implements EventSubscriberInterface
{
protected $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
*
* add an article to cart
@@ -44,6 +54,7 @@ class Cart implements EventSubscriberInterface
*/
public function addArticle(ActionEvent $event)
{
var_dump($this);
$request = $event->getRequest();
if ($request->isMethod("post")) {
@@ -66,7 +77,6 @@ class Cart implements EventSubscriberInterface
if($form->isValid()) {
} else {
var_dump($form->createView());
}
}
@@ -194,7 +204,10 @@ class Cart implements EventSubscriberInterface
$newCart = $cart->duplicate($this->generateCookie(), $customer);
$session->setCart($newCart->getId());
return $newCart;
$cartEvent = new CartEvent($newCart);
$this->dispatcher->dispatch(TheliaEvents::CART_DUPLICATE, $newCart);
return $cartEvent->cart;
}
protected function generateCookie()

View File

@@ -14,6 +14,8 @@
<service id="thelia.action.cart" class="Thelia\Action\Cart">
<tag name="kernel.event_subscriber"/>
<argument type="service" id="event_dispatcher"/>
</service>
<service id="thelia.action.customer" class="Thelia\Action\Customer" scope="request">

View File

@@ -29,30 +29,13 @@ use Thelia\Model\Cart;
class CartEvent extends InternalEvent {
protected $cart;
protected $modified;
public $cart;
public function __construct(Cart $cart)
{
$this->cart = $cart;
$this->modified = false;
}
public function setCart(Cart $cart)
{
$this->cart = $cart;
$this->modified = true;
}
public function getCart()
{
return $this->cart;
}
public function isModified()
{
return $this->modified;
}
}

View File

@@ -88,4 +88,9 @@ final class TheliaEvents
* Sent before customer insertion, to allow modules to create a custom customer reference.
*/
const CREATECUSTOMER_CUSTOMREF = "customer.creation.customref";
/**
* sent when a new existing cat id duplicated. This append when current customer is different from current cart
*/
const CART_DUPLICATE = "cart.duplicate";
}

View File

@@ -8,14 +8,6 @@ use Thelia\Model\Base\ProductSaleElementsQuery;
class Cart extends BaseCart
{
protected $dispatcher;
public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function duplicate($token, Customer $customer = null)
{
$cartItems = $this->getCartItems();
@@ -50,9 +42,4 @@ class Cart extends BaseCart
return $cart;
}
protected function dispatchEvent($name)
{
}
}