CartSession.php in Commerce Core 8.2
File
modules/cart/src/CartSession.php
View source
<?php
namespace Drupal\commerce_cart;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CartSession implements CartSessionInterface {
protected $session;
public function __construct(SessionInterface $session) {
$this->session = $session;
}
public function getCartIds($type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
return $this->session
->get($key, []);
}
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));
}
public function hasCartId($cart_id, $type = self::ACTIVE) {
$key = $this
->getSessionKey($type);
$ids = $this->session
->get($key, []);
return in_array($cart_id, $ids);
}
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 {
$this->session
->remove($key);
}
}
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];
}
}