You are here

public function SensorManager::enableSensor in Monitoring 8

Same name and namespace in other branches
  1. 7 lib/Drupal/monitoring/Sensor/SensorManager.php \Drupal\monitoring\Sensor\SensorManager::enableSensor()

Enable a sensor.

Checks if the sensor is enabled and enables it if not.

Parameters

string $sensor_name: Sensor name to be enabled.

Throws

\Drupal\monitoring\Sensor\NonExistingSensorException Thrown if the requested sensor does not exist.

File

src/Sensor/SensorManager.php, line 192
Contains \Drupal\monitoring\Sensor\SensorManager.

Class

SensorManager
Manages sensor definitions and settings.

Namespace

Drupal\monitoring\Sensor

Code

public function enableSensor($sensor_name) {
  $sensor_config = $this
    ->getSensorConfigByName($sensor_name);
  if (!$sensor_config
    ->isEnabled()) {
    $sensor_config->status = TRUE;
    $sensor_config
      ->save();
    $available_sensors = \Drupal::state()
      ->get('monitoring.available_sensors', array());
    if (!isset($available_sensors[$sensor_name])) {

      // Use the watchdog message as the disappeared sensor does when new
      // sensors are detected.
      \Drupal::logger('monitoring')
        ->notice('@count new sensor/s added: @names', array(
        '@count' => 1,
        '@names' => $sensor_name,
      ));
    }
    $available_sensors[$sensor_name]['enabled'] = TRUE;
    $available_sensors[$sensor_name]['name'] = $sensor_name;
    \Drupal::state()
      ->set('monitoring.available_sensors', $available_sensors);
  }
}