Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

Commit

Permalink
misc refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Aug 16, 2014
1 parent ab1eb64 commit 9bb195d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 59 deletions.
108 changes: 74 additions & 34 deletions src/Cart/Cart.php
Expand Up @@ -17,7 +17,7 @@ class Cart implements Arrayable
/**
* Items in the cart.
*
* @var array
* @var CartItem[]
*/
private $items = array();

Expand Down Expand Up @@ -120,12 +120,12 @@ public function update($itemId, $key, $value)
{
$item = $this->find($itemId);

if ($item) {
$item->$key = $value;
} else {
if (!$item) {
throw new InvalidArgumentException(sprintf('Item [%s] does not exist in cart.', $itemId));
}

$item->$key = $value;

return $item->id;
}

Expand Down Expand Up @@ -160,7 +160,7 @@ public function has($itemId)
*
* @return mixed
*/
protected function find($itemId)
public function find($itemId)
{
foreach ($this->items as $item) {
if ($itemId === $item->id) {
Expand Down Expand Up @@ -188,9 +188,11 @@ public function totalUniqueItems()
*/
public function totalItems()
{
return array_sum(array_map(function ($item) {
return $item->quantity;
}, $this->items));
return array_sum(
array_map(function (CartItem $item) {
return $item->quantity;
}, $this->items)
);
}

/**
Expand All @@ -200,9 +202,11 @@ public function totalItems()
*/
public function total()
{
return (float) array_sum(array_map(function (CartItem $item) {
return (float) array_sum(
array_map(function (CartItem $item) {
return $item->getTotalPrice();
}, $this->items));
}, $this->items)
);
}

/**
Expand All @@ -212,9 +216,11 @@ public function total()
*/
public function totalExcludingTax()
{
return (float) array_sum(array_map(function (CartItem $item) {
return $item->getTotalPriceExcludingTax();
}, $this->items));
return (float) array_sum(
array_map(function (CartItem $item) {
return $item->getTotalPriceExcludingTax();
}, $this->items)
);
}

/**
Expand All @@ -224,9 +230,11 @@ public function totalExcludingTax()
*/
public function tax()
{
return (float) array_sum(array_map(function (CartItem $item) {
return $item->getTotalTax();
}, $this->items));
return (float) array_sum(
array_map(function (CartItem $item) {
return $item->getTotalTax();
}, $this->items)
);
}

/**
Expand Down Expand Up @@ -258,31 +266,63 @@ public function restore()
{
$state = $this->store->get($this->id);

// suppress unserializable error
$data = @unserialize($state);
$data = @unserialize($state); // suppress unserializable error

$this->restoreCheckType($data);
$this->restoreCheckContents($data);
$this->restoreCheckContentsType($data);

$this->id = $data['id'];
$this->items = array();

foreach ($data['items'] as $itemArr) {
$this->items[] = new CartItem($itemArr);
}
}

/**
* Check the data to be restored is of the correct type.
*
* @param mixed $data
*
* @throws CartRestoreException
*/
private function restoreCheckType($data)
{
if ($data === false) {
throw new CartRestoreException('Saved cart state is unserializable.');
}

if (is_array($data)) {

if (!isset($data['id']) || !isset($data['items'])) {
throw new CartRestoreException('Missing cart ID or cart items.');
}

if (!is_string($data['id']) || !is_array($data['items'])) {
throw new CartRestoreException('Cart ID not a string or cart items not an array.');
}
if (!is_array($data)) {
throw new CartRestoreException('Unserialized data is not an array.');
}
}

$this->id = $data['id'];
$this->items = array();
/**
* Check the contents of the data to be restored contains the correct data.
*
* @param array $data
*
* @throws CartRestoreException
*/
private function restoreCheckContents(array $data)
{
if (!isset($data['id']) || !isset($data['items'])) {
throw new CartRestoreException('Missing cart ID or cart items.');
}
}

foreach ($data['items'] as $itemArr) {
$this->items[] = new CartItem($itemArr);
}
} else {
throw new CartRestoreException('Unserialized data is not an array.');
/**
* Check the contents of the data to be restored is of the correct type.
*
* @param array $data
*
* @throws CartRestoreException
*/
private function restoreCheckContentsType(array $data)
{
if (!is_string($data['id']) || !is_array($data['items'])) {
throw new CartRestoreException('Cart ID not a string or cart items not an array.');
}
}

Expand Down
72 changes: 47 additions & 25 deletions src/Cart/CartItem.php
Expand Up @@ -5,6 +5,12 @@
use ArrayAccess;
use InvalidArgumentException;

/**
* @property string $id
* @property integer $quantity
* @property float $price
* @property float $tax
*/
class CartItem implements ArrayAccess, Arrayable
{
/**
Expand All @@ -21,23 +27,16 @@ class CartItem implements ArrayAccess, Arrayable
*/
public function __construct(array $data = array())
{
foreach ($data as $k => $v) {
$this->$k = $v;
}

// make sure quantity is set
if ( ! isset($this->quantity)) {
$this->quantity = 1;
}
$defaults = array(
'quantity' => 1,
'price' => 0.00,
'tax' => 0.00
);

// make sure price is set
if ( ! isset($this->price)) {
$this->price = 0.00;
}
$data = array_merge($defaults, $data);

// make sure tax is set
if ( ! isset($this->tax)) {
$this->tax = 0.00;
foreach ($data as $k => $v) {
$this->$k = $v;
}
}

Expand Down Expand Up @@ -86,32 +85,55 @@ public function get($key)
* @param mixed $value
*
* @return string
*
* @throws InvalidArgumentException
*/
public function set($key, $value)
{
switch ($key) {
case 'quantity':
if ( ! is_integer($value)) {
throw new InvalidArgumentException('Quantity must be an integer.');
}
$this->setCheckTypeInteger($value, $key);
break;
case 'price':
case 'tax':
if ( ! is_numeric($value)) {
throw new InvalidArgumentException(sprintf('%s must be numeric', $key));
}
$this->setCheckIsNumeric($value, $key);

$value = (float) $value;
break;
}

$this->data[$key] = $value;

return $this->getId();
}

/**
* Check the value being set is an integer.
*
* @param mixed $value
* @param string $name
*
* @throws InvalidArgumentException
*/
private function setCheckTypeInteger($value, $name)
{
if ( ! is_integer($value)) {
throw new InvalidArgumentException(sprintf('%s must be an integer.', $name));
}
}

/**
* Check the value being set is an integer.
*
* @param mixed $value
* @param string $name
*
* @throws InvalidArgumentException
*/
private function setCheckIsNumeric($value, $name)
{
if ( ! is_numeric($value)) {
throw new InvalidArgumentException(sprintf('%s must be numeric.', $name));
}
}

/**
* Get the total price of the cart item including tax.
*
Expand Down Expand Up @@ -159,7 +181,7 @@ public function getSinglePriceExcludingTax()
*/
public function getTotalTax()
{
return (float) ($this->tax * $this->quantity);
return (float) $this->tax * $this->quantity;
}

/**
Expand Down

0 comments on commit 9bb195d

Please sign in to comment.