Skip to content

Commit

Permalink
Merge pull request #16 from anam-hossain/feature/session-time
Browse files Browse the repository at this point in the history
Allow user to set session time
  • Loading branch information
anam-hossain authored Feb 25, 2019
2 parents 042f7c5 + c5ca6fe commit aac345a
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Exception;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;

class Cart implements CartInterface
Expand Down Expand Up @@ -36,10 +37,17 @@ class Cart implements CartInterface
*
* @param string $name
* @param \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface $storage
* @param array $options
* @return void
*/
public function __construct($name = null, SessionStorageInterface $storage = null)
public function __construct($name = null, SessionStorageInterface $storage = null, array $options = [])
{
if (!$storage) {
$options = array_merge(['gc_maxlifetime' => 604800], $options);

$storage = new NativeSessionStorage($options);
}

$this->session = new Session($storage);

$this->collection = new Collection();
Expand Down Expand Up @@ -82,7 +90,7 @@ public function named($name)
* @param Array $product
* @return \Anam\Phpcart\Collection
*/
public function add(Array $product)
public function add(array $product)
{
$this->collection->validateItem($product);

Expand All @@ -108,15 +116,15 @@ public function add(Array $product)
* @param Array $product
* @return \Anam\Phpcart\Collection
*/
public function update(Array $product)
public function update(array $product)
{
$this->collection->setItems($this->session->get($this->getCart(), []));

if (! isset($product['id'])) {
if (!isset($product['id'])) {
throw new Exception('id is required');
}

if (! $this->has($product['id'])) {
if (!$this->has($product['id'])) {
throw new Exception('There is no item in shopping cart with id: ' . $product['id']);
}

Expand Down Expand Up @@ -146,7 +154,6 @@ public function updateQty($id, $quantity)
return $this->update($item);
}


/**
* Update price of an Item.
*
Expand Down Expand Up @@ -224,7 +231,7 @@ public function has($id)
{
$this->collection->setItems($this->session->get($this->getCart(), []));

return $this->collection->findItem($id)? true : false;
return $this->collection->findItem($id) ? true : false;
}

/**
Expand All @@ -249,7 +256,7 @@ public function getTotal()
{
$items = $this->getItems();

return $items->sum(function($item) {
return $items->sum(function ($item) {
return $item->price * $item->quantity;
});
}
Expand All @@ -264,29 +271,29 @@ public function totalQuantity()
{
$items = $this->getItems();

return $items->sum(function($item) {
return $items->sum(function ($item) {
return $item->quantity;
});
}

/**
* Clone a cart to another
*
*
* @param mix $cart
*
*
* @return void
*/

public function copy($cart)
{
if (is_object($cart)) {
if (! $cart instanceof \Anam\Phpcart\Cart) {
if (!$cart instanceof \Anam\Phpcart\Cart) {
throw new InvalidArgumentException("Argument must be an instance of " . get_class($this));
}

$items = $this->session->get($cart->getCart(), []);
} else {
if (! $this->session->has($cart . self::CARTSUFFIX)) {
if (!$this->session->has($cart . self::CARTSUFFIX)) {
throw new Exception('Cart does not exist: ' . $cart);
}

Expand Down Expand Up @@ -318,4 +325,4 @@ public function clear()
{
$this->session->remove($this->getCart());
}
}
}

0 comments on commit aac345a

Please sign in to comment.