You are here

private function FlowForm::validateSyncCoreAccessToSite in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::validateSyncCoreAccessToSite()
  2. 2.0.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::validateSyncCoreAccessToSite()

Ask the Sync Core to ping the site for all required methods and show an error in the form if the site doesn't respond in time or with an error code.

Parameters

string $sync_core_url:

Return value

bool|TranslatableMarkup

Throws

\Exception

1 call to FlowForm::validateSyncCoreAccessToSite()
FlowForm::validateForm in src/Form/FlowForm.php
Form validation handler.

File

src/Form/FlowForm.php, line 2186

Class

FlowForm
Form handler for the Flow add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

private function validateSyncCoreAccessToSite($sync_core_url) {
  $methods = [
    SyncCoreClient::METHOD_POST,
    SyncCoreClient::METHOD_PATCH,
    SyncCoreClient::METHOD_DELETE,
  ];
  $auth_type = ContentSyncSettings::getInstance()
    ->getAuthenticationType();
  $export_url = ContentSyncSettings::getInstance()
    ->getSiteBaseUrl();
  $authentication_provider = AuthenticationByUser::getInstance();
  if (IApplicationInterface::AUTHENTICATION_TYPE_BASIC_AUTH == $auth_type) {
    $authentication = [
      'type' => 'basic_auth',
      'username' => $authentication_provider
        ->getUsername(),
      'password' => $authentication_provider
        ->getPassword(),
      'base_url' => $export_url,
    ];
  }
  else {
    $authentication = [
      'type' => 'drupal8_services',
      'username' => $authentication_provider
        ->getUsername(),
      'password' => $authentication_provider
        ->getPassword(),
      'base_url' => $export_url,
    ];
  }
  $client = SyncCoreFactory::getSyncCore($sync_core_url);
  foreach ($methods as $method) {
    try {
      $client
        ->requestPing($export_url, $method, $authentication);
    } catch (ForbiddenException $e) {
      return $this
        ->t('The Sync Core could not authenticate against this site. Please check that the Content Sync REST interface allows the configured authentication type and that the Content Sync user still exists. Message: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
    } catch (NotFoundException $e) {
      return $this
        ->t('The Sync Core did not receive a valid response from this site for the method %method. Please configure inbound traffic to be allowed from the <a href="https://edge-box.atlassian.net/wiki/spaces/SUP/pages/143982665/Technical+Requirements">Sync Core IP addresses</a> to this site. Message: @message', [
        '@message' => $e
          ->getMessage(),
        '%method' => $method,
      ]);
    } catch (BadRequestException $e) {
      return $this
        ->t('The Sync Core could not reach this site because the URL of this site is invalid. Please update the site base URL in the Content Sync Settings tab. Raw IP addresses and localhost domain names are not allowed. Message: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
    } catch (SyncCoreException $e) {
      return $this
        ->t('The Sync Core could not reach this site for the method %method. Please configure inbound traffic to be allowed from the <a href="https://edge-box.atlassian.net/wiki/spaces/SUP/pages/143982665/Technical+Requirements">Sync Core IP addresses</a> to this site. Message: @message', [
        '@message' => $e
          ->getMessage(),
        '%method' => $method,
      ]);
    }
  }
  return false;
}