You are here

class AcquiaConnectorMiddleware in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 tests/modules/src/AcquiaConnectorMiddleware.php \Drupal\acquia_connector_test\AcquiaConnectorMiddleware
  2. 3.x tests/modules/src/AcquiaConnectorMiddleware.php \Drupal\acquia_connector_test\AcquiaConnectorMiddleware

Guzzle middleware for the Acquia Connector API.

Hierarchy

Expanded class hierarchy of AcquiaConnectorMiddleware

1 string reference to 'AcquiaConnectorMiddleware'
acquia_connector_test.services.yml in tests/modules/acquia_connector_test.services.yml
tests/modules/acquia_connector_test.services.yml
1 service uses AcquiaConnectorMiddleware
acquia_connector_test.client_middleware in tests/modules/acquia_connector_test.services.yml
Drupal\acquia_connector_test\AcquiaConnectorMiddleware

File

tests/modules/src/AcquiaConnectorMiddleware.php, line 12

Namespace

Drupal\acquia_connector_test
View source
class AcquiaConnectorMiddleware {

  /**
   * Invoked method that returns a promise.
   */
  public function __invoke() {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler) {
        $uri = $request
          ->getUri();

        // API requests to NSPI.
        if ($uri
          ->getScheme() . '://' . $uri
          ->getHost() === 'http://mock-spi-server') {
          return $this
            ->createPromise($request);
        }

        // Otherwise, no intervention. We defer to the handler stack.
        return $handler($request, $options);
      };
    };
  }

  /**
   * Creates a promise for the NSPI request.
   *
   * @param \Psr\Http\Message\RequestInterface $request
   *   Request interface.
   *
   * @return \GuzzleHttp\Promise\PromiseInterface
   *   Promise interface.
   *
   * @throws \Exception
   */
  protected function createPromise(RequestInterface $request) {
    $nspiController = new NspiController();
    $path = $request
      ->getUri()
      ->getPath();
    switch ($path) {
      case '/agent-api/subscription':
        $response = $nspiController
          ->getSubscription($request);
        $this
          ->updateRequestCount();
        break;
      case '/spi-api/site':
        $response = $nspiController
          ->nspiUpdate($request);
        $this
          ->updateRequestCount();
        break;
      case '/agent-api/subscription/communication':
        $response = $nspiController
          ->getCommunicationSettings($request);
        $this
          ->updateRequestCount();
        break;
      case '/agent-api/subscription/credentials':
        $response = $nspiController
          ->getCredentials($request);
        $this
          ->updateRequestCount();
        break;
      default:

        // @todo fix problem with adding native route matching and parsing.
        if (strstr($path, '/spi_def/get')) {
          $parts = explode('/', $path);
          $response = $nspiController
            ->spiDefinition($request, $parts[3]);
          $this
            ->updateRequestCount();
        }
        else {
          throw new \Exception("Unhandled path {$path}");
        }
    }
    return new FulfilledPromise($response);
  }

  /**
   * Update request count.
   */
  protected function updateRequestCount() {
    $requests = \Drupal::state()
      ->get('acquia_connector_test_request_count', 0);
    $requests++;
    \Drupal::state()
      ->set('acquia_connector_test_request_count', $requests);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaConnectorMiddleware::createPromise protected function Creates a promise for the NSPI request.
AcquiaConnectorMiddleware::updateRequestCount protected function Update request count.
AcquiaConnectorMiddleware::__invoke public function Invoked method that returns a promise.