You are here

public function MonitoringSensorConfigResource::get in Monitoring 8

Responds to sensor INFO GET requests.

Parameters

string $id: (optional) The sensor name, returns a list of all sensors when empty.

Return value

\Drupal\rest\ResourceResponse The response containing the sensor config.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

File

src/Plugin/rest/resource/MonitoringSensorConfigResource.php, line 93
Definition of Drupal\monitoring\Plugin\rest\resource\MonitoringSensorResource.

Class

MonitoringSensorConfigResource
Provides a resource for monitoring sensors.

Namespace

Drupal\monitoring\Plugin\rest\resource

Code

public function get($id = NULL) {
  $format = \Drupal::request()
    ->getRequestFormat('ĵson');
  if ($id) {
    try {
      $sensor_config = $this->sensorManager
        ->getSensorConfigByName($id);
    } catch (NonExistingSensorException $e) {
      throw new NotFoundHttpException($e
        ->getMessage(), $e);
    }
    $response = $sensor_config
      ->getDefinition();
    $url = Url::fromRoute('rest.monitoring-sensor.GET', [
      'id' => $id,
      '_format' => $format,
    ])
      ->setAbsolute()
      ->toString(TRUE);
    $response['uri'] = $url
      ->getGeneratedUrl();
    $response = new ResourceResponse($response);
    $response
      ->addCacheableDependency($url);
    $response
      ->addCacheableDependency($sensor_config);
    return $response;
  }
  $list = array();
  $cacheable_metadata = new CacheableMetadata();
  foreach ($this->sensorManager
    ->getAllSensorConfig() as $id => $sensor_config) {
    $list[$id] = $sensor_config
      ->getDefinition();
    $url = Url::fromRoute('rest.monitoring-sensor.GET', [
      'id' => $id,
      '_format' => $format,
    ])
      ->setAbsolute()
      ->toString(TRUE);
    $list[$id]['uri'] = $url
      ->getGeneratedUrl();
    $cacheable_metadata = $cacheable_metadata
      ->merge($url);
    $cacheable_metadata = $cacheable_metadata
      ->merge(CacheableMetadata::createFromObject($sensor_config));
  }
  $response = new ResourceResponse($list);
  $response
    ->addCacheableDependency($cacheable_metadata);
  return $response;
}