ValueComparisonSensorPluginBase.php in Monitoring 8
File
src/SensorPlugin/ValueComparisonSensorPluginBase.php
View source
<?php
namespace Drupal\monitoring\SensorPlugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\monitoring\Result\SensorResultInterface;
abstract class ValueComparisonSensorPluginBase extends SensorPluginBase {
protected abstract function getValueDescription();
protected abstract function getValue();
protected function getValueText() {
if ($this->sensorConfig
->isBool()) {
$actual_value = $this
->getValue() ? 'TRUE' : 'FALSE';
}
else {
$actual_value = $this
->getValue();
}
return $actual_value;
}
protected function getExpectedValue() {
return $this->sensorConfig
->getSetting('value');
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['value'] = array(
'#title' => t('Expected value'),
'#description' => $this
->getValueDescription(),
'#default_value' => $this
->getExpectedValue(),
);
if ($this->sensorConfig
->isBool()) {
$form['value']['#type'] = 'checkbox';
}
elseif ($this->sensorConfig
->isNumeric()) {
$form['value']['#type'] = 'number';
}
else {
$form['value']['#type'] = 'textfield';
}
return $form;
}
public function runSensor(SensorResultInterface $result) {
$result
->setValue($this
->getValue());
$result
->setExpectedValue($this
->getExpectedValue());
}
}