You are here

class Negotiator in Consumers 8

Extracts the consumer information from the given context.

@internal

Hierarchy

  • class \Drupal\consumers\Negotiator uses \Psr\Log\LoggerAwareTrait

Expanded class hierarchy of Negotiator

1 string reference to 'Negotiator'
consumers.services.yml in ./consumers.services.yml
consumers.services.yml
1 service uses Negotiator
consumer.negotiator in ./consumers.services.yml
Drupal\consumers\Negotiator

File

src/Negotiator.php, line 18

Namespace

Drupal\consumers
View source
class Negotiator {
  use LoggerAwareTrait;

  /**
   * Protected requestStack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Protected entityRepository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * Instantiates a new Negotiator object.
   */
  public function __construct(RequestStack $request_stack, EntityRepositoryInterface $entity_repository) {
    $this->requestStack = $request_stack;
    $this->entityRepository = $entity_repository;
  }

  /**
   * Obtains the consumer from the request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return \Drupal\consumers\Entity\Consumer|null
   *   The consumer.
   *
   * @throws \Drupal\consumers\MissingConsumer
   */
  protected function doNegotiateFromRequest(Request $request) {

    // There are several ways to negotiate the consumer:
    // 1. Via a custom header.
    $consumer_uuid = $request->headers
      ->get('X-Consumer-ID');
    if (!$consumer_uuid) {

      // 2. Via a query string parameter.
      $consumer_uuid = $request->query
        ->get('consumerId');
      if (!$consumer_uuid && $request->query
        ->has('_consumer_id')) {
        $this->logger
          ->warning('The "_consumer_id" query string parameter is deprecated and it will be removed in the next major version of the module, please use "consumerId" instead.');
        $consumer_uuid = $request->query
          ->get('_consumer_id');
      }
    }
    if (!$consumer_uuid) {
      return $this
        ->loadDefaultConsumer();
    }
    try {

      /** @var \Drupal\consumers\Entity\Consumer $consumer */
      $consumer = $this->entityRepository
        ->loadEntityByUuid('consumer', $consumer_uuid);
    } catch (EntityStorageException $exception) {
      watchdog_exception('consumers', $exception);
      return $this
        ->loadDefaultConsumer();
    }
    return $consumer;
  }

  /**
   * Obtains the consumer from the request.
   *
   * @param \Symfony\Component\HttpFoundation\Request|null $request
   *   The request object to inspect for a consumer. Set to NULL to use the
   *   current request.
   *
   * @return \Drupal\consumers\Entity\Consumer|null
   *   The consumer.
   *
   * @throws \Drupal\consumers\MissingConsumer
   */
  public function negotiateFromRequest(Request $request = NULL) {

    // If the request is not provided, use the request from the stack.
    $request = $request ? $request : $this->requestStack
      ->getCurrentRequest();
    $consumer = $this
      ->doNegotiateFromRequest($request);
    $request->attributes
      ->set('consumer_uuid', $consumer
      ->uuid());
    return $consumer;
  }

  /**
   * Finds and loads the default consumer.
   *
   * @return \Drupal\consumers\Entity\Consumer
   *   The consumer entity.
   *
   * @throws \Drupal\consumers\MissingConsumer
   */
  protected function loadDefaultConsumer() {

    // Find the default consumer.
    $results = $this->storage
      ->getQuery()
      ->condition('is_default', TRUE)
      ->execute();
    $consumer_id = reset($results);
    if (!$consumer_id) {

      // Throw if there is no default consumer..
      throw new MissingConsumer('Unable to find the default consumer.');
    }

    /** @var \Drupal\consumers\Entity\Consumer $consumer */
    $consumer = $this->storage
      ->load($consumer_id);
    return $consumer;
  }

  /**
   * Sets the storage from the entity type manager.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function setEntityStorage(EntityTypeManagerInterface $entity_type_manager) {
    $this->storage = $entity_type_manager
      ->getStorage('consumer');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Negotiator::$entityRepository protected property Protected entityRepository.
Negotiator::$requestStack protected property Protected requestStack.
Negotiator::$storage protected property The entity storage.
Negotiator::doNegotiateFromRequest protected function Obtains the consumer from the request.
Negotiator::loadDefaultConsumer protected function Finds and loads the default consumer.
Negotiator::negotiateFromRequest public function Obtains the consumer from the request.
Negotiator::setEntityStorage public function Sets the storage from the entity type manager.
Negotiator::__construct public function Instantiates a new Negotiator object.