Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
omnilight committed Dec 9, 2014
1 parent f5b10cc commit 1952689
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions ShoppingCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace yz\shoppingcart;

use Yii;
use yii\base\Component;
use yii\base\Event;
use Yii;


/**
Expand Down Expand Up @@ -53,6 +53,32 @@ public function init()
$this->loadFromSession();
}

/**
* Loads cart from session
*/
public function loadFromSession()
{
if (isset(Yii::$app->session[$this->cartId]))
$this->setSerialized(Yii::$app->session[$this->cartId]);
}

/**
* Saves cart to the session
*/
public function saveToSession()
{
Yii::$app->session[$this->cartId] = $this->getSerialized();
}

/**
* Sets cart from serialized string
* @param string $serialized
*/
public function setSerialized($serialized)
{
$this->_positions = unserialize($serialized);
}

/**
* @param CartPositionInterface $position
* @param int $quantity
Expand All @@ -73,7 +99,16 @@ public function put($position, $quantity = 1)
'data' => ['action' => 'put', 'position' => $this->_positions[$position->getId()]],
]));
if ($this->storeInSession)
$this->saveToSession();
$this->saveToSession();
}

/**
* Returns cart positions as serialized items
* @return string
*/
public function getSerialized()
{
return serialize($this->_positions);
}

/**
Expand All @@ -100,7 +135,7 @@ public function update($position, $quantity)
'data' => ['action' => 'update', 'position' => $this->_positions[$position->getId()]],
]));
if ($this->storeInSession)
$this->saveToSession();
$this->saveToSession();
}

/**
Expand All @@ -117,7 +152,7 @@ public function remove($position)
]));
unset($this->_positions[$position->getId()]);
if ($this->storeInSession)
$this->saveToSession();
$this->saveToSession();
}

/**
Expand All @@ -130,7 +165,7 @@ public function removeAll()
'data' => ['action' => 'removeAll'],
]));
if ($this->storeInSession)
$this->saveToSession();
$this->saveToSession();
}

/**
Expand Down Expand Up @@ -174,7 +209,7 @@ public function setPositions($positions)
'data' => ['action' => 'positions'],
]));
if ($this->storeInSession)
$this->saveToSession();
$this->saveToSession();
}

/**
Expand Down Expand Up @@ -232,34 +267,5 @@ public function getHash()
return md5(serialize($data));
}

/**
* Returns cart positions as serialized items
* @return string
*/
public function getSerialized()
{
return serialize($this->_positions);
}

/**
* Sets cart from serialized string
* @param string $serialized
*/
public function setSerialized($serialized)
{
$this->_positions = unserialize($serialized);
}

public function saveToSession()
{
Yii::$app->session[$this->cartId] = $this->getSerialized();
}

public function loadFromSession()
{
if (isset(Yii::$app->session[$this->cartId]))
$this->setSerialized(Yii::$app->session[$this->cartId]);
}


}

0 comments on commit 1952689

Please sign in to comment.