You are here

class UpdateStatusSensorPlugin in Monitoring 8

Monitors for available updates of Drupal core and installed contrib modules.

@SensorPlugin( id = "update_status", label = @Translation("Update status"), description = @Translation("Monitors for available updates of Drupal core and installed contrib modules."), addable = FALSE, provider = "update" )

Based on drupal core update module.

Hierarchy

Expanded class hierarchy of UpdateStatusSensorPlugin

File

src/Plugin/monitoring/SensorPlugin/UpdateStatusSensorPlugin.php, line 27
Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\UpdateStatusSensorPlugin.

Namespace

Drupal\monitoring\Plugin\monitoring\SensorPlugin
View source
class UpdateStatusSensorPlugin extends SensorPluginBase {

  /**
   * {@inheritdoc}
   */
  protected $configurableValueType = FALSE;

  /**
   * {@inheritdoc}
   */
  public function runSensor(SensorResultInterface $result) {
    $type = $this->sensorConfig
      ->getSetting('type');
    $status = $this
      ->calculateStatus($type);
    $result
      ->setStatus($status);
    $available = update_get_available();
    $project_data = update_calculate_project_data($available);
    if ($type == 'core') {
      $this
        ->checkCore($result, $project_data);
    }
    else {
      $this
        ->checkContrib($result, $project_data);
    }
  }

  /**
   * Checks core status and sets sensor status message.
   *
   * @param \Drupal\monitoring\Result\SensorResultInterface $result
   * @param array $project_data
   */
  protected function checkCore(SensorResultInterface $result, $project_data) {
    $info = $project_data['drupal'];
    $status = $this
      ->getStatusText($info['status']);
    if ($status == 'unknown') {
      $result
        ->addStatusMessage('Core update status unknown');

      // Do not escalate in case the status is unknown.
      $result
        ->setStatus(SensorResultInterface::STATUS_INFO);
    }
    elseif ($status == 'current') {
      $result
        ->addStatusMessage('Core up to date');
    }
    else {
      $result
        ->addStatusMessage('Core (@current) - @status - latest @latest', array(
        '@status' => $status,
        '@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
        '@latest' => isset($info['latest_version']) ? $info['latest_version'] : NULL,
      ));
    }
  }

  /**
   * Checks contrib status and sets sensor status message.
   *
   * @param \Drupal\monitoring\Result\SensorResultInterface $result
   * @param array $project_data
   */
  protected function checkContrib(SensorResultInterface $result, $project_data) {
    unset($project_data['drupal']);
    $updates = [];
    $important_updates = [];
    foreach ($project_data as $info) {
      $status_text = $this
        ->getStatusText($info['status']);
      if (!isset($updates[$status_text])) {
        $updates[$status_text] = 0;
      }
      $updates[$status_text]++;
      if ($status_text == 'NOT SECURE' || $status_text == 'not supported') {
        if (!isset($important_updates[$status_text])) {
          $important_updates[$status_text] = [];
        }
        $important_updates[$status_text][] = $info;
      }
    }
    foreach ($updates as $status_text => $count) {
      $result
        ->addStatusMessage($count . ' ' . $status_text);
    }
    foreach ($important_updates as $status_text => $update_info) {
      foreach ($update_info as $info) {
        if ($status_text == 'NOT SECURE') {
          $result
            ->addStatusMessage('@module (@status, @current => @recommended)', array(
            '@module' => $info['info']['name'],
            '@status' => $status_text,
            '@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
            '@recommended' => isset($info['recommended']) ? $info['recommended'] : NULL,
          ));
        }
        else {
          $result
            ->addStatusMessage('@module (@status, @current)', array(
            '@module' => $info['info']['name'],
            '@status' => $status_text,
            '@current' => isset($info['existing_version']) ? $info['existing_version'] : NULL,
          ));
        }
      }
    }
  }

  /**
   * Gets status text.
   *
   * @param int $status
   *   One of UpdateManagerInterface::* constants.
   *
   * @return string
   *   Status text.
   */
  protected function getStatusText($status) {
    switch ($status) {
      case UpdateManagerInterface::NOT_SECURE:
        return 'NOT SECURE';
        break;
      case UpdateManagerInterface::CURRENT:
        return 'current';
        break;
      case UpdateManagerInterface::REVOKED:
        return 'version revoked';
        break;
      case UpdateManagerInterface::NOT_SUPPORTED:
        return 'not supported';
        break;
      case UpdateManagerInterface::NOT_CURRENT:
        return 'update available';
        break;
      case UpdateFetcherInterface::UNKNOWN:
      case UpdateFetcherInterface::NOT_CHECKED:
      case UpdateFetcherInterface::NOT_FETCHED:
      case UpdateFetcherInterface::FETCH_PENDING:
        return 'unknown';
        break;
    }
  }

  /**
   * Executes the update requirements hook and calculates the status for it.
   *
   * @param string $type
   *   Which types of updates to check for, core or contrib.
   *
   * @return string
   *   One of the SensorResultInterface status constants.
   */
  protected function calculateStatus($type) {
    \Drupal::service('module_handler')
      ->loadInclude('update', 'install');
    $requirements = update_requirements('runtime');
    $update_info = array();
    if (isset($requirements['update_' . $type])) {
      $update_info = $requirements['update_' . $type];
    }
    $update_info += array(
      'severity' => REQUIREMENT_OK,
    );
    if ($update_info['severity'] == REQUIREMENT_OK) {
      return SensorResultInterface::STATUS_OK;
    }
    elseif ($update_info['severity'] == REQUIREMENT_INFO) {
      return SensorResultInterface::STATUS_INFO;
    }
    elseif ($update_info['severity'] == REQUIREMENT_WARNING) {
      return SensorResultInterface::STATUS_INFO;
    }
    else {
      return SensorResultInterface::STATUS_CRITICAL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
SensorPluginBase::$pluginDefinition protected property The plugin implementation definition.
SensorPluginBase::$pluginId protected property The plugin_id.
SensorPluginBase::$sensorConfig protected property Current sensor config object.
SensorPluginBase::$services protected property
SensorPluginBase::addService public function Service setter. Overrides SensorPluginInterface::addService
SensorPluginBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 13
SensorPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides SensorPluginInterface::calculateDependencies 4
SensorPluginBase::create public static function Creates an instance of the sensor with config. Overrides SensorPluginInterface::create 7
SensorPluginBase::getConfigurableValueType public function Configurable value type. Overrides SensorPluginInterface::getConfigurableValueType
SensorPluginBase::getDefaultConfiguration public function Default configuration for a sensor. Overrides SensorPluginInterface::getDefaultConfiguration 8
SensorPluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
SensorPluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
SensorPluginBase::getSensorId public function Gets sensor name (not the label). Overrides SensorPluginInterface::getSensorId
SensorPluginBase::getService public function @todo: Replace with injection Overrides SensorPluginInterface::getService
SensorPluginBase::isEnabled public function Determines if sensor is enabled. Overrides SensorPluginInterface::isEnabled
SensorPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 3
SensorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
SensorPluginBase::__construct function Instantiates a sensor object. 8
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UpdateStatusSensorPlugin::$configurableValueType protected property Allows plugins to control if the value type can be configured. Overrides SensorPluginBase::$configurableValueType
UpdateStatusSensorPlugin::calculateStatus protected function Executes the update requirements hook and calculates the status for it.
UpdateStatusSensorPlugin::checkContrib protected function Checks contrib status and sets sensor status message.
UpdateStatusSensorPlugin::checkCore protected function Checks core status and sets sensor status message.
UpdateStatusSensorPlugin::getStatusText protected function Gets status text.
UpdateStatusSensorPlugin::runSensor public function Runs the sensor, updating $sensor_result. Overrides SensorPluginInterface::runSensor