You are here

protected function Negotiator::doNegotiateFromRequest in Consumers 8

Obtains the consumer from the request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Drupal\consumers\Entity\Consumer|null The consumer.

Throws

\Drupal\consumers\MissingConsumer

1 call to Negotiator::doNegotiateFromRequest()
Negotiator::negotiateFromRequest in src/Negotiator.php
Obtains the consumer from the request.

File

src/Negotiator.php, line 62

Class

Negotiator
Extracts the consumer information from the given context.

Namespace

Drupal\consumers

Code

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;
}