class AcquiaConnectorMiddleware in Acquia Connector 3.x
Same name and namespace in other branches
- 8.2 tests/modules/src/AcquiaConnectorMiddleware.php \Drupal\acquia_connector_test\AcquiaConnectorMiddleware
- 8 tests/modules/src/AcquiaConnectorMiddleware.php \Drupal\acquia_connector_test\AcquiaConnectorMiddleware
Guzzle middleware for the Acquia Connector API.
Hierarchy
- class \Drupal\acquia_connector_test\AcquiaConnectorMiddleware
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
File
- tests/
modules/ src/ AcquiaConnectorMiddleware.php, line 12
Namespace
Drupal\acquia_connector_testView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiaConnectorMiddleware:: |
protected | function | Creates a promise for the NSPI request. | |
AcquiaConnectorMiddleware:: |
protected | function | Update request count. | |
AcquiaConnectorMiddleware:: |
public | function | Invoked method that returns a promise. |