You are here

final class Context in Commerce Core 8.2

Contains known global information (customer, store, time).

Passed to price resolvers and availability checkers.

Hierarchy

Expanded class hierarchy of Context

22 files declare their use of Context
AddToCartForm.php in modules/cart/src/Form/AddToCartForm.php
AvailabilityCheckerInterface.php in modules/order/src/AvailabilityCheckerInterface.php
AvailabilityManager.php in modules/order/src/AvailabilityManager.php
AvailabilityManagerInterface.php in modules/order/src/AvailabilityManagerInterface.php
AvailabilityManagerTest.php in tests/src/Unit/AvailabilityManagerTest.php

... See full list

File

src/Context.php, line 13

Namespace

Drupal\commerce
View source
final class Context {

  /**
   * The customer.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $customer;

  /**
   * The store.
   *
   * @var \Drupal\commerce_store\Entity\StoreInterface
   */
  protected $store;

  /**
   * The time.
   *
   * @var int
   */
  protected $time;

  /**
   * The data.
   *
   * Used to provide additional information for a specific set of consumers
   * (e.g. price resolvers).
   *
   * @var array
   */
  protected $data;

  /**
   * Constructs a new Context object.
   *
   * @param \Drupal\Core\Session\AccountInterface $customer
   *   The customer.
   * @param \Drupal\commerce_store\Entity\StoreInterface $store
   *   The store.
   * @param int|null $time
   *   The unix timestamp, or NULL to use the current time.
   * @param array $data
   *   The data.
   */
  public function __construct(AccountInterface $customer, StoreInterface $store, int $time = NULL, array $data = []) {
    $this->customer = $customer;
    $this->store = $store;
    $this->time = $time ?: time();
    $this->data = $data;
  }

  /**
   * Gets the customer.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The customer.
   */
  public function getCustomer() : AccountInterface {
    return $this->customer;
  }

  /**
   * Gets the store.
   *
   * @return \Drupal\commerce_store\Entity\StoreInterface
   *   The store.
   */
  public function getStore() : StoreInterface {
    return $this->store;
  }

  /**
   * Gets the time.
   *
   * @return int
   *   The time.
   */
  public function getTime() : int {
    return $this->time;
  }

  /**
   * Gets a data value with the given key.
   *
   * @param string $key
   *   The key.
   * @param mixed $default
   *   The default value.
   *
   * @return mixed
   *   The value.
   */
  public function getData(string $key, $default = NULL) {
    return isset($this->data[$key]) ? $this->data[$key] : $default;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Context::$customer protected property The customer.
Context::$data protected property The data.
Context::$store protected property The store.
Context::$time protected property The time.
Context::getCustomer public function Gets the customer.
Context::getData public function Gets a data value with the given key.
Context::getStore public function Gets the store.
Context::getTime public function Gets the time.
Context::__construct public function Constructs a new Context object.