public function NspiController::nspiUpdate in Acquia Connector 8
Same name and namespace in other branches
- 8.2 tests/modules/src/Controller/NspiController.php \Drupal\acquia_connector_test\Controller\NspiController::nspiUpdate()
- 3.x tests/modules/src/Controller/NspiController.php \Drupal\acquia_connector_test\Controller\NspiController::nspiUpdate()
SPI API site update.
Parameters
\GuzzleHttp\Psr7\Request $request: Request.
Return value
\GuzzleHttp\Psr7\Response JsonResponse.
File
- tests/
modules/ src/ Controller/ NspiController.php, line 75
Class
- NspiController
- Class NspiController.
Namespace
Drupal\acquia_connector_test\ControllerCode
public function nspiUpdate(Request $request) {
$data = json_decode($request
->getBody(), TRUE);
$fields = [
'time' => 'is_numeric',
'nonce' => 'is_string',
'hash' => 'is_string',
];
$result = $this
->basicAuthenticator($fields, $data);
if (!empty($result['error'])) {
return new Response(200, [], json_encode($result));
}
if (!empty($data['authenticator']['identifier'])) {
if ($data['authenticator']['identifier'] != self::ACQTEST_ID && $data['authenticator']['identifier'] != self::ACQTEST_ERROR_ID) {
return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], json_encode($this
->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
->t('Subscription not found'))));
}
if ($data['authenticator']['identifier'] == self::ACQTEST_ERROR_ID) {
return new Response();
}
else {
$result = $this
->validateAuthenticator($data);
// Needs for update definition.
$data['body']['spi_def_update'] = TRUE;
$spi_data = $data['body'];
$result['body'] = [
'spi_data_received' => TRUE,
];
if (isset($spi_data['spi_def_update'])) {
$result['body']['update_spi_definition'] = TRUE;
}
// Reflect send_method as nspi_messages if set.
if (isset($spi_data['send_method'])) {
$result['body']['nspi_messages'][] = $spi_data['send_method'];
}
$result['authenticator']['hash'] = CryptConnector::acquiaHash($result['secret']['key'], $result['authenticator']['time'] . ':' . $result['authenticator']['nonce']);
if (isset($spi_data['test_validation_error'])) {
// Force a validation fail.
$result['authenticator']['nonce'] = 'TEST';
}
$site_action = $spi_data['env_changed_action'];
// First connection.
if (empty($spi_data['site_uuid'])) {
$site_action = 'create';
}
switch ($site_action) {
case 'create':
$result['body']['site_uuid'] = self::ACQTEST_SITE_UUID;
// Set machine name.
\Drupal::state()
->set('acqtest_site_machine_name', $spi_data['machine_name']);
// Set name.
\Drupal::state()
->set('acqtest_site_name', $spi_data['name']);
$acquia_hosted = (int) filter_var($spi_data['acquia_hosted'], FILTER_VALIDATE_BOOLEAN);
\Drupal::state()
->set('acqtest_site_acquia_hosted', $acquia_hosted);
$result['body']['nspi_messages'][] = $this
->t('This is the first connection from this site, it may take awhile for it to appear.');
return new Response(200, [], json_encode($result));
case 'update':
$update = $this
->updateNspiSite($spi_data);
$result['body']['nspi_messages'][] = $update;
break;
case 'unblock':
\Drupal::state()
->delete('acqtest_site_blocked');
$result['body']['spi_error'] = '';
$result['body']['nspi_messages'][] = $this
->t('Your site has been enabled and is sending data to Acquia Cloud.');
return new Response(200, [], json_encode($result));
case 'block':
\Drupal::state()
->set('acqtest_site_blocked', TRUE);
$result['body']['spi_error'] = '';
$result['body']['nspi_messages'][] = $this
->t('You have disabled your site from sending data to Acquia Cloud.');
return new Response(200, [], json_encode($result));
}
// Update site name if it has changed.
$tacqtest_site_name = \Drupal::state()
->get('acqtest_site_name');
if (isset($spi_data['name']) && $spi_data['name'] != $tacqtest_site_name) {
if (!empty($tacqtest_site_name)) {
$name_update_message = $this
->t('Site name updated (from @old_name to @new_name).', [
'@old_name' => $tacqtest_site_name,
'@new_name' => $spi_data['name'],
]);
\Drupal::state()
->set('acqtest_site_name', $spi_data['name']);
}
$result['body']['nspi_messages'][] = $name_update_message;
}
// Detect Changes.
if ($changes = $this
->detectChanges($spi_data)) {
$result['body']['nspi_messages'][] = $changes['response'];
$result['body']['spi_error'] = TRUE;
$result['body']['spi_environment_changes'] = json_encode($changes['changes']);
return new Response(200, [], json_encode($result));
}
unset($result['secret']);
return new Response(200, [], json_encode($result));
}
}
else {
return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], json_encode($this
->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
->t('Invalid arguments'))));
}
}