You are here

public function UpdateHelper::sendUpdates in Evercurrent 8.2

Same name and namespace in other branches
  1. 8 src/UpdateHelper.php \Drupal\evercurrent\UpdateHelper::sendUpdates()

Send updates to the Maintenance server.

Parameters

bool $force: Force Drupal's update collector to refresh available updates.

string $key: Use another key than what is saved.

bool $out: Display error messages as screen messages.

Return value

bool Success Status

Overrides UpdateHelperInterface::sendUpdates

1 call to UpdateHelper::sendUpdates()
UpdateHelper::testUpdate in src/UpdateHelper.php
Test sending an update to the server.

File

src/UpdateHelper.php, line 73

Class

UpdateHelper
Default UpdateHelper instantiation.

Namespace

Drupal\evercurrent

Code

public function sendUpdates($force = TRUE, $key = NULL, $out = FALSE) {
  $config = \Drupal::config('evercurrent.admin_config');
  if (!$key) {
    $key = $this
      ->getKeyFromSettings();
  }
  $valid = $this
    ->keyCheck($key);
  if (!$valid) {
    $this
      ->writeStatus(RMH_STATUS_WARNING, 'RMH Update check not run. The key is not a vaild key. It should be a 32-digit string with only letters and numbers.', $out);
    return FALSE;
  }
  $data = [];
  if ($available = update_get_available(TRUE)) {
    module_load_include('inc', 'update', 'update.compare');
    $data = update_calculate_project_data($available);
  }

  // Module version.
  $version = \Drupal::service('extension.list.module')
    ->getExtensionInfo('evercurrent');
  $sender_data = [
    'send_url' => $config
      ->get('target_address'),
    'project_name' => $this
      ->getEnvironmentUrl(),
    'key' => $key,
    'module_version' => $version['version'],
    'api_version' => 1,
    'updates' => [],
  ];
  $status_list = [
    UpdateManagerInterface::NOT_SECURE,
    UpdateManagerInterface::REVOKED,
    UpdateManagerInterface::NOT_SUPPORTED,
    UpdateManagerInterface::CURRENT,
    UpdateFetcherInterface::NOT_CHECKED,
    UpdateManagerInterface::NOT_CURRENT,
  ];
  foreach ($data as $module => $module_info) {
    if (in_array($module_info['status'], $status_list)) {
      $sender_data['updates'][$module] = $data[$module];

      // In some cases (like multisite installations),
      // modules on certain paths are considered unimportant.
      $sender_data['updates'][$module]['module_path'] = str_replace('/' . $module, '', drupal_get_path('module', $module));
    }
  }

  // Send active module data, to allow us to act on uninstalled modules.
  $enabled_modules = $this->moduleHandler
    ->getModuleList();
  $sender_data['enabled'] = [];
  foreach ($enabled_modules as $enabled_key => $enabled_module) {
    $sender_data['enabled'][$enabled_key] = $enabled_key;
  }
  $enabled_themes = $this->themeHandler
    ->listInfo();
  foreach ($enabled_themes as $enabled_key => $enabled_theme) {
    $sender_data['enabled'][$enabled_key] = $enabled_key;
  }

  // Retrieve active installation profile data.
  // We mark this as enabled send this if we are using an installation profile
  // that the Update Manager module also reports on. Otherwise, Evercurrent
  // will not tell us about updates for it.
  $install_profile = Settings::get('install_profile');
  if ($install_profile && in_array($install_profile, array_keys($sender_data['updates']))) {
    $sender_data['enabled'][$install_profile] = $install_profile;
  }

  // Expose hook to add anything else.
  $this->moduleHandler
    ->alter('evercurrent_update_data', $sender_data);

  // Send the updates to the server.
  $path = $sender_data['send_url'] . RMH_URL;

  // Set up a request.

  /** @var \Drupal::httpClient $client */
  try {
    $response = \Drupal::httpClient()
      ->request('POST', $path, [
      'body' => json_encode($sender_data),
      'headers' => [
        'Content-Type' => 'application/json',
      ],
    ]);
  } catch (\Exception $e) {
    $this
      ->writeStatus(RMH_STATUS_ERROR, 'When trying to reach the server URL, Drupal reported the followng connection error: ' . $e
      ->getMessage(), $out);
    return FALSE;
  }
  $code = $response
    ->getStatusCode();
  $body = (string) $response
    ->getBody();
  if (!$response
    ->getStatusCode() == 200) {
    $this
      ->writeStatus(RMH_STATUS_ERROR, 'Error code ' . $code . ' when trying to post to ' . $path, $out);
    return FALSE;
  }
  else {

    // Check the response data, was it successful?
    $response_data = Json::decode($body);
    if ($response_data) {
      $saved = $response_data['saved'];
      if (!$saved) {
        $this
          ->writeStatus(RMH_STATUS_ERROR, $response_data['message'], $out);
        return FALSE;
      }
      else {
        \Drupal::state()
          ->set('evercurrent_last_run', time());
        $this
          ->writeStatus(RMH_STATUS_OK, $response_data['message'], $out);

        // If successful, we want to reassure that listening mode is off.
        $this
          ->disableListening();
        return TRUE;
      }
    }
  }
  return FALSE;
}