Skip to content

Commit

Permalink
Added CartActionEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
omnilight committed Apr 6, 2015
1 parent 5e3f47c commit b7f38c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
28 changes: 28 additions & 0 deletions CartActionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace yz\shoppingcart;
use yii\base\Event;


/**
* Class CartActionEvent
*/
class CartActionEvent extends Event
{
const ACTION_UPDATE = 'update';
const ACTION_POSITION_PUT = 'positionPut';
const ACTION_BEFORE_REMOVE = 'beforeRemove';
const ACTION_REMOVE_ALL = 'removeAll';
const ACTION_SET_POSITIONS = 'setPositions';

/**
* Name of the action taken on the cart
* @var string
*/
public $action;
/**
* Position of the cart that was affected. Could be null if action deals with all positions of the cart
* @var CartPositionInterface
*/
public $position;
}
38 changes: 22 additions & 16 deletions ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ public function put($position, $quantity = 1)
$position->setQuantity($quantity);
$this->_positions[$position->getId()] = $position;
}
$this->trigger(self::EVENT_POSITION_PUT, new Event([
'data' => $this->_positions[$position->getId()],
$this->trigger(self::EVENT_POSITION_PUT, new CartActionEvent([
'action' => CartActionEvent::ACTION_POSITION_PUT,
'position' => $this->_positions[$position->getId()],
]));
$this->trigger(self::EVENT_CART_CHANGE, new Event([
'data' => ['action' => 'put', 'position' => $this->_positions[$position->getId()]],
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_POSITION_PUT,
'position' => $this->_positions[$position->getId()],
]));
if ($this->storeInSession)
$this->saveToSession();
Expand Down Expand Up @@ -137,11 +139,13 @@ public function update($position, $quantity)
$position->setQuantity($quantity);
$this->_positions[$position->getId()] = $position;
}
$this->trigger(self::EVENT_POSITION_UPDATE, new Event([
'data' => $this->_positions[$position->getId()],
$this->trigger(self::EVENT_POSITION_UPDATE, new CartActionEvent([
'action' => CartActionEvent::ACTION_UPDATE,
'position' => $this->_positions[$position->getId()],
]));
$this->trigger(self::EVENT_CART_CHANGE, new Event([
'data' => ['action' => 'update', 'position' => $this->_positions[$position->getId()]],
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_UPDATE,
'position' => $this->_positions[$position->getId()],
]));
if ($this->storeInSession)
$this->saveToSession();
Expand All @@ -162,11 +166,13 @@ public function remove($position)
*/
public function removeById($id)
{
$this->trigger(self::EVENT_BEFORE_POSITION_REMOVE, new Event([
'data' => $this->_positions[$id],
$this->trigger(self::EVENT_BEFORE_POSITION_REMOVE, new CartActionEvent([
'action' => CartActionEvent::ACTION_BEFORE_REMOVE,
'position' => $this->_positions[$id],
]));
$this->trigger(self::EVENT_CART_CHANGE, new Event([
'data' => ['action' => 'remove', 'position' => $this->_positions[$id]],
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_BEFORE_REMOVE,
'position' => $this->_positions[$id],
]));
unset($this->_positions[$id]);
if ($this->storeInSession)
Expand All @@ -179,8 +185,8 @@ public function removeById($id)
public function removeAll()
{
$this->_positions = [];
$this->trigger(self::EVENT_CART_CHANGE, new Event([
'data' => ['action' => 'removeAll'],
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_REMOVE_ALL,
]));
if ($this->storeInSession)
$this->saveToSession();
Expand Down Expand Up @@ -223,8 +229,8 @@ public function getPositions()
public function setPositions($positions)
{
$this->_positions = $positions;
$this->trigger(self::EVENT_CART_CHANGE, new Event([
'data' => ['action' => 'positions'],
$this->trigger(self::EVENT_CART_CHANGE, new CartActionEvent([
'action' => CartActionEvent::ACTION_SET_POSITIONS,
]));
if ($this->storeInSession)
$this->saveToSession();
Expand Down

0 comments on commit b7f38c1

Please sign in to comment.