You are here

public static function UpgradeStatusForm::doHttpRequest in Upgrade Status 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::doHttpRequest()

Do an HTTP request with the type and machine name.

Parameters

string $project_machine_name: The machine name of the project.

Return value

array A three item array with any potential errors, the error message and the returned data as the third item. Either of them will be FALSE if they are not applicable. Data may also be NULL if response JSON decoding failed.

2 calls to UpgradeStatusForm::doHttpRequest()
UpgradeStatusForm::parseProject in src/Form/UpgradeStatusForm.php
Batch callback to analyze a project.
UpgradeStatusForm::submitForm in src/Form/UpgradeStatusForm.php
Form submission handler.

File

src/Form/UpgradeStatusForm.php, line 1288

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

public static function doHttpRequest(string $project_machine_name) {
  $error = $message = $data = FALSE;

  // Prepare for a POST request to scan this project. The separate HTTP
  // request is used to separate any PHP errors found from this batch process.
  // We can store any errors and gracefully continue if there was any PHP
  // errors in parsing.
  $url = Url::fromRoute('upgrade_status.analyze', [
    'project_machine_name' => $project_machine_name,
  ]);

  // Pass over authentication information because access to this functionality
  // requires administrator privileges.

  /** @var \Drupal\Core\Session\SessionConfigurationInterface $session_config */
  $session_config = \Drupal::service('session_configuration');
  $request = \Drupal::request();
  $session_options = $session_config
    ->getOptions($request);

  // Unfortunately DrupalCI testbot does not have a domain that would normally
  // be considered valid for cookie setting, so we need to work around that
  // by manually setting the cookie domain in case there was none. What we
  // care about is we get actual results, and cookie on the host level should
  // suffice for that.
  $cookie_domain = empty($session_options['cookie_domain']) ? '.' . $request
    ->getHost() : $session_options['cookie_domain'];
  $cookie_jar = new CookieJar();
  $cookie = new SetCookie([
    'Name' => $session_options['name'],
    'Value' => $request->cookies
      ->get($session_options['name']),
    'Domain' => $cookie_domain,
    'Secure' => $session_options['cookie_secure'],
  ]);
  $cookie_jar
    ->setCookie($cookie);
  $options = [
    'cookies' => $cookie_jar,
    'timeout' => 0,
  ];

  // Try a POST request with the session cookie included. We expect valid JSON
  // back. In case there was a PHP error before that, we log that.
  try {
    $response = \Drupal::httpClient()
      ->post($url
      ->setAbsolute()
      ->toString(), $options);
    $data = json_decode((string) $response
      ->getBody(), TRUE);
    if (!$data) {
      $error = 'PHP Fatal Error';
      $message = (string) $response
        ->getBody();
    }
  } catch (\Exception $e) {
    $error = 'Scanning exception';
    $message = $e
      ->getMessage();
  }
  return [
    $error,
    $message,
    $data,
  ];
}