final class CartTokenSession in Commerce Cart API 8
Decorates the cart session to support cart tokens.
Hierarchy
- class \Drupal\commerce_cart_api\CartTokenSession implements CartSessionInterface
Expanded class hierarchy of CartTokenSession
2 files declare their use of CartTokenSession
- CartTokenSessionConfiguration.php in src/
Session/ CartTokenSessionConfiguration.php - CartTokenSubscriber.php in src/
EventSubscriber/ CartTokenSubscriber.php
File
- src/
CartTokenSession.php, line 13
Namespace
Drupal\commerce_cart_apiView source
final class CartTokenSession implements CartSessionInterface {
const HEADER_NAME = 'Commerce-Cart-Token';
const QUERY_NAME = 'cartToken';
/**
* The inner cart session service.
*
* @var \Drupal\commerce_cart\CartSessionInterface
*/
private $inner;
/**
* Request stack service.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
private $requestStack;
/**
* The tempstore service.
*
* @var \Drupal\Core\TempStore\SharedTempStoreFactory
*/
private $tempStore;
/**
* Constructs a new CartTokenSession object.
*
* @param \Drupal\commerce_cart\CartSessionInterface $inner
* The decorated cart session.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
* The temp store factory.
*/
public function __construct(CartSessionInterface $inner, RequestStack $request_stack, SharedTempStoreFactory $temp_store_factory) {
$this->inner = $inner;
$this->requestStack = $request_stack;
$this->tempStore = $temp_store_factory
->get('commerce_cart_api_tokens');
}
/**
* {@inheritdoc}
*/
public function getCartIds($type = self::ACTIVE) {
if ($this
->getCurrentRequestCartToken() === NULL) {
return $this->inner
->getCartIds($type);
}
$data = $this
->getTokenCartData();
return $data[$type];
}
/**
* {@inheritdoc}
*/
public function addCartId($cart_id, $type = self::ACTIVE) {
$this->inner
->addCartId($cart_id, $type);
if ($this
->getCurrentRequestCartToken() !== NULL) {
$data = $this
->getTokenCartData();
$ids = $data[$type];
$ids[] = $cart_id;
$data[$type] = $ids;
$this
->setTokenCartData($data);
}
}
/**
* {@inheritdoc}
*/
public function hasCartId($cart_id, $type = self::ACTIVE) {
if ($this
->getCurrentRequestCartToken() === NULL) {
return $this->inner
->hasCartId($cart_id, $type);
}
$data = $this
->getTokenCartData();
$ids = $data[$type];
return in_array($cart_id, $ids, TRUE);
}
/**
* {@inheritdoc}
*/
public function deleteCartId($cart_id, $type = self::ACTIVE) {
$this->inner
->deleteCartId($cart_id, $type);
if ($this
->getCurrentRequestCartToken() !== NULL) {
$data = $this
->getTokenCartData();
$ids = $data[$type];
$ids = array_diff($ids, [
$cart_id,
]);
$data[$type] = $ids;
$this
->setTokenCartData($data);
}
}
/**
* Get the cart token from the request.
*
* @return string
* The cart token.
*/
private function getCurrentRequestCartToken() {
$request = $this->requestStack
->getCurrentRequest();
assert($request instanceof Request);
return $request->headers
->get(static::HEADER_NAME);
}
/**
* Get the token cart data.
*
* @return array
* The data.
*/
private function getTokenCartData() {
$defaults = [
static::ACTIVE => [],
static::COMPLETED => [],
];
$token = $this
->getCurrentRequestCartToken();
if (empty($token)) {
return $defaults;
}
return $this->tempStore
->get($token) ?: $defaults;
}
/**
* Set the token cart data.
*
* @param array $data
* The data.
*/
private function setTokenCartData(array $data) {
$token = $this
->getCurrentRequestCartToken();
if (!empty($token)) {
$this->tempStore
->set($token, $data);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartSessionInterface:: |
constant | |||
CartSessionInterface:: |
constant | |||
CartTokenSession:: |
private | property | The inner cart session service. | |
CartTokenSession:: |
private | property | Request stack service. | |
CartTokenSession:: |
private | property | The tempstore service. | |
CartTokenSession:: |
public | function |
Adds the given cart order ID to the session. Overrides CartSessionInterface:: |
|
CartTokenSession:: |
public | function |
Deletes the given cart order id from the session. Overrides CartSessionInterface:: |
|
CartTokenSession:: |
public | function |
Gets all cart order ids from the session. Overrides CartSessionInterface:: |
|
CartTokenSession:: |
private | function | Get the cart token from the request. | |
CartTokenSession:: |
private | function | Get the token cart data. | |
CartTokenSession:: |
public | function |
Checks whether the given cart order ID exists in the session. Overrides CartSessionInterface:: |
|
CartTokenSession:: |
constant | |||
CartTokenSession:: |
constant | |||
CartTokenSession:: |
private | function | Set the token cart data. | |
CartTokenSession:: |
public | function | Constructs a new CartTokenSession object. |