You are here

public function UpdateHelper::sendUpdates in Evercurrent 8

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

Send updates to the Maintenance server.

Parameters

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

$key: Use another key than what is saved

$out: Display error messages as screen messages

Return value

mixed

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 64
Contains Drupal\evercurrent\UpdateHelper.

Class

UpdateHelper
Class UpdateHelper.

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 = array();
  if ($available = update_get_available(TRUE)) {
    module_load_include('inc', 'update', 'update.compare');
    $data = update_calculate_project_data($available);
  }

  // Module version
  $version = system_get_info('module', 'evercurrent');
  $sender_data = [
    'send_url' => $config
      ->get('target_address'),
    'project_name' => $this
      ->get_environment_url(),
    'key' => $key,
    'module_version' => $version['version'],
    'api_version' => 1,
    'updates' => [],
  ];
  $status_list = [
    UPDATE_NOT_SECURE,
    UPDATE_REVOKED,
    UPDATE_NOT_SUPPORTED,
    UPDATE_CURRENT,
    UPDATE_NOT_CHECKED,
    UPDATE_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->module_handler
    ->getModuleList();
  $sender_data['enabled'] = array();
  foreach ($enabled_modules as $enabled_key => $enabled_module) {
    $sender_data['enabled'][$enabled_key] = $enabled_key;
  }
  $enabled_themes = $this->theme_handler
    ->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->module_handler
    ->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, [
      'form_params' => array(
        'data' => json_encode($sender_data),
      ),
    ]);
  } 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;
}