Session::getCart return a Thelia\Model\Cart instance or null if cart is
not valid
This commit is contained in:
@@ -25,13 +25,14 @@ namespace Thelia\Core\HttpFoundation\Session;
|
|||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Session\Session as BaseSession;
|
use Symfony\Component\HttpFoundation\Session\Session as BaseSession;
|
||||||
use Thelia\Core\Security\User\UserInterface;
|
use Thelia\Core\Security\User\UserInterface;
|
||||||
use Thelia\Form\BaseForm;
|
use Thelia\Exception\InvalidCartException;
|
||||||
use Thelia\Model\ConfigQuery;
|
use Thelia\Model\Base\CartQuery;
|
||||||
|
use Thelia\Model\Cart;
|
||||||
use Thelia\Tools\URL;
|
use Thelia\Tools\URL;
|
||||||
|
|
||||||
class Session extends BaseSession {
|
class Session extends BaseSession
|
||||||
|
{
|
||||||
// -- Language ------------------------------------------------------------
|
// -- Language ------------------------------------------------------------
|
||||||
|
|
||||||
public function getLocale()
|
public function getLocale()
|
||||||
{
|
{
|
||||||
@@ -47,34 +48,34 @@ class Session extends BaseSession {
|
|||||||
|
|
||||||
public function setCustomerUser(UserInterface $user)
|
public function setCustomerUser(UserInterface $user)
|
||||||
{
|
{
|
||||||
$this->set('customer_user', $user);
|
$this->set('customer_user', $user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCustomerUser()
|
public function getCustomerUser()
|
||||||
{
|
{
|
||||||
return $this->get('customer_user');
|
return $this->get('customer_user');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clearCustomerUser()
|
public function clearCustomerUser()
|
||||||
{
|
{
|
||||||
return $this->remove('customer_user');
|
return $this->remove('customer_user');
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Admin user -----------------------------------------------------------
|
// -- Admin user -----------------------------------------------------------
|
||||||
|
|
||||||
public function setAdminUser(UserInterface $user)
|
public function setAdminUser(UserInterface $user)
|
||||||
{
|
{
|
||||||
$this->set('admin_user', $user);
|
$this->set('admin_user', $user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAdminUser()
|
public function getAdminUser()
|
||||||
{
|
{
|
||||||
return $this->get('admin_user');
|
return $this->get('admin_user');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clearAdminUser()
|
public function clearAdminUser()
|
||||||
{
|
{
|
||||||
return $this->remove('admin_user');
|
return $this->remove('admin_user');
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Error form -----------------------------------------------------------
|
// -- Error form -----------------------------------------------------------
|
||||||
@@ -84,24 +85,24 @@ class Session extends BaseSession {
|
|||||||
*/
|
*/
|
||||||
public function setErrorFormName($formName)
|
public function setErrorFormName($formName)
|
||||||
{
|
{
|
||||||
$this->set('error_form', $formName);
|
$this->set('error_form', $formName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getErrorFormName()
|
public function getErrorFormName()
|
||||||
{
|
{
|
||||||
return $this->get('error_form', null);
|
return $this->get('error_form', null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clearErrorFormName()
|
public function clearErrorFormName()
|
||||||
{
|
{
|
||||||
return $this->remove('error_form');
|
return $this->remove('error_form');
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Return page ----------------------------------------------------------
|
// -- Return page ----------------------------------------------------------
|
||||||
|
|
||||||
public function setReturnToUrl($url)
|
public function setReturnToUrl($url)
|
||||||
{
|
{
|
||||||
$this->set('return_to_url', $url);
|
$this->set('return_to_url', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,7 +111,7 @@ class Session extends BaseSession {
|
|||||||
*/
|
*/
|
||||||
public function getReturnToUrl()
|
public function getReturnToUrl()
|
||||||
{
|
{
|
||||||
return $this->get('return_to_url', URL::getIndexPage());
|
return $this->get('return_to_url', URL::getIndexPage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Cart ------------------------------------------------------------------
|
// -- Cart ------------------------------------------------------------------
|
||||||
@@ -122,7 +123,28 @@ class Session extends BaseSession {
|
|||||||
*/
|
*/
|
||||||
public function getCart()
|
public function getCart()
|
||||||
{
|
{
|
||||||
return $this->get("cart_id");
|
$cart_id = $this->get("cart_id");
|
||||||
|
$cart = null;
|
||||||
|
if ($cart_id) {
|
||||||
|
$cart = CartQuery::create()->findPk($cart_id);
|
||||||
|
try {
|
||||||
|
$this->verifyValidCart($cart);
|
||||||
|
} catch (InvalidCartException $e) {
|
||||||
|
$cart = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $cart;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function verifyValidCart(Cart $cart)
|
||||||
|
{
|
||||||
|
$customer = $this->getCustomerUser();
|
||||||
|
if ($customer && $cart->getCustomerId() != $customer->getId()) {
|
||||||
|
throw new InvalidCartException("customer in session and customer_id in cart are not the same");
|
||||||
|
} else if($customer === null && $cart->getCustomerId() !== null) {
|
||||||
|
throw new InvalidCartException("Customer exists in cart and not in session");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user