You are here

class WishlistSession in Commerce Wishlist 8.3

Default implementation of the wishlist session.

Hierarchy

Expanded class hierarchy of WishlistSession

1 string reference to 'WishlistSession'
commerce_wishlist.services.yml in ./commerce_wishlist.services.yml
commerce_wishlist.services.yml
1 service uses WishlistSession
commerce_wishlist.wishlist_session in ./commerce_wishlist.services.yml
Drupal\commerce_wishlist\WishlistSession

File

src/WishlistSession.php, line 10

Namespace

Drupal\commerce_wishlist
View source
class WishlistSession implements WishlistSessionInterface {

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

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

  /**
   * {@inheritdoc}
   */
  public function getWishlistIds() {
    return $this->session
      ->get('commerce_wishlists', []);
  }

  /**
   * {@inheritdoc}
   */
  public function addWishlistId($wishlist_id) {
    $ids = $this->session
      ->get('commerce_wishlists', []);
    $ids[] = $wishlist_id;
    $this->session
      ->set('commerce_wishlists', array_unique($ids));
  }

  /**
   * {@inheritdoc}
   */
  public function hasWishlistId($wishlist_id) {
    $ids = $this->session
      ->get('commerce_wishlists', []);
    return in_array($wishlist_id, $ids);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteWishlistId($wishlist) {
    $ids = $this->session
      ->get('commerce_wishlists', []);
    $ids = array_diff($ids, [
      $wishlist,
    ]);
    if (!empty($ids)) {
      $this->session
        ->set('commerce_wishlists', $ids);
    }
    else {

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

}

Members

Namesort descending Modifiers Type Description Overrides
WishlistSession::$session protected property The session.
WishlistSession::addWishlistId public function Adds the given wishlist ID to the session. Overrides WishlistSessionInterface::addWishlistId
WishlistSession::deleteWishlistId public function Deletes the given wishlist ID from the session. Overrides WishlistSessionInterface::deleteWishlistId
WishlistSession::getWishlistIds public function Gets all wishlist ids from the session. Overrides WishlistSessionInterface::getWishlistIds
WishlistSession::hasWishlistId public function Checks whether the given wishlist ID exists in the session. Overrides WishlistSessionInterface::hasWishlistId
WishlistSession::__construct public function Constructs a new WishlistSession object.