You are here

class CartSession in Commerce Core 8.2

Default implementation of the cart session.

Hierarchy

Expanded class hierarchy of CartSession

1 file declares its use of CartSession
CheckoutController.php in modules/checkout/src/Controller/CheckoutController.php
1 string reference to 'CartSession'
commerce_cart.services.yml in modules/cart/commerce_cart.services.yml
modules/cart/commerce_cart.services.yml
1 service uses CartSession
commerce_cart.cart_session in modules/cart/commerce_cart.services.yml
Drupal\commerce_cart\CartSession

File

modules/cart/src/CartSession.php, line 10

Namespace

Drupal\commerce_cart
View source
class CartSession implements CartSessionInterface {

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
   */
  protected $session;

  /**
   * Constructs a new CartSession object.
   *
   * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
   *   The session.
   */
  public function __construct(SessionInterface $session) {
    $this->session = $session;
  }

  /**
   * {@inheritdoc}
   */
  public function getCartIds($type = self::ACTIVE) {
    $key = $this
      ->getSessionKey($type);
    return $this->session
      ->get($key, []);
  }

  /**
   * {@inheritdoc}
   */
  public function addCartId($cart_id, $type = self::ACTIVE) {
    $key = $this
      ->getSessionKey($type);
    $ids = $this->session
      ->get($key, []);
    $ids[] = $cart_id;
    $this->session
      ->set($key, array_unique($ids));
  }

  /**
   * {@inheritdoc}
   */
  public function hasCartId($cart_id, $type = self::ACTIVE) {
    $key = $this
      ->getSessionKey($type);
    $ids = $this->session
      ->get($key, []);
    return in_array($cart_id, $ids);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteCartId($cart_id, $type = self::ACTIVE) {
    $key = $this
      ->getSessionKey($type);
    $ids = $this->session
      ->get($key, []);
    $ids = array_diff($ids, [
      $cart_id,
    ]);
    if (!empty($ids)) {
      $this->session
        ->set($key, $ids);
    }
    else {

      // Remove the empty list to allow the system to clean up empty sessions.
      $this->session
        ->remove($key);
    }
  }

  /**
   * Gets the session key for the given cart session type.
   *
   * @param string $type
   *   The cart session type.
   *
   * @return string
   *   The session key.
   *
   * @throws \InvalidArgumentException
   *   Thrown when the given $type is unknown.
   */
  protected function getSessionKey($type) {
    $keys = [
      self::ACTIVE => 'commerce_cart_orders',
      self::COMPLETED => 'commerce_cart_completed_orders',
    ];
    if (!isset($keys[$type])) {
      throw new \InvalidArgumentException(sprintf('Unknown cart session type "%s".', $type));
    }
    return $keys[$type];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CartSession::$session protected property The session.
CartSession::addCartId public function Adds the given cart order ID to the session. Overrides CartSessionInterface::addCartId
CartSession::deleteCartId public function Deletes the given cart order id from the session. Overrides CartSessionInterface::deleteCartId
CartSession::getCartIds public function Gets all cart order ids from the session. Overrides CartSessionInterface::getCartIds
CartSession::getSessionKey protected function Gets the session key for the given cart session type.
CartSession::hasCartId public function Checks whether the given cart order ID exists in the session. Overrides CartSessionInterface::hasCartId
CartSession::__construct public function Constructs a new CartSession object.
CartSessionInterface::ACTIVE constant
CartSessionInterface::COMPLETED constant