Skip to content

Commit

Permalink
update basket api
Browse files Browse the repository at this point in the history
  • Loading branch information
jirisvoboda committed Jul 6, 2015
1 parent eb742a5 commit a7568dc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
84 changes: 64 additions & 20 deletions lib/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
* @property int $count
* @property Session $session
*/
class Basket extends Component
{
class Basket extends Component {

const ITEM_PRODUCT = '\dlds\ecom\models\BasketProductInterface';
const ITEM_DISCOUNT = '\dlds\ecom\models\BasketDiscountInterface';

use SubComponentTrait;

/**
* @var BasketItemInterface[]
*/
Expand All @@ -39,6 +38,7 @@ class Basket extends Component
* @var Session
*/
private $session;

/**
* Override this to provide custom (e.g. database) storage for basket data
*
Expand Down Expand Up @@ -92,11 +92,14 @@ public function setStorage($storage)
*/
public function createOrder(OrderInterface $order, $clear = true)
{
try {
try
{
$order->saveFromBasket($this);
$clear && $this->clear();
return $order;
} catch (\Exception $exception) {
}
catch (\Exception $exception)
{
throw $exception;
}
}
Expand All @@ -121,8 +124,34 @@ public function add(BasketItemInterface $element, $save = true)
*/
protected function addItem(BasketItemInterface $item)
{
$uniqueId = md5(uniqid('_bs', true));
$this->items[$uniqueId] = $item;
$id = $this->getItemId($item);

if (isset($this->items[$id]))
{
$this->items[$id]->addQuantity($item->getQuantity());
}
else
{
$this->items[$id] = $item;
}

if ($this->items[$id]->getQuantity() < 1)
{
$this->remove($id);
}
}

/**
* Get an item from the basket
*
* @param models\BasketItemInterface $element
* @return $this
*/
public function get(BasketItemInterface $item)
{
$id = $this->getItemId($item);

return isset($this->items[$id]) ? $this->items[$id] : null;
}

/**
Expand All @@ -133,12 +162,15 @@ protected function addItem(BasketItemInterface $item)
* @throws \yii\base\InvalidParamException
* @return $this
*/
public function remove($uniqueId, $save = true)
public function remove(BasketItemInterface $item, $save = true)
{
if (!isset($this->items[$uniqueId])) {
$id = $this->getItemId($item);

if (!isset($this->items[$id]))
{
throw new InvalidParamException('Item not found');
}
unset($this->items[$uniqueId]);
unset($this->items[$id]);

$save && $this->storage->save($this);
return $this;
Expand All @@ -162,13 +194,14 @@ public function getCount($itemType = null)
public function getItems($itemType = null)
{
$items = $this->items;
if (!is_null($itemType)) {
$items = array_filter($items,
function ($item) use ($itemType) {
/** @var $item BasketItemInterface */
return is_subclass_of($item, $itemType);
});
if (!is_null($itemType))
{
$items = array_filter($items, function ($item) use ($itemType) {
/** @var $item BasketItemInterface */
return is_subclass_of($item, $itemType);
});
}

return $items;
}

Expand Down Expand Up @@ -198,7 +231,8 @@ public function getTotalDiscounts($format = true)
public function getAttributeTotal($attribute, $itemType = null)
{
$sum = 0;
foreach ($this->getItems($itemType) as $model) {
foreach ($this->getItems($itemType) as $model)
{
$sum += $model->{$attribute};
}
return $sum;
Expand All @@ -216,7 +250,8 @@ public function getTotalDue($format = true)
$sum = $this->getAttributeTotal('totalPrice', self::ITEM_PRODUCT);

// apply discounts
foreach ($this->getItems(self::ITEM_DISCOUNT) as $discount) {
foreach ($this->getItems(self::ITEM_DISCOUNT) as $discount)
{
/** @var $discount BasketDiscountInterface */
$discount->applyToBasket($this, $sum);
}
Expand Down Expand Up @@ -252,6 +287,14 @@ protected function getStorage()
return $this->storage;
}

/**
* Retrieves item id
*/
protected function getItemId(BasketItemInterface $item)
{
return md5(sprintf('%s_%s', $item->getType(), $item->getPKValue()));
}

/**
* @param string $uniqueId
* @param string $attribute
Expand All @@ -260,10 +303,11 @@ protected function getStorage()
*/
public function update($uniqueId, $attribute, $value)
{
if (!isset($this->items[$uniqueId]) || !$this->items[$uniqueId]->hasAttribute($attribute)) {
if (!isset($this->items[$uniqueId]) || !$this->items[$uniqueId]->hasAttribute($attribute))
{
return false;
}

$this->items[$uniqueId]->setAttribute($attribute, $value);
}
}
}
25 changes: 22 additions & 3 deletions lib/models/BasketItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*
* @package dlds\ecom\models
*/
interface BasketItemInterface extends \Serializable
{
interface BasketItemInterface extends \Serializable {

/**
* Returns the label for the basket item (displayed in basket etc)
*
Expand Down Expand Up @@ -57,4 +57,23 @@ public function hasAttribute($name);
* @return string
*/
public function getPKValue();
}

/**
* Retrieves Basket Item type
*
* @return string
*/
public function getType();

/**
* Adds quantity of product (if quantity is < 0 it will subtract the quantity)
* @param int $quantity
*/
public function addQuantity($quantity);

/**
* Gets quantity of product
* @return int quantity of product
*/
public function getQuantity();
}

0 comments on commit a7568dc

Please sign in to comment.